copy & paste with variable sheet name object required vba - vba

I have a report with two sheets of data. One is called 7.26.2018 and another is All Pending. When I open the report, only 7.26.2018 is visible, and I want to have vba codes to copy & paste data from 7.26.2018 into All Pending, from column A to column N.
Each time I run this report, I need change the sheet name to the date I am working on it. So if I work on it tomorrow, I need to change the sheet name to 7.27.2018 and paste the data into All Pending tab. In essence, I am keeping all records in All Pending using the new data from 7.26.2018 sheet.
Now, I don't know how to set the sheet name to be a variable in order to copy & paste results so I am getting an error of object required for ws3. Any ideas on how to go around it?
Sub Main()
ActiveSheet.Name = Format(Date, "M.DD.YYYY")
Worksheets("All Pending").Visible = xlSheetVisible
Dim ws3, ws4 As Worksheet
Dim LR3, LR4 As Long
Set ws3 = Worksheet.Name(Date)
Set ws4 = Worksheets("All Pending")
LR3 = ws3.Cells(Rows.Count, "A").End(xlUp).Row
LR4 = ws4.Cells(Rows.Count, "A").End(xlUp).Row
ws3.Range("A2:N" & LR3).Copy
ws4.Range("A" & LR4 + 1).PasteSpecial Paste:=xlPasteValues
End Sub

Worksheet.Name(Date) is not the syntax you are looking for, since:
You want the Worksheets collection, just like the next line - ... = Worksheets("All Pending").
You've already formatted Date as "M.DD.YYY", so if you want to reference the sheet by its name, you can't just use Date. And you wouldn't use .Name either - again the next line shows you how to reference a sheet by its name - in this case Worksheets(Format(Date, "M.DD.YYYY"))
The easiest way is just to Set ws3 = ActiveSheet at the beginning, since you are also using ActiveSheet to rename the sheet in question.
Note that for variable declaration, Dim ws3, ws4 as Worksheet declares ws3 as Variant - you should have Dim ws3 as Worksheet, ws4 as Worksheet. Same goes for the next line.
So here is a revision of your original code:
Sub Main()
Dim ws3 As Worksheet, ws4 As Worksheet
Dim LR3 As Long, LR4 As Long
Set ws3 = ActiveSheet
Set ws4 = Worksheets("All Pending")
ws3.Name = Format(Date, "M.DD.YYYY")
ws4.Visible = xlSheetVisible ' Not really necessary, as you mentioned
LR3 = ws3.Cells(Rows.Count, "A").End(xlUp).Row
LR4 = ws4.Cells(Rows.Count, "A").End(xlUp).Row
ws3.Range("A2:N" & LR3).Copy
ws4.Range("A" & LR4 + 1).PasteSpecial Paste:=xlPasteValues
End Sub

Related

VBA - Copy variable range from one sheet to another

I would like to copy data from a sheet "Inv_Headers", Column C, from 2nd row until the last row to a sheet "Customers", Column U, from 4th row.
Private Sub Invoice_C()
Dim ws As Worksheet, ws1 As Worksheet
Dim lastrow As Long
Set ws = Worksheets("Inv_Headers")
Set ws2 = Worksheets("CUSTOMERS")
lastrow = ws.Cells(Rows.Count, 3).End(xlUp).Row ' last row in column C
ws.Range("C2:C" & lastrow).Copy
ws1.Range("U4").PasteSpecial xlPasteValues
ws1.Activate
End Sub
My code is giving me error msg '91' - Object variable or With block variable not set. But the code should work without With statement as well, shouldn't it?
Could I ask you for your advices, please?
Many thanks!
Based on check from #Absinthe, I've corrected the typo and here is the correct code:
Private Sub Invoice_C()
Dim ws As Worksheet, ws1 As Worksheet
Dim lastrow As Long
Set ws = Worksheets("Inv_Headers")
Set ws1 = Worksheets("CUSTOMERS")
lastrow = ws.Cells(Rows.Count, 3).End(xlUp).Row ' last row in column C
ws.Range("C2:C" & lastrow).Copy
ws1.Range("U4").PasteSpecial xlPasteValues
ws1.Activate
End Sub
In addition to Srpic's offering, I can remember not getting this part to work:
ws.Range("C2:C" & lastrow).Copy
you can fix it with ws.Range("C2", "C" & lastrow).Copy
If you start typing in Range() you will see that , is an acceptable separator, whereas : for an incomplete range assingment is not.
Private Sub Invoice_C()
Dim ws As Worksheet, ws1 As Worksheet
Dim lastrow As Long
Set ws = Worksheets("Inv_Headers")
Set ws1 = Worksheets("CUSTOMERS")
lastrow = ws.Cells(Rows.Count, 3).End(xlUp).Row ' last row in column C
ws.Range("C2", "C" & lastrow).Copy
ws1.Range("U4").PasteSpecial xlPasteValues
ws1.Activate
End Sub

