Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VBA code works alone, but not in larger macro.

Status
Not open for further replies.

Lippershey

Technical User
Jul 3, 2001
2
GB
Hi,
I have written a small bit of code that formats 4 cells on a sheet in Excell 97. When I run it on its own in a sub as follows it works fine:

Sub_formatcells()

Dim chkrw, chkcl As Integer
Dim sht As String

chkrw = 1
chkcl = 1
sht = "Sheet1"

With Worksheets(sht).Range(Cells(chkrw, chkcl), Cells(chkrw, chkcl + 2))
.Font.Bold = True
.Font.Color = RGB(0, 0, 255)
End With

With Worksheets(sht).Range(Cells(chkrw, chkcl + 3), Cells(chkrw, chkcl + 4))
.Font.Bold = True
.HorizontalAlignment = xlRight
End With

End Sub


Now, the problem comes when I take part of this and use it in a larger macro like this:

...
With Worksheets(sht).Range(Cells(chkrw, chkcl), Cells(chkrw, chkcl + 2))
.Font.Bold = True
.Font.Color = RGB(0, 0, 255)
End With

With Worksheets(sht).Range(Cells(chkrw, chkcl + 3), Cells(chkrw, chkcl + 4))
.Font.Bold = True
.HorizontalAlignment = xlRight
End With
...

I have checked that the variables are being correctly set and defined, and that is not the problem, but it still gives me a
"Runtime error 1004.
Application or object-defined error"

Any ideas as to why this might be happening?

Cheers,
Paul.
 
With Worksheets(sht).Range(Cells(chkrw, chkcl).address, Cells(chkrw, chkcl + 2).address)
...

it works
ide
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top