I would like to copy and paste all used cells in one column on one spreadsheet to another spreadsheet. I am trying to get it done such that the report can be run many times with things being added/deleted to the first column and pasted over.
I am trying to do it with the following code, but it is giving me runtime error 91 - Object variable or With block variable not set.
Here's the code:
public Sheet1 as String, Sheet 2 as string
Sub PolicyLanguage()
Dim Range1 As Range
Set Range1 = Range("A1:A" & Cells(Rows.Count, "A").End(xlUp).Row)
ThisWorkbook.Worksheets(Sheet1).Activate
Range1.Copy Destination:=wb.Sheets(Sheet2)
End Sub
Update: I was able to solve the problem relatively cleanly by using a count function to determine the number of cells in the column that contained a value, and use that number to determine my copy range. This came in handy in a lot of other sections of my macro, so I figured I'd post it in case anyone needs it.
Dim i As Integer, n As Integer
i = Worksheets("Sheet1").Range("B:B").Cells.SpecialCells(xlCellTypeConstants).Count
n = i + 1
Worksheets("Sheet1").Range("A1:A" & CStr(n)).Copy Destination:=Sheets(NewSheetName).Range("A1:A" & CStr(n))
wb
is not defined. InsteadRange1.Copy Destination:=Sheet2.Range("A1")
. Also remove that superfluousThisWorkbook.Worksheets(Sheet1).Activate
Really no reason to active a worksheet here unless you really want that sheet to be selected when the code is done running. You could also doRange1.Copy Destination:=ThisWorkbook.Worksheets("Sheet2").Range("A1")
or similar. – JNevillSheet1
orSheet 2
– Marcucciboy2