VBA Run Time Error 424 Object Required

drew27c

New Member
Joined
Aug 16, 2016
Messages
24
I have am having trouble with this line in my macro below.

Code:
Range("J2:J" & LastRow).Value = "=IFERROR(VLOOKUP(H2," & LCase(sh.Name) & "_rate,3,FALSE),"""")"
The code is supposed to change the named range in the formula based on the sheet name. I can't figure out why I am getting this error.

Code:
Sub macro()
    Dim sh As Variant
    For Each sh In Array("vr1", "vr2", "vr3", "ht1", "ht2", "ht3")
        Sheets(sh).Columns.AutoFit
        Sheets(sh).Columns.HorizontalAlignment = xlLeft
        Sheets(sh).Columns("L:M").ColumnWidth = 10
        Sheets(sh).Columns("N:P").ColumnWidth = 4
        Sheets(sh).Columns("R:T").ColumnWidth = 30
        Sheets(sh).Columns("H:I").HorizontalAlignment = xlCenter
        Sheets(sh).Columns("N:P").HorizontalAlignment = xlCenter
        Dim LastRow As Long
        LastRow = Range("B" & Rows.Count).End(xlUp).Row 'find last row in dataset
        Range("N2:N" & LastRow).Value = "=IFERROR(VLOOKUP(B2,cam_pivot,2,FALSE),"""")" 
        Range("O2:O" & LastRow).Value = "=IFERROR(VLOOKUP(B2,auto_pivot,2,FALSE),"""")" 
        Range("P2:P" & LastRow).Value = "=IFERROR(VLOOKUP(B2,per_pivot,2,FALSE),"""")" 
        Range("J2:J" & LastRow).Value = "=IFERROR(VLOOKUP(H2," & LCase(sh.Name) & "_rate,3,FALSE),"""")"
        Range("K2:K" & LastRow).Value = "=PRODUCT(I2,J2)" 'tally rate * qty
        Next sh
        Worksheets("VR1").Activate
        Range("A2").Select
End Sub
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
Try removing the .Name so that it's just LCase(sh), although as all the values in the array are lowercase you could get rid of the LCase as well
 
Upvote 0
Try removing the .Name so that it's just LCase(sh), although as all the values in the array are lowercase you could get rid of the LCase as well

OK! so that removes the error, but I am getting the wrong range in the formula per sheet, or no formula at all.
 
Upvote 0
Do you have named ranges such as vr1_rate?
 
Upvote 0
Assuming that you are trying to put the formula onto all those sheets try
Code:
Sub macro()
   Dim sh As Variant
   For Each sh In Array("vr1", "vr2", "vr3", "ht1", "ht2", "ht3")
      With Sheets(sh)
         .Columns.AutoFit
         .Columns.HorizontalAlignment = xlLeft
         .Columns("L:M").ColumnWidth = 10
         .Columns("N:P").ColumnWidth = 4
         .Columns("R:T").ColumnWidth = 30
         .Columns("H:I").HorizontalAlignment = xlCenter
         .Columns("N:P").HorizontalAlignment = xlCenter
         Dim LastRow As Long
         LastRow = .Range("B" & Rows.Count).End(xlUp).Row 'find last row in dataset
         .Range("N2:N" & LastRow).Value = "=IFERROR(VLOOKUP(B2,cam_pivot,2,FALSE),"""")"
         .Range("O2:O" & LastRow).Value = "=IFERROR(VLOOKUP(B2,auto_pivot,2,FALSE),"""")"
         .Range("P2:P" & LastRow).Value = "=IFERROR(VLOOKUP(B2,per_pivot,2,FALSE),"""")"
         .Range("J2:J" & LastRow).Value = "=IFERROR(VLOOKUP(H2," & LCase(sh.Name) & "_rate,3,FALSE),"""")"
         .Range("K2:K" & LastRow).Value = "=PRODUCT(I2,J2)" 'tally rate * qty
      End With
   Next sh
   Worksheets("VR1").Activate
   Range("A2").Select
End Sub
 
Upvote 0
I have am having trouble with this line in my macro below.

Code:
Range("J2:J" & LastRow).Value = "=IFERROR(VLOOKUP(H2," & LCase(sh.Name) & "_rate,3,FALSE),"""")"
The code is supposed to change the named range in the formula based on the sheet name. I can't figure out why I am getting this error.

Code:
Sub macro()
    Dim sh As Variant
    For Each sh In Array("vr1", "vr2", "vr3", "ht1", "ht2", "ht3")
        Sheets(sh).Columns.AutoFit
        Sheets(sh).Columns.HorizontalAlignment = xlLeft
        Sheets(sh).Columns("L:M").ColumnWidth = 10
        Sheets(sh).Columns("N:P").ColumnWidth = 4
        Sheets(sh).Columns("R:T").ColumnWidth = 30
        Sheets(sh).Columns("H:I").HorizontalAlignment = xlCenter
        Sheets(sh).Columns("N:P").HorizontalAlignment = xlCenter
        Dim LastRow As Long
        LastRow = Range("B" & Rows.Count).End(xlUp).Row 'find last row in dataset
        Range("N2:N" & LastRow).Value = "=IFERROR(VLOOKUP(B2,cam_pivot,2,FALSE),"""")" 
        Range("O2:O" & LastRow).Value = "=IFERROR(VLOOKUP(B2,auto_pivot,2,FALSE),"""")" 
        Range("P2:P" & LastRow).Value = "=IFERROR(VLOOKUP(B2,per_pivot,2,FALSE),"""")" 
        Range("J2:J" & LastRow).Value = "=IFERROR(VLOOKUP(H2," & LCase(sh.Name) & "_rate,3,FALSE),"""")"
        Range("K2:K" & LastRow).Value = "=PRODUCT(I2,J2)" 'tally rate * qty
        Next sh
        Worksheets("VR1").Activate
        Range("A2").Select
End Sub

It worked perfectly once I removed .name (all my ranges and sheets are lowercase now).

Thank you so very much.
 
Last edited:
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,175
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