Error en una macro con comentario anidado

Hoy vengo a pedirle su ayuda, cuando ejecuto la macro no me da el resultado que deseo sino otra condicion en mi comentario. Ejemplo. Selecciono 10 o mas filas y ejecuto mi macro claro cada uno con con comentario como ven en la imagen pero lo que yo deseo es que por fila salga el comentario: aguas & "la fecha que sale en la primera columna de la fila" & "la hora que depende la segunda columna de la fila" y & la celda misma eso si me sale bien como ven en la foto tengo un problema en ello.

asi es como me sale LA CUAL ESTA MAL

Asi yo deseo que me obtenga LA CUAL ESTA BIEN

Claro la hora aumenta un par de segundos cada ves k pasa a otra columna pero toda la hora es referente de la segunda columna alguna ayuda por favor si tienen dudas puedo pasarles el archivo a su correo.

1


3

A = 1
For Each rngCell In Selection
If rngCell <> Empty Then
p = rngCell.Row
c = rngCell.Column
s = Range(primerabusqueda).Offset(p - I, 0).Value
r = Range(primerabusqueda).Offset(p - I, 1).Value
If rngCell.Comment Is Nothing Then
rngCell.AddComment
If IsDate(rngCell.Value) = True Then
rngCell.Comment.Text Text:="aguas " & s & " " & TimeValue(r) + TimeValue("0:00:10") & " " & Format(rngCell.Value, "yyyy-mm-dd")
ElseIf rngCell.Value = Range(primerabusqueda).Offset(p - I, 1).Value Then
rngCell.Comment.Text Text:="aguas " & s & " " & TimeValue(r) + TimeValue("0:00:10") & " " & Format(rngCell.Value, "hh:mm:ss AM/PM")
Else
rngCell.Comment.Text Text:="aguas " & s & " " & TimeValue(r) + TimeValue("0:00:10") & " " & rngCell.Value
End If
With rngCell
.Comment.Shape.Width = 120.64
.Comment.Shape.Height = 84.64
End With
A = A + 1
End If
End If
Next rngCell
On Error GoTo 0
ElseIf Not Selection.Find("día", , xlValues, xlWhole, xlByColumns, xlNext, False, , False) Is Nothing Then
Set resultado = Selection.Find("día", , xlValues, xlWhole, xlByColumns, xlNext, False, , False)
primerabusqueda = resultado.Address
Intersect(Selection, Selection. Offset(ActiveCell.Offset(1, 0). Row - ActiveCell. Row, 0)).Select
A = 1
For Each rngCell In Selection
If rngCell <> Empty Then
p = rngCell.Row
c = rngCell.Column
s = Range(primerabusqueda).Offset(p - I, 0).Value
H = Abs(4 * Sin(p - I) - 0.1 / (-Sin(6 / (-3.14159264 * (p - I) / 180))))
r = Application.WorksheetFunction.Text(Now() + 0.041666667 * H + c * 0.00001159 * (3 - 1 / A), "h:mm:ss AM/PM")
If rngCell.Comment Is Nothing Then
rngCell.AddComment
If IsDate(rngCell.Value) = True Then
rngCell.Comment.Text Text:="aguas " & s & " " & r & " " & Format(rngCell.Value, "yyyy-mm-dd")
Else
rngCell.Comment.Text Text:="aguas " & s & " " & r & " " & rngCell.Value
End If
With rngCell
.Comment.Shape.Width = 120.64
.Comment.Shape.Height = 84.64
End With
A = A + 1
End If
End If
Next rngCell
On Error GoTo 0
End If
Else
'realiza una nueva seleccion sin tomar en cuenta el encabezado de la tabla
Intersect(Selection, Selection. Offset(ActiveCell.Offset(1, 0). Row - ActiveCell. Row, 0)).Select
A = 1
For Each rngCell In Selection
If rngCell <> Empty Then
p = rngCell.Row
c = rngCell.Column
s = Range(primerabusqueda).Offset(p - I, 0).Value
H = Abs(4 * Sin(p - I) - 0.1 / (-Sin(6 / (-3.14159264 * (p - I) / 180))))
r = Application.WorksheetFunction.Text(Now() + 0.041666667 * H + c * 0.00001159 * (3 - 1 / A), "h:mm:ss AM/PM")
If rngCell.Comment Is Nothing Then
rngCell.AddComment
If IsDate(rngCell.Value) = True Then
rngCell.Comment.Text Text:="aguas " & s & " " & r & " " & Format(rngCell.Value, "yyyy-mm-dd")
Else
rngCell.Comment.Text Text:="aguas " & s & " " & r & " " & rngCell.Value
End If
With rngCell
.Comment.Shape.Width = 120.64
.Comment.Shape.Height = 84.64
End With
A = A + 1
End If
End If
Next rngCell
On Error GoTo 0
End If
End Sub

        