VBA - Copy data from one sheet to another

I'm trying to copy data from sheet "DATEV_RAB_UNVERBINDLICH", Range D2:D to sheet "Ready to upload", Range B2:B.
I would like find data to the last row and then copy them into the other sheet. I have this code:
Sub CopyInvoiceNo()
Dim ws, ws1 As Worksheet
Dim lastrow As Long
lastrow = .Cells(.Rows.Count, "D").End(xlUp).Row
Set ws = Sheets("DATEV_RAB_UNVERBINDLICH")
Set ws1 = Sheets("Ready to upload")
ws.Range("D2" & lastrow).Copy
ws1.Range("B4").PasteSpecial xlPasteValues
ws1.Activate
End Sub
But I receive error msg Invalid or unqualified reference. Could you advise me, what do I do wrong, please?
Two errors in your code
At line 3, this is not the correct way to define multiple variable types in the same line. You have to define each of them.
At line 10, lets say your lastrow is "20". It would set the range to "D220", which is also not what you want.
Sub CopyInvoiceNo()
Dim ws As Worksheet, ws1 As Worksheet
Dim lastrow As Long
Set ws = Sheets("DATEV_RAB_UNVERBINDLICH")
Set ws1 = Sheets("Ready to upload")
lastrow = ws.Cells(Rows.count, 4).End(xlUp).Row
ws.Range("D2:D" & lastrow).Copy
ws1.Range("B4").PasteSpecial xlPasteValues
ws1.Activate
End Sub
I also changed to define the variables and its values at the start of the code. The lastrow bit didn't code here too after a bit of testing. Now it works for me at least.

Copy and Paste Dynamic Range from One Worksheet to Another

I am attempting to copy column A starting in row 2 on "Sheet1" and paste it in Column C on sheet "ABC" starting at row 5. The number of rows in Column A is variable so I cannot use a fixed range.
The code below does what I need, but I am trying to avoid using .Select and .Activate
LastRow = Range("A" & Rows.Count).End(xlUp).Row
Sheets("Sheet1").Range("A2:A" & LastRow).Copy
Sheets("ABC").Activate
Sheets("ABC").Range("C5:C" & LastRow).Select
Selection.PasteSpecial xlPasteValues
I tried setting the columns equal to one another using this code:
Sheets("ABC").Range("C5").End(xlDown).Value=Sheets("Sheet1").Range("A2:A" & LastRow).Value
This runs without error but it "does nothing" -- No data appears on worksheet "ABC"
I also tried to following:
Dim WS As Worksheet
Dim wsABC As worksheet
Set WS = Sheets("Sheet1")
Set wsABC = Sheets("ABC")
LastRow = Range("A" & Rows.Count).End(xlUp).Row
WS.Range("A2:A" & LastRow).Copy
wsABC.Range("C5").End(xlDown).Paste
This produces a "Run-time error #438 Object doesn't support this property or method" Error on this line:
wsABC.Range("C5").End(xlDown).Paste
Another method I tried was as follows:
Dim WS As Worksheet
Set WS = Sheets("Sheet1")
Set wsABC = Sheets("ABC")
With WS
LastRow = Range("A" & Rows.Count).End(xlUp).Row
WS.Range("A2:A" & LastRow).value = wsABC.Range("C5:C & LastRow").Value
End With
This produces a "Run-time error '1004' Application-defined or object defined error.
I am open to corrections / comments on any of my attempts, I just want to avoid using .Select and .Activate.
Thank you in advance for your time and assistance!
Coding styles can vary greatly. Here's one way to do what you're looking for:
Sub tgr()
Dim wb As Workbook
Dim wsData As Worksheet
Dim wsDest As Worksheet
Set wb = ActiveWorkbook
Set wsData = wb.Sheets("Sheet1")
Set wsDest = wb.Sheets("ABC")
With wsData.Range("A2", wsData.Cells(wsData.Rows.Count, "A").End(xlUp))
wsDest.Range("C5").Resize(.Rows.Count).Value = .Value
End With
End Sub
With Worksheets("Sheet 1")
With .Range("A2", .Cells(.Rows.Count, 1).End(xlUp))
Worksheets("ABC").Range("C5").Resize(.Rows.Count).Value = .Value
End With
End With

