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

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

Related

What is VBA code error 1004 and how to solve it?

It is showing error 1004 on ** --- ** line in this code. PLease help
Sub test()
Workbooks("OI ANALYSIS.xlsm").Activate
Sheet3.Select
Range("A16:C16").Copy
Sheet4.Select
**Range("B2").End(xlDown).Offset(1, 0).Select**
Selection.PasteSpecial Paste:=xlPasteValues
Application.CutCopyMode = False
Application.OnTime Now + TimeValue("00:00:01"), "test"
End Sub
Try this-
Sub test()
Workbooks("OI ANALYSIS.xlsm").Activate
Worksheets("Sheet3").Select
Range("A16:C16").Copy
Worksheets("Sheet4").Select
Range("B2").End(xlDown).Offset(1, 0).Select
Selection.PasteSpecial Paste:=xlPasteValues
Application.CutCopyMode = False
Application.OnTime Now + TimeValue("00:00:01"), "test"
End Sub
The Sheet3.Select syntax isn't correct in VBA. Run-time error 1004 is common when you don't specify the range correctly or when an invalid name is used.

Why am i getting this error with copy/paste?

I am getting an error
Runtime error 1004: Application defined or Object defined error
in my vba code. Could you please help me correct it?
Sub INPUT_DATA()
' INPUT_DATA Macro
' Application.ScreenUpdating = False
Application.EnableEvents = False
Sheets("Input").Select
If Range("D55").Value = 0 Then
Range("B2:AI52").Select
Selection.Copy
Sheets("Database").Select
ActiveSheet.Range("A2").End(xlDown).Offset(1, 0).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End If
Sheets("Input").Select
MsgBox ("File has been updated. DO NOT PRESS UPDATE again, as it will enter the same data once again")
End Sub
You didn't say which line is causing the error, but it looks like it's likely this line...
ActiveSheet.Range("A2").End(xlDown).Offset(1, 0).Select
It starts at A2, and then it goes down until it finds the last used row. However, if there aren't any used rows below A2, it finds the very last row in Column A. Then it tries to offset to one row below it, which doesn't exist, and hence the error. Instead, you can find the next available row as follows...
ActiveSheet.Range("A" & Rows.Count).End(xlUp).Offset(1, 0).Select
Although, there's no need to do all of that selecting. It's very inefficient. So your macro can be re-written as follows...
Option Explicit
Sub INPUT_DATA()
' INPUT_DATA Macro
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
Dim sourceWorksheet As Worksheet
Set sourceWorksheet = Worksheets("Input")
Dim destinationWorksheet As Worksheet
Set destinationWorksheet = Worksheets("Database")
With sourceWorksheet
If .Range("D55").Value = 0 Then
.Range("B2:AI52").Copy
With destinationWorksheet
.Cells(.Rows.Count, "A").End(xlUp).Offset(1, 0).PasteSpecial Paste:=xlPasteValues
End With
End If
End With
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
MsgBox ("File has been updated. DO NOT PRESS UPDATE again, as it will enter the same data once again")
End Sub

Error 9 "out of range" when copying Excel range

I'm trying to copy data from one workbook to another after a button click but the function range doesn't work and always returns
error 9 "out of range".
Sub Button1_Click()
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Workbooks.Open "C:\Users\Username\Desktop\Allineamento\Data\MasterData.xlsx"
Workbooks("MasterData").Sheets(2).range(Cells(13, 2), Cells(800, 16)).Copy
Workbooks("ImportSheets").Sheets("Master Data").Cells(2, 1).PasteSpecial Paste.Value
Workbooks("MasterData").Close
Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Sub
With basic Excel functions, the Macro-recorder is really useful. This is the code, generated by copying and pasting some excel range:
Sub Macro1()
Range("A1:E8").Select
Selection.Copy
Selection.PasteSpecial xlPasteValues
ActiveSheet.Paste
Application.CutCopyMode = False
Range("L5").Select
Application.WindowState = xlNormal
Range("B7").Select
End Sub
Although, the code is not efficient, it is quite useful to see the needed code for the copy-paste - Range.PasteSpecial xlPasteValues
Thus, in the question code, replace PasteSpecial Paste.Value with PasteSpecial xlPasteValues.

Issue with a run-time error 1004: PasteSpecial method of Range Class failed

I am trying to run a paste value macro in a different sheet in a workbook that previously had a password protection.
The Macro works fine in the destination sheet for the values but not in the other tabs where i get this error runtime error '1004' - select method of range class failed
How should i proceed to allow this macro to run from a different active sheet?
Sub PasteSpecial_ValuesOnly()
Worksheets("ARF Table").Range("A2:AI13").Copy
'Error occurs below
**Worksheets("ARF Export").Range("A2:AI13").Select**
Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
Worksheets("ARF Export").Range("AK2").Value = Worksheets("ARF Export").Range("AD2").Value
End Sub
You only require the top left cell of the destination.
You don't need to .Select and direct value transfer is faster.
Sub PasteSpecial_ValuesOnly()
Worksheets("ARF Table").Range("A2:AI13").Copy
Worksheets("ARF Export").Range("A2").PasteSpecial Paste:=xlValues
Worksheets("ARF Export").Range("AK2").Value = Worksheets("ARF Export").Range("AD2").Value
End Sub
Alternate,
Sub PasteSpecial_ValuesOnly()
with Worksheets("ARF Table").Range("A2:AI13")
Worksheets("ARF Export").Range("A2").resize(.rows.count, .columns.count) = .value
end with
Worksheets("ARF Export").Range("AK2").Value = Worksheets("ARF Export").Range("AD2").Value
End Sub

Range, empty row in Excel vba

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

Resources