Range, empty row in Excel vba - excel

I've found a snippet for selecting the next empty row in excel, but when I run the macro it tells me that it throws a runtime error 1004. Any idea why?
Sub CopyValues()
Sheets("Report").Select
Range("B4:D10").Select
Selection.Copy
Sheets("Data").Select
Range("A1").End(xlDown).Offset(1, 0).Select
ActiveSheet.Paste
End Sub
And yes, the worksheet names are correct.
Range("A1").End(xlDown).Offset(1, 0).Select throws the error

Sub CopyValues()
Sheets("Report").Range("B4:D10").Copy _
Destination:=Sheets("Data").Range("A" & Sheets("Data").Range("A" & Rows.Count).End(xlUp).Row + 1)
End Sub

Related

Range and .End(xlDown).Offset(1,0) Run-time error '1004'

I'm trying to copy from Sheet 1 (A1:C1) and paste a special value with offset and xlDown in Sheet 2 and place the respective results in Range (B3:D3).
Sheet 1
Sheet 4 - (Required output)
I'm getting a Run-time error '1004' Application-defined or object-defined error.
Sub test()
Workbooks("testing.xlsm").Activate
Sheet1.Select
Range("A1:C1").Copy
Sheet2.Select
Range("B3").End(xlDown).Offset(1, 0).Select
Selection.PasteSpecial Paste:=xlPasteValues
Application.CutCopyMode = False
Application.OnTime Now + TimeValue("00:00:10"), "test"
End Sub
This should work:
Sub test()
Dim wsT As Worksheet
With Workbooks("testing.xlsm")
Set wsT = .Sheets("Sheet2")
wsT.Cells(wsT.Rows.Count, "B").End(xlUp).Offset(1, 0).Resize(1, 3).Value = _
.Sheets("Sheet1").Range("A1:C1").Value
Application.OnTime Now + TimeValue("00:00:10"), "test"
End With
End Sub
It's more efficient version of your code.
However, if you prefer to keep your style, this is less efficient version:
Sub test()
Workbooks("testing.xlsm").Activate
Sheet1.Select
Range("A1:C1").Copy
Sheet2.Select
Cells(Sheet2.Rows.Count, "B").End(xlUp).Offset(1, 0).Select
Selection.PasteSpecial Paste:=xlPasteValues
Application.CutCopyMode = False
Application.OnTime Now + TimeValue("00:00:10"), "test"
End Sub

Paste and on Error display msgbox using with statement

I need some advice to rectify the code below. I have this code to copy paste lines to another sheet for data compilation purpose. And I'm running well using the with statement below, the problem is, when there's no data to paste, I do not know how to end the code with message box.
I see the similar question above, but how to comply the code into the With statement of VBA below?
Following is the code I read from other user, to return message box if error.
If Err Then
MsgBox "Nothing to paste!"
Err.Clear
End If
My original code, without the Message box return.
*Sub FnLstRow()
Application.ScreenUpdating = False
Dim LR As Long
ThisWorkbook.Worksheets("Data").Select
LR = Cells(Rows.Count, "AO").End(xlUp).Row
Cells(LR, 1).Offset(1, 0).EntireRow.Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
With Sheets("LatestData")
.Cells(.Rows.Count, "A").End(xlUp).Offset(1, 0).PasteSpecial Paste:=xlPasteValues
End With
Application.CutCopyMode = False
Range("A1").Select
Application.ScreenUpdating = True
End Sub**
this is a suboptimal solution that might not work but you could try: I created a dynamic array and tried to pass your entire selection into it. If the selection had no values, it will select all of the remaining cells in the sheet, and it is likely too large to pass into the array, resulting in a run time error. I didn't touch your code except for creating the array and adding the error handling section. I hope it works lol
Sub FnLstRow()
Application.ScreenUpdating = False
Dim LR As Long
Dim Arr() As Variant
ThisWorkbook.Worksheets("Data").Select
LR = Cells(Rows.Count, "AO").End(xlUp).Row
Cells(LR, 1).Offset(1, 0).EntireRow.Select
Range(Selection, Selection.End(xlDown)).Select
On Error GoTo Handler
Arr = Selection
On Error GoTo 0
Selection.Copy
With Sheets("LatestData")
.Cells(.Rows.Count, "A").End(xlUp).Offset(1, 0).PasteSpecial Paste:=xlPasteValues
End With
Application.CutCopyMode = False
Range("A1").Select
Application.ScreenUpdating = True
Exit Sub
Handler:
MsgBox "Nothing to paste!"
End Sub