1 respuesta

Respuesta
1

Te anexo el código actualizado para esta petición:

Lo que yo deseo es que por fila salga el comentario: aguas & "la fecha que sale en la primera columna de la fila" & "la hora que depende la segunda columna de la fila" y & la celda misma

Suponiendo que la fecha la tienes en la columna A, la hora en la B y el dato en la C.

Entonces cambia esta parte de tu código:

For Each rngCell In Selection
If rngCell <> Empty Then
p = rngCell.Row
c = rngCell.Column
s = Range(primerabusqueda).Offset(p - I, 0).Value
r = Range(primerabusqueda).Offset(p - I, 1).Value
If rngCell.Comment Is Nothing Then
rngCell.AddComment
If IsDate(rngCell.Value) = True Then
rngCell.Comment.Text Text:="aguas " & s & " " & TimeValue(r) + TimeValue("0:00:10") & " " & Format(rngCell.Value, "yyyy-mm-dd")
ElseIf rngCell.Value = Range(primerabusqueda).Offset(p - I, 1).Value Then
rngCell.Comment.Text Text:="aguas " & s & " " & TimeValue(r) + TimeValue("0:00:10") & " " & Format(rngCell.Value, "hh:mm:ss AM/PM")
Else
rngCell.Comment.Text Text:="aguas " & s & " " & TimeValue(r) + TimeValue("0:00:10") & " " & rngCell.Value
End If
With rngCell
.Comment.Shape.Width = 120.64
.Comment.Shape.Height = 84.64
End With
A = A + 1
End If
End If
Next rngCell

Por este código:

    For Each rngcell In Selection
        If rngcell.Value <> Empty Then
            wcom = Format(Cells(rngcell.Row, "A").Value, "yyyy/mm/dd") & " " & _
                   Format(Cells(rngcell.Row, "B").Value, "h:mm:ss AM/PM") & " " & _
                   Cells(rngcell.Row, "C").Value
            If rngcell.Comment Is Nothing Then rngcell.AddComment
            rngcell.Comment.Text Text:="aguas " & wcom
            rngcell.Comment.Shape.Width = 120.64
            rngcell.Comment.Shape.Height = 84.64
        End If
    Next rngcell

Prueba y me comentas qué le hace falta, ya que hay algunas partes en tu macro que no entendí y tampoco pusiste una explicación, pero si solamente necesitas el comentario con la fecha, la hora y el valor de la celda, entonces el código que te estoy enviando te va a funcionar.