Fill down the workbook instead of overwriting the first entry over and over again

I am using the following code to copy and paste certain rows into a new workbook:
Sub ReportCreator()
Dim wbI As Workbook, wbO As Workbook
Dim wsI As Worksheet, wsO As Worksheet
Dim iCounter As Long
Dim lrow As Long
'~~> Source/Input Workbook
Set wbI = ThisWorkbook
'~~> Set the relevant sheet from where you want to copy
Set wsI = wbI.Sheets("Pharmas")
'~~> Destination/Output Workbook
Set wbO = Workbooks.Add
lastRow = ThisWorkbook.Worksheets("Pharmas").Cells(Rows.Count, "L").End(xlUp).Row
With wbO
'~~> Set the relevant sheet to where you want to paste
Set wsO = wbO.Sheets("Sheet1")
'~~>. Save the file
For iCounter = 2 To lastRow
If wsI.Cells(iCounter, 4) = "Barr" Then
wsI.Rows(iCounter).Copy
End If
wsO.Range("A").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Next iCounter
.SaveAs Filename:="C:\Users\rrrrr\Desktop\eeee.xls", FileFormat:=56
End With
End Sub
When the code goes down my list, any row with Barr in column 4 is copied and then pasted onto the new workbook.
The problem I'm encountering is that it's not pasting down the new workbook for each row it finds. Instead on the new workbook, it just overwrites the first row with newer information. When I debug, the code portion that looks for Barr and copies the row is working, but it's not pasting down the workbook, it's just overwriting the first row.
I've tried adding altering the paste code as follows:
wsI.Rows(iCounter).Copy
End If
lrow = Range("A" & .Rows.Count).End(xlUp).Row
wsO.Range("A" & lrow + 1).PasteSpecial Paste:=xlPasteValues,
However, it tells me Object doesn't support this property or method.
I am sure the paste code is incorrect but I'm not sure how to change it so it fills down the workbook instead of overwriting the first entry over and over again with each subsequent find of Barr.
Try this amended code
Sub OhYa()
Dim wbI As Workbook, wbO As Workbook
Dim wsI As Worksheet, wsO As Worksheet
Dim iCounter As Long
Dim lrow As Long, rw As Long
'~~> Source/Input Workbook
'Set wbI = ThisWorkbook
'~~> Set the relevant sheet from where you want to copy
Set wsI = Sheets("Pharmas")
'~~> Destination/Output Workbook
Set wbO = Workbooks.Add
lastRow = wsI.Cells(Rows.Count, 4).End(xlUp).Row
With wbO
'~~> Set the relevant sheet to where you want to paste
Set wsO = wbO.Sheets("Sheet1")
'~~>. Save the file
With wsI
For iCounter = 2 To lastRow
If wsI.Cells(iCounter, 4) = "Barr" Then
.Cells(iCounter, 4).EntireRow.Copy
rw = wsO.Cells(wsO.Rows.Count, "A").End(xlUp).Row + 1
wsO.Cells(rw, 1).PasteSpecial Paste:=xlPasteValues
End If
Next iCounter
'.SaveAs Filename:="C:\Users\rrrrr\Desktop\eeee.xls", FileFormat:=56
End With
End With
Application.CutCopyMode = 0
End Sub
In your solution you forgot to reference wsO:
lrow = wsO.Range("A" & wsO.Rows.Count).End(xlUp).Row
wsO.Range("A" & lrow + 1).PasteSpecial Paste:=xlPasteValues
This solution should work, but should be slow. You can run faster by keeping track of the next row in a variable and icrementing it whenever you paste a row.
If all you want are the values then do not use paste but assign the value to the cell directly.
lrow = wsO.Range("A" & wsO.Rows.Count).End(xlUp).Row
wsO.rows(lrow + 1).value =wsI.Rows(iCounter).value

