shollenbmk
New Member
- Joined
- Feb 17, 2016
- Messages
- 5
Hi all, first post of mine to this forum.
I've been working with this bit of VBA for a few hours now, but haven't been able to figure it out. Needless to say, I'm not very good in VBA. I am trying to copy several rows from one sheet to another, making sure that the pasted content starts on the first empty row in the destination.
System: Excel 2010 on Windows 7
Source: Current workbook. (I know I should reference a specific filename, but the name of this file changes all the time. The macro exists inside the source workbook, so I am assume that running it from there will allow for the use of currentworkbook.)
Destination: "Example_of_TimeKeeper2016.xlsx"
What happens: The macro successfully copies the correct fields, as well as successfully opening the destination file. Unfortunately, it pastes the data in source formatting, instead of just pasting text. Also, it pastes it wherever it wants, rather than on the first blank row.
Last, I always get an error saying "Run-time error '1004': Application-defined or object-defined error"
My intuition tells me that something is wrong with Range(c.Address).Select in the "paste" portion of the code, but I can't figure it out. Can anyone help?
Sub CopyRows()
secondWorkbook = "Example_of_TimeKeeper2016.xlsx"
lastColumn = "k"
currentWorkbook = ThisWorkbook.Name
Workbooks.Open ThisWorkbook.Path & "\" & secondWorkbook
Windows(currentWorkbook).Activate
With Worksheets(1).Columns("A:A")
Set c = .Find("", LookIn:=xlValues)
If Not c Is Nothing Then
secondAddress = Replace(c.Address, "$A$", "")
Range("A2:" & lastColumn & CStr(CInt(secondAddress))).Select
Selection.Copy
End If
End With
' -------- Activate the Second Workbook-----------
Windows(secondWorkbook).Activate
With Worksheets(1).Columns("A:K")
Set c = .Find("", LookIn:=xlValues)
If Not c Is Nothing Then
'------- Select and paste the data from First Workbook--------------
Range(c.Address).Select
ActiveSheet.Paste
End If
End With
End Sub
I've been working with this bit of VBA for a few hours now, but haven't been able to figure it out. Needless to say, I'm not very good in VBA. I am trying to copy several rows from one sheet to another, making sure that the pasted content starts on the first empty row in the destination.
System: Excel 2010 on Windows 7
Source: Current workbook. (I know I should reference a specific filename, but the name of this file changes all the time. The macro exists inside the source workbook, so I am assume that running it from there will allow for the use of currentworkbook.)
Destination: "Example_of_TimeKeeper2016.xlsx"
What happens: The macro successfully copies the correct fields, as well as successfully opening the destination file. Unfortunately, it pastes the data in source formatting, instead of just pasting text. Also, it pastes it wherever it wants, rather than on the first blank row.

My intuition tells me that something is wrong with Range(c.Address).Select in the "paste" portion of the code, but I can't figure it out. Can anyone help?
Sub CopyRows()
secondWorkbook = "Example_of_TimeKeeper2016.xlsx"
lastColumn = "k"
currentWorkbook = ThisWorkbook.Name
Workbooks.Open ThisWorkbook.Path & "\" & secondWorkbook
Windows(currentWorkbook).Activate
With Worksheets(1).Columns("A:A")
Set c = .Find("", LookIn:=xlValues)
If Not c Is Nothing Then
secondAddress = Replace(c.Address, "$A$", "")
Range("A2:" & lastColumn & CStr(CInt(secondAddress))).Select
Selection.Copy
End If
End With
' -------- Activate the Second Workbook-----------
Windows(secondWorkbook).Activate
With Worksheets(1).Columns("A:K")
Set c = .Find("", LookIn:=xlValues)
If Not c Is Nothing Then
'------- Select and paste the data from First Workbook--------------
Range(c.Address).Select
ActiveSheet.Paste
End If
End With
End Sub