[' Si es lo que necesitas. No olvides valorar la respuesta. 

Me sale lo mismo por favor ayúdame lo que pasa es que no pude anexar el código completo por que era muy largo

Sub comment2()
Dim rngCell As Range
Dim resultado As Range
Dim primerabusqueda As String
On Error Resume Next
If Not Selection.Find("FECHA", , xlValues, xlWhole, xlByColumns, xlNext, False, , False) Is Nothing Then
Set resultado = Selection.Find("FECHA", , xlValues, xlWhole, xlByColumns, xlNext, False, , False)
primerabusqueda = resultado.Address
ElseIf Not Selection.Find("FECHA DE CONTROL", , xlValues, xlWhole, xlByColumns, xlNext, False, , False) Is Nothing Then
Set resultado = Selection.Find("FECHA DE CONTROL", , xlValues, xlWhole, xlByColumns, xlNext, False, , False)
primerabusqueda = resultado.Address
ElseIf Not Selection.Find("día", , xlValues, xlWhole, xlByColumns, xlNext, False, , False) Is Nothing Then
Set resultado = Selection.Find("día", , xlValues, xlWhole, xlByColumns, xlNext, False, , False)
primerabusqueda = resultado.Address
End If
I = ActiveCell.Offset(1, 0).Row - 1
If Not Selection.Find("HORA", , xlValues, xlWhole, xlByColumns, xlNext, False, , False) Is Nothing Then
If Not Selection.Find("FECHA", , xlValues, xlWhole, xlByColumns, xlNext, False, , False) Is Nothing Then
Set resultado = Selection.Find("FECHA", , xlValues, xlWhole, xlByColumns, xlNext, False, , False)
primerabusqueda = resultado.Address
Intersect(Selection, Selection.Offset(ActiveCell.Offset(1, 0).Row - ActiveCell.Row, 0)).Select
A = 1
For Each rngCell In Selection
If rngCell <> Empty Then
p = rngCell.Row
c = rngCell.Column
s = Range(primerabusqueda).Offset(p - I, 0).Value
r = Range(primerabusqueda).Offset(p - I, 1).Value
If rngCell.Comment Is Nothing Then
rngCell.AddComment
If IsDate(rngCell.Value) = True Then
rngCell.Comment.Text Text:="aguas " & s & " " & TimeValue(r) + TimeValue("0:00:10") & " " & Format(rngCell.Value, "yyyy-mm-dd")
ElseIf rngCell.Value = Range(primerabusqueda).Offset(p - I, 1).Value Then
rngCell.Comment.Text Text:="aguas " & s & " " & TimeValue(r) + TimeValue("0:00:10") & " " & Format(rngCell.Value, "hh:mm:ss AM/PM")
Else
rngCell.Comment.Text Text:="aguas " & s & " " & TimeValue(r) + TimeValue("0:00:10") & " " & rngCell.Value
End If
With rngCell
.Comment.Shape.Width = 120.64
.Comment.Shape.Height = 84.64
End With
A = A + 1
End If
End If
Next rngCell
On Error GoTo 0
ElseIf Not Selection.Find("día", , xlValues, xlWhole, xlByColumns, xlNext, False, , False) Is Nothing Then
Set resultado = Selection.Find("día", , xlValues, xlWhole, xlByColumns, xlNext, False, , False)
primerabusqueda = resultado.Address
Intersect(Selection, Selection.Offset(ActiveCell.Offset(1, 0).Row - ActiveCell.Row, 0)).Select
A = 1
For Each rngCell In Selection
If rngCell <> Empty Then
p = rngCell.Row
c = rngCell.Column
s = Range(primerabusqueda).Offset(p - I, 0).Value
H = Abs(4 * Sin(p - I) - 0.1 / (-Sin(6 / (-3.14159264 * (p - I) / 180))))
r = Application.WorksheetFunction.Text(Now() + 0.041666667 * H + c * 0.00001159 * (3 - 1 / A), "h:mm:ss AM/PM")
If rngCell.Comment Is Nothing Then
rngCell.AddComment
If IsDate(rngCell.Value) = True Then
rngCell.Comment.Text Text:="aguas " & s & " " & r & " " & Format(rngCell.Value, "yyyy-mm-dd")
Else
rngCell.Comment.Text Text:="aguas " & s & " " & r & " " & rngCell.Value
End If
With rngCell
.Comment.Shape.Width = 120.64
.Comment.Shape.Height = 84.64
End With
A = A + 1
End If
End If
Next rngCell
On Error GoTo 0
End If
Else
'realiza una nueva seleccion sin tomar en cuenta el encabezado de la tabla
Intersect(Selection, Selection.Offset(ActiveCell.Offset(1, 0).Row - ActiveCell.Row, 0)).Select
A = 1
For Each rngCell In Selection
If rngCell <> Empty Then
p = rngCell.Row
c = rngCell.Column
s = Range(primerabusqueda).Offset(p - I, 0).Value
H = Abs(4 * Sin(p - I) - 0.1 / (-Sin(6 / (-3.14159264 * (p - I) / 180))))
r = Application.WorksheetFunction.Text(Now() + 0.041666667 * H + c * 0.00001159 * (3 - 1 / A), "h:mm:ss AM/PM")
If rngCell.Comment Is Nothing Then
rngCell.AddComment
If IsDate(rngCell.Value) = True Then
rngCell.Comment.Text Text:="aguas " & s & " " & r & " " & Format(rngCell.Value, "yyyy-mm-dd")
Else
rngCell.Comment.Text Text:="aguas " & s & " " & r & " " & rngCell.Value
End If
With rngCell
.Comment.Shape.Width = 120.64
.Comment.Shape.Height = 84.64
End With
A = A + 1
End If
End If
Next rngCell
On Error GoTo 0
End If
End Sub

Mi fila tiene 10 columnas y deseo que siempre tenga ese formato que le mencione por favor ayúdeme aguas & "la fecha que sale en la primera columna de la fila" & "la hora que depende la segunda columna de la fila" y & el dato de la celda

Allí le adjunte mi macro completa espero me pueda guiar y si tiene una duda más le podría pasar mi archivo por correo agradecería su ayuda.

Con mi código no sale lo mismo

Solamente prueba mi código.

En un módulo nuevo pon el siguiente código:

sub nuevo()    
    For Each rngcell In Selection
        If rngcell.Value <> Empty Then
            wcom = Format(Cells(rngcell.Row, "A").Value, "yyyy/mm/dd") & " " & _
                   Format(Cells(rngcell.Row, "B").Value, "h:mm:ss AM/PM") & " " & _
                   Cells(rngcell.Row, "C").Value
            If rngcell.Comment Is Nothing Then rngcell.AddComment
            rngcell.Comment.Text Text:="aguas " & wcom
            rngcell.Comment.Shape.Width = 120.64
            rngcell.Comment.Shape.Height = 84.64
        End If
    Next rngcell
end sub

Selecciona las celdas a las que le quieres poner el comentario y ejecuta mi código.

Después de que ejecutes mi código, revisamos tu código y me explicas qué falta.

También pon una imagen de tus datos, pero que se vean las filas y las columnas de la hoja de excel.

Hola amigo Dante Amor  me gustaría que me ayude aquí le mando una foto con una mejor idea lo que deseo es que me salga este formato en el comentario al seleccionar un rango aguas & "la fecha que sale en la primera columna de la fila" & "la hora que depende la segunda columna de la fila" y & la info de la celda

Me gustaría que la hora que depende de la columna "C" considere los segundos osea mientras pasa cada columna en la fila aumente los segundos un poco 3 o 4 segundos como se muestra en la imagen gracias por su apoyo.

Esto me gustaría que salga.

Pero al momento de ejecutar la macro me salen estos valores que no son los que deseo

le falta la fecha como ve y la hora no se guía de la columna "C" de su fila.

Aquí como se sale el mismo error no sale la fecha y la hora no se guía de la columna "C" de su fila muchas gracias 

Pero no ejecutaste mi macro.

Esto es lo que pediste:

Lo que yo deseo es que por fila salga el comentario: aguas & "la fecha que sale en la primera columna de la fila" & "la hora que depende la segunda columna de la fila" y & la celda misma

Y eso justamente hace mi macro.


Con todo gusto te ayudo con todas tus peticiones, pero debes valorar esta respuesta y abrir una nueva pregunta. En la nueva pregunta describes con detalle lo que necesitas.

Añade tu respuesta

Haz clic para o

Más respuestas relacionadas