Runtime error 1004

ccordner

Active Member
Joined
Apr 28, 2010
Messages
355
Good Morning

The following code gives me a runtime error 1004. If I take out the reference to the sheet, it works, but on the active sheet. I don't really want to activate the sheet first, because the macro alternates between two worksheets quite frequently. Is there a way of doing this without selecting the sheet first?

Code:
With Sheets("Leave Summary").Range(Cells(LeaveHeader, 1), Cells(LeaveHeader, 4)).Borders(xlEdgeLeft)
        .LineStyle = xlContinuous
        .Weight = xlThin
End With
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
You need to fully qualify the range

Code:
With Sheets("Leave Summary").Range(Sheets("Leave Summary").Cells(LeaveHeader, 1), Sheets("Leave Summary").Cells(LeaveHeader, 4)).Borders(xlEdgeLeft)
        .LineStyle = xlContinuous
        .Weight = xlThin
End With
 
Upvote 0
Hi

You need to fully qualify the Cells() references too:

Rich (BB code):
With Sheets("Leave Summary")
  With .Range(.Cells(LeaveHeader, 1), .Cells(LeaveHeader, 4)).Borders(xlEdgeLeft)
        .LineStyle = xlContinuous
        .Weight = xlThin
  End With
End With

Note the periods before the Cells()
 
Upvote 0
Just tried that, but it's saying "Invalid or unqualified reference" and highlighting the first ".cells".
 
Upvote 0
Below is the first part of the sub. The bit in red is highlighted when the error code comes up.

Code:
Sub SAPHeader()
LeaveHeader = SummaryRow + 1
If SummaryRow = 4 Then LeaveHeader = 3
With Sheets("Leave Summary")
    .Cells(LeaveHeader, 1) = LocNumber & ", " & Location
    .Cells(LeaveHeader + 2, 1) = "Pay No."
    .Cells(LeaveHeader + 2, 2) = "Name"
    .Cells(LeaveHeader + 2, 3) = "SAP Code"
    .Cells(LeaveHeader + 2, 4) = "Detail"
End With
With Sheets("Leave Summary").Range([COLOR=red].Cells[/COLOR](LeaveHeader, 1), .Cells(LeaveHeader, 4))
        .MergeCells = True
End With
With Sheets("Leave Summary").Range(.Cells(LeaveHeader, 1), .Cells(LeaveHeader, 4))
    .Size = 12
    .Color = -4165632
    .Italic = True
End With
   
With Sheets("Leave Summary").Range(.Cells(LeaveHeader, 1), .Cells(LeaveHeader, 4))
    .Bold = True
End With
   
With Sheets("Leave Summary").Range(.Cells(LeaveHeader, 1), .Cells(LeaveHeader, 4)).Borders(xlEdgeLeft)
        .LineStyle = xlContinuous
        .Weight = xlThin
End With
 
Upvote 0
Try like this

Code:
With Sheets("Leave Summary")
    .Range(.Cells(LeaveHeader, 1), .Cells(LeaveHeader, 4)).MergeCells = True
End With
 
Upvote 0

Forum statistics

Threads
1,224,570
Messages
6,179,608
Members
452,930
Latest member
racefanjtd

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top