Error 91 Object Variable or With block variable not set

MRN227

New Member
Joined
Dec 18, 2018
Messages
10
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
 

Some videos you may like

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK

JLGWhiz

Well-known Member
Joined
Feb 7, 2012
Messages
11,784
Office Version
2013
Platform
Windows
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:

Watch MrExcel Video

Forum statistics

Threads
1,095,971
Messages
5,447,598
Members
405,458
Latest member
newbie111

This Week's Hot Topics

Top