Copy rows starting from certain row till the end using macro

I need to copy values of one excel and create a new one with required format. Say i need to copy columns from B11 to BG11 and rows will be till the end.( i don't know how to find the end of rows). And I have column heading in b7 to bg7. In between there are unwanted rows and i don't need it. So in the new excel i want column headings(which is from b7 to bg7) as first row and the values from b11 to bg11 till the end.
This is my first excel Macro. I don't know how to proceed. So with references from some stackoverflow question and other site, i have tried the below code. but it is not giving the required output.
Sub newFormat()
Dim LastRow As Integer, i As Integer, erow As Integer
LastRow = ActiveSheet.Range(“B” & Rows.Count).End(xlUp).Row
For i = 2 To LastRow
Sheets("MySheetName").Range("B7:BG7").Copy
Sheets("MySheetName").Range("B11:BG11").Copy
Workbooks.Open Filename:=”C:\Users\abcd\Documents\Newformat.xlsx”
Worksheets(“Sheet1”).Select
erow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
ActiveSheet.Cells(erow, 1).Select
ActiveSheet.Paste
ActiveWorkbook.Save
ActiveWorkbook.Close
Application.CutCopyMode = False
End If
Next i
End Sub
this may be simple. any help would be appreciated.
Few things...
Do not use Integer for rows. Post xl2007, the number of rows have increased and Integer can't hold that. Use Long
You do not need to select a range to paste on it. You can directly perform the action.
You do not need to use a loop. You can copy ranges in two chunks
Work with objects so Excel doesn't get confused by your objects.
Since Sheet1 is empty, you don't need to find the last row there. Simply start at 1.
To output the data to new workbook, you have to use Workbooks.Add
See this example (Untested)
Sub newFormat()
Dim wbO As Workbook
Dim wsI As Worksheet, wsO As Worksheet
Dim LastRow As Long, erow As Long
'~~> Set this to the relevant worksheet
Set wsI = ThisWorkbook.Sheets("HW SI Upload")
'~~> Find the last row in Col B
LastRow = wsI.Range("B" & wsI.Rows.Count).End(xlUp).Row
'~~> Open a new workbook
Set wbO = Workbooks.Add
'~~> Set this to the relevant worksheet
Set wsO = wbO.Sheets(1)
'~~> The first row in Col A for writing
erow = 1
'~~> Copy Header
wsI.Range("B7:BG7").Copy wsO.Range("A" & erow)
'~~> Increment output row by 1
erow = erow + 1
'~~> Copy all rows from 11 to last row
wsI.Range("B11:BG" & LastRow).Copy wsO.Range("A" & erow)
'~~> Clear Clipboard
Application.CutCopyMode = False
'
'~~> Code here to do a Save As
'
End Sub
Different but the same
Rename the sheet
Sub Button1_Click()
Dim wb As Workbook, ws As Worksheet, sh As Worksheet
Dim LstRw As Long, Rng As Range, Hrng As Range
Set sh = Sheets("MySheetName")
With sh
Set Hrng = .Range("B7:BG7")
LstRw = .Cells(.Rows.Count, "B").End(xlUp).Row
Set Rng = .Range("B11:BG" & LstRw)
End With
Application.ScreenUpdating = 0
Workbooks.Open Filename:="C:\Users\abcd\Documents\Newformat.xlsx"
Set wb = Workbooks("Newformat.xlsx")
Set ws = wb.Sheets(1)
Hrng.Copy ws.Cells(Rows.Count, "A").End(xlUp).Offset(1)
Rng.Copy ws.Cells(Rows.Count, "A").End(xlUp).Offset(1)
ws.Name = sh.Name 'renames sheet
wb.Save
wb.Close
End Sub