Every time I try to run my macro, the first section surrounded by the LIRCounter runs just fine, but Excel returns a "Run-time error '1004': Application-defined or object-defined error" and the Debug highlights my Range(...).Merge sections on the rest of my macro.
I have tried reformatting the range by removing the .Merge, the Counter variable, and rewriting my entire code, but I can't narrow down what part of the range is causing the error.
I have tried reformatting the range by removing the .Merge, the Counter variable, and rewriting my entire code, but I can't narrow down what part of the range is causing the error.
Sub MergeCells()
Set Worksheet = Worksheets("Technical Data")
With Worksheet
For LIRCounter = 44 To 15 Step -1
If .Cells(LIRCounter, 19).Value = Not IsEmpty(Cells(LIRCounter, 19)) Then
Else
.Range(.Cells(LIRCounter, 21), .Cells(LIRCounter, 26)).Merge
End If
If .Cells(LIRCounter, 19).Value = Not IsEmpty(Cells(LIRCounter, 19)) Then
Else
.Range(.Cells(LIRCounter, 21), .Cells(LIRCounter, 26)) = "N/A"
End If
Next LIRCounter
For ETCounter = 44 To 15 Step -1
If .Cells(ETCounter, 3).Value = "Structural" Then
.Range(.Cells(ETCounter, 4), .Cells(ETCounter, 12)).Merge
End If
If .Cells(ETCounter, 3).Value = "Structural" Then
.Range(.Cells(ETCounter, 4), .Cells(ETCounter, 12)) = "N/A - Structural"
End If
Next ETCounter
For ETCounter2 = 44 To 15 Step -1
If .Cells(ETCounter2, 3).Value = "Structural" Then
.Range(.Cells(ETCounter2, 15), .Cells(ETCounter2, 26)).Merge
End If
If .Cells(ETCounter2, 3).Value = "Structural" Then
.Range(.Cells(ETCounter2, 15), .Cells(ETCounter2, 26)) = "N/A - Structural"
End If
Next ETCounter2
End With
End Sub
The expected result is if "Structural" is selected from the dropdown menu in cell C15, then the cells D15:L15 merge into one cell, and the cells O15:Z15 merge into one cell, and both merged cells say "N/A - Structural". And the same goes for every row down to row 44. When the Macro is run, it just returns "Run-time error '1004': Application-defined or object-defined error" and no cells are merged.