Object Variable or With Block Variable Not Set

SBF12345

Well-known Member
Joined
Jul 26, 2014
Messages
614
Code:
D = PA.Worksheets(g).Range(Cells(B, "A"), Cells(2, C))

receiving an error on the above line, not sure what is causing it...B, C are both integer variables, g is a string variable, and PA is a workbook variable. Syntax?
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
The line occurs within this part of the code

Code:
    For Each ws In COM.Worksheets

    A = COM.Worksheets(g).Range("A7").value


    C = PA.Worksheets(g).Cells(2, Columns.Count).End(xlToLeft).Column


    For B = C To 2 Step -1


        If A = PA.Worksheets(g).Cells(B, "A").value Then


        D = PA.Worksheets(g).Range(Cells(B, "A"), Cells(2, C))


        E = B - 2


        End If


    Next B
 
Upvote 0
It's ALWAYS easier to debug if we can see ALL the code !!
 
Upvote 0
Code:
D = PA.Worksheets(g).Range(Cells(B, "A"), Cells(2, C))

receiving an error on the above line, not sure what is causing it...
I think you need to specify the sheet reference for both of those Cells objects...
Code:
[table="width: 500"]
[tr]
	[td]D = PA.Worksheets(g).Range(PA.Worksheets(g).Cells(B, "A"), PA.Worksheets(g).Cells(2, C))[/td]
[/tr]
[/table]
or alternately (and probably "cleaner"...
Code:
With PA.Worksheets(g)
  D = .Range(.Cells(B, "A"), .Cells(2, C))
End WIth
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,159
Members
453,021
Latest member
Justyna P

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