Error 91 Object Variable or With block variable not set

MRN227

New Member
Joined
Dec 18, 2018
Messages
15
I trying to write this macro with the intention of when the command button is selected specified cell data from the current sheet is copy and pasted to the next available row on another sheet. This syntax works fine on a user form but I'm having trouble trying to achieve the same thing with just a work sheet. When I try to run I get error 91. Below is what I've got so far:

Private Sub CommandButton1_Click()

Dim lg As Worksheet
Set lg = Sheets("Log")

lg.Select
lg.Range("A2:T2").Select
Selection.ListObject.ListRows.Add (1)
With Selection.Interior
.Pattern = xlNone
.TintAndShade = 0
.PatternTintAndShade = 0
End With

lg.Cells(2, 1) = Sheets("sheet2").Range("A1")

Unload Me
lg.Select

End Sub
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
Two things,
1. Use the .Value property when populating a cell with another cell's value.
Code:
lg.Cells(2, 1) = Sheets("sheet2").Range("A1").[COLOR=#ff0000]Value[/COLOR]
2. You do not need the 'Unload Me' if you are not running this code from a UserForm. It will cause an error.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,213,510
Messages
6,114,037
Members
448,543
Latest member
MartinLarkin

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