Object Required Error in VBA code

davec8723

New Member
Joined
Jul 29, 2011
Messages
45
Hello,

I am having some trouble with code I am trying to write. I need the variable LR to be redefined on each worksheet the code works on. I keep getting an error on the line underlined below. Hoping someone can help.

Code:
Sub Update_Actuals()


Dim LR As Variant, i As Long, NUM As Long, ws As Variant
NUM = Sheet2.Range("E46").Value


Application.ScreenUpdating = False


For Each ws In Array("MT IMP", "MT RES IMP", "MT R")


    With Sheets(ws)
[U]        Set LR = ws.Range("T" & ws.Rows.Count).End(xlUp).Row[/U]
        For i = 18 To LR
            .Range("H" & i).Copy
            .Range("H" & i).Resize(, NUM).PasteSpecial _
            Paste:=xlPasteFormulas


            i = i + 16
        Next i
    End With
    
Next
 
Application.CutCopyMode = False
Application.ScreenUpdating = True


MsgBox ("Done")
    
End Sub
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Thank you for your response! If I remove the ws then I am getting a "Type Mismatch" error. Any idea why?
 
Upvote 0
What's your code now and which line causes the error? LR should really be declared as Long rather than Variant.
 
Upvote 0
Here is the code. If I change LR to Long as soon as I click F8 to begin working through the code it returns an error that object is required. Thank you very much for your help.

The line "Set LR = .Range("T" & .Rows.Count).End(xlUp).Row" causes the error

Code:
Sub Update_Actuals()


Dim LR As Variant, i As Long, NUM As Long, ws As Variant
NUM = Sheet2.Range("E46").Value


'Application.ScreenUpdating = False


For Each ws In Array("MT IMP", "MT RES IMP", "MT R")


    With Sheets(ws)
        Set LR = .Range("T" & .Rows.Count).End(xlUp).Row
        For i = 18 To LR
            .Range("H" & i).Copy
            .Range("H" & i).Resize(, NUM).PasteSpecial _
            Paste:=xlPasteFormulas


            i = i + 16
        Next i
    End With
    
Next
 
Application.CutCopyMode = False
'Application.ScreenUpdating = True


MsgBox ("Done")
    
End Sub
 
Last edited:
Upvote 0
You need to remove Set in:

Set LR = .Range("T" & .Rows.Count).End(xlUp).Row

The Set keyword is for object variables. Sorry I didn't spot that earlier.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,216,127
Messages
6,129,024
Members
449,482
Latest member
al mugheen

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