Runtime Error 1004 VBA

I have some VBA code that gives me "Runtime Error 1004: PasteSpecial method of Range class failed."
This is my code:
Sub CurrentToOld()
rownumber = ThisWorkbook.Sheets("As-Built Emergency").Range("A1", Worksheets("As-Built Emergency").Range("A2").End(xlDown)).Rows.Count
ThisWorkbook.Sheets("As-Built Emergency").Activate
ThisWorkbook.ActiveSheet.Rows("2:" & rownumber).Select
Selection.Cut
ThisWorkbook.Sheets("OLD AB EMERGENCY").Activate
ThisWorkbook.ActiveSheet.Range("A2").Select
ThisWorkbook.ActiveSheet.Range("A2").PasteSpecial Paste:=xlPasteSpecial, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
End Sub
The error is in the 2nd to last line, and I can't seem to figure out why. I'm copying around 150 rows, and there are no merged cells within any of the copied ones.
I've tried removing the .Select statements and I change what I copied from Rows("2:" & rownumber) to Range("A1:D4") [something small which exists] but I still get the error.
EDIT: I put this line:
ThisWorkbook.ActiveSheet.Range("A2").Select
Because I wanted to test if it was a problem with the way I wrote the PasteSpecial code or if I actually needed to declare anything.
But the statement above works when I tested it in the debugger, so I'm at a standstill here.
Try just activating that range.
ThisWorkbook.ActiveSheet.Range("A2").Activate
or try declaring the worksheet
Sub CurrentToOld()
Dim wb As Workbook
Set WorkBk = ActiveWorkbook
rownumber = WorkBk.Sheets("As-Built Emergency").Range("A1", Worksheets("As-Built Emergency").Range("A2").End(xlDown)).Rows.Count
WorkBk.Sheets("As-Built Emergency").Activate
WorkBk.ActiveSheet.Rows("2:" & rownumber).Select
Selection.Cut
WorkBk.Sheets("OLD AB EMERGENCY").Activate
WorkBk.ActiveSheet.Range("A2").Select
WorkBk.ActiveSheet.Range("A2").PasteSpecial Paste:=xlPasteSpecial, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
End Sub
Or declare the sheet as well
Sub CurrentToOld()
Dim wb As Workbook
Set WorkBk = ActiveWorkbook
Dim ws As Excel.Worksheet
Set ws = Application.ActiveSheet
rownumber = WorkBk.Sheets("As-Built Emergency").Range("A1", Worksheets("As-Built Emergency").Range("A2").End(xlDown)).Rows.Count
WorkBk.Sheets("As-Built Emergency").Activate
ws.Rows("2:" & rownumber).Select
Selection.Cut
WorkBk.Sheets("OLD AB EMERGENCY").Activate
ws.Range("A2").Select
ws.Range("A2").PasteSpecial Paste:=xlPasteSpecial, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
End Sub
Sub CurrentToOld()
Dim rownumber As Long
rownumber = ThisWorkbook.Sheets("As-Built Emergency").Range("A1", Worksheets("As-Built Emergency").Range("A2").End(xlDown).Address).Rows.Count
ThisWorkbook.Sheets("As-Built Emergency").Activate
ThisWorkbook.ActiveSheet.Rows("2:" & rownumber).Select
Selection.Cut
ThisWorkbook.Sheets("OLD AB EMERGENCY").Select
ThisWorkbook.Sheets("OLD AB EMERGENCY").Range("A1").Select
ActiveSheet.Paste
End Sub
Apparently the problem was that the sheet I was posting to had data, and I needed to clear out the used cells first.

Looping through all active Worksheets

I have a really weak experience working with VBA, but now faced an issue where it is really required.
I need to copy cell's value from multiple worksheets (besides "Summary") into one worksheet, but facing a problem. When running a macro, I get around 30 lines with the values I need, but all 30 values belong to the same worksheet. Seems like the loop is running only around 1 worksheet. Could you help me finding the mistake in the code?
Sub CopyTotalSalesPrice()
For Each Worksheet In ActiveWorkbook.Worksheets
If Worksheet.Name <> "Summary" Then
Worksheet.Cells(Rows.Count, 7).End(xlUp).Select
End If
If Selection.Value > "0" Then
Selection.Copy
Worksheets("Summary").Cells(Rows.Count, 6).End(xlUp).Offset(2, 0).PasteSpecial (xlPasteValues)
Range("D4").Select
Selection.Copy
Worksheets("Summary").Cells(Rows.Count, 4).End(xlUp).Offset(2, 0).PasteSpecial (xlPasteValues)
End If
Next Worksheet
Worksheets("Summary").Select
End Sub
when using Cells(Rows.Count, 7).End(xlUp).Select and everything else, they refer to the current sheet. So you either put in front of them Worksheet.Cells(Rows.Count, 7).End(xlUp).Select or you activate the sheet first with Worksheet.Activate
or you can just do as follow:
Sub CopyTotalSalesPrice()
For Each Worksheet In ActiveWorkbook.Worksheets
With Worksheet
If .Name <> "Summary" Then
.Cells(Rows.Count, 7).End(xlUp).Copy Destination:=Worksheets("Summary").Cells(Rows.Count, 6).End(xlUp).Offset(2, 0)
.Range("D4").Copy Destination:=Worksheets("Summary").Cells(Rows.Count, 4).End(xlUp).Offset(2, 0)
End If
End With
Next Worksheet
Worksheets("Summary").Select
End Sub
Try this:
Sub CopyTotalSalesPrice()
For Each Worksheet In ActiveWorkbook.Worksheets
If Worksheet.Name <> "Summary" Then
Worksheet.Select
Worksheet.Cells(Worksheet.Rows.Count, 7).End(xlUp).Select
End If
If Selection.Value > "0" Then
Selection.Copy
Worksheets("Summary").Cells(Worksheet.Rows.Count, 6).End(xlUp).Offset(2, 0).PasteSpecial (xlPasteValues)
Range("D4").Select
Selection.Copy
Worksheets("Summary").Cells(Worksheet.Rows.Count, 4).End(xlUp).Offset(2, 0).PasteSpecial (xlPasteValues)
End If
Next Worksheet
Worksheets("Summary").Select
End Sub
I replaced this Cells(Rows.Count, 7).End(xlUp).Select with Worksheet.Cells(Worksheet.Rows.Count, 7).End(xlUp).Select

ActiveSheet.Paste Failing VBA Excel

My VBA code is set to copy a value from one sheet (NB this value will change each time the sheet is open) and paste into a 'database' on the next available row.
Think I've got it right but the Paste method seems to fail, can anyone see why?
Windows("Invoice Program.xlsm").Activate
Range("B4").Select
Application.CutCopyMode = False
Selection.Copy
Workbooks.Open ("C:\Users\Invoice Database.xlsx")
Windows("Invoice Database.xlsx").Activate
Range("A" & Rows.Count).End(xlUp).Offset(1).Select
Selection.Paste
Dim varTemp as Variant
Windows("Invoice Program.xlsm").Activate
Range("B4").Select
varTemp = ActiveCell.Value
Workbooks.Open ("C:\Users\Invoice Database.xlsx")
Windows("Invoice Database.xlsx").Activate
Range("A" & Rows.Count).End(xlUp).Offset(1).Select
ActiveCell = varTemp
Use ActiveSheet.Paste but in the line before use DoEvents.
DoEvents
ActiveSheet.Paste

Resources