I’m trying to run the macro bellow and I get the following message error when I try to run it:
BASIC runtime error.
An exception occurred
Type: com.sun.star.uno.RuntimeException
Message: .
and it stops on line:
oRangeA = oSheet.getCellRangeByName(“A2:A2047” & oSheet.Rows.Count)
Anybody can help fixing the problem??
Thanks
REM ***** This macro will compare the values in Column A
REM with those in Columns D and E, and format the corresponding
REM cells in Column D or E based on the length of the value in Column C.
REM Green indicates a length greater than 1, and red indicates a length less than 2.
Sub CompareAndFormatCells()
Dim oSheet As Object
Dim oRangeA As Object
Dim oRangeD As Object
Dim oRangeE As Object
Dim oRangeC As Object
Dim i As Long
' Set the active sheet (adjust sheet name if needed)
oSheet = ThisComponent.Sheets.getByName("BD-direta")
' Define the ranges for columns A, D, E, and C
oRangeA = oSheet.getCellRangeByName("A2:A2047" & oSheet.Rows.Count)
oRangeD = oSheet.getCellRangeByName("D2:D2047" & oSheet.Rows.Count)
oRangeE = oSheet.getCellRangeByName("E2:E2047" & oSheet.Rows.Count)
oRangeC = oSheet.getCellRangeByName("C2:C2047" & oSheet.Rows.Count)
' Loop through each row in column A
For i = 0 To oRangeA.Rows.Count - 1
Dim aValue As String
Dim dValue As String
Dim eValue As String
Dim cValueLength As Integer
aValue = oRangeA.getCellByPosition(0, i).String
dValue = oRangeD.getCellByPosition(0, i).String
eValue = oRangeE.getCellByPosition(0, i).String
cValueLength = Len(oRangeC.getCellByPosition(0, i).String)
' Compare values in column A with D and E
If aValue = dValue Or aValue = eValue Then
' Check length in column C
If cValueLength > 1 Then
' Turn cell in column D or E green
oRangeD.getCellByPosition(0, i).CellBackColor = RGB(0, 255, 0) ' Green
oRangeE.getCellByPosition(0, i).CellBackColor = RGB(0, 255, 0) ' Green
Else
' Turn cell in column D or E red
oRangeD.getCellByPosition(0, i).CellBackColor = RGB(255, 0, 0) ' Red
oRangeE.getCellByPosition(0, i).CellBackColor = RGB(255, 0, 0) ' Red
End If
End If
Next i
End Sub