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

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
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,214,971
Messages
6,122,525
Members
449,088
Latest member
RandomExceller01

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