copy data macro

flamme20

New Member
Joined
Apr 27, 2011
Messages
31
I've got the following macro:

Sub SearchString()
Dim LSearchRow As Integer
Dim LCopyToRow As Integer
Dim myPath As String
Dim myFileName As String
Dim wkbkSourceWasOpen As Boolean
Dim wkbkSource As Workbook

myPath = "C:\"
myFileName = "debit.xls"

Set wkbkSource = Nothing
On Error Resume Next
Set wkbkSource = Workbooks(myFileName)
On Error GoTo 0

wkbkSourceWasOpen = True
If wkbkSource Is Nothing Then
wkbkSourceWasOpen = False
On Error Resume Next
Set wkbkSource = Workbooks.Open(Filename:=myPath & myFileName, ReadOnly:=True)
On Error GoTo 0

If wkbkSource Is Nothing Then
MsgBox "Run for your life"
End If
End If

wkbkSource.Sheets("ddd").Select

'Start search in row 71
LSearchRow = 1

'Start copying data to row 5 in urea (row counter variable)
LCopyToRow = 1

While Len(Range("A" & CStr(LSearchRow)).Value) > 0

'If value in column E = "---", copy entire row to Sheet2
If Range("A" & CStr(LSearchRow)).Value = "ST" Then


'Select row in Sheet1 to copy
wkbkSource.Sheets("ddd").Select
Range("C" & CStr(LSearchRow)).Select
Selection.Copy

'Paste row into Sheet2 in next row
Sheets("Ëèñò2").Select
Range("B" & CStr(LCopyToRow)).Select
ActiveSheet.Paste


'Move counter to next row
LCopyToRow = LCopyToRow + 1

'Go back to Sheet1 to continue searching
Sheets("ddd").Select

End If

LSearchRow = LSearchRow + 1

Wend

'Position on cell A3
Application.CutCopyMode = False
Range("A1").Select

MsgBox "All matching data has been copied."

Exit Sub

Err_Execute:
MsgBox "An error occurred."

End Sub

and I need to copy data from a closed book to an active workbook sheet (where macro is written). It does copy data, but to a sheet in the same closed workbook, which it opens up. Could somebody give advice as to how change it to "ThisWorkbook.Sheet paste" function?
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
I've got the following macro:

Sub SearchString()
Dim LSearchRow As Integer
Dim LCopyToRow As Integer
Dim myPath As String
Dim myFileName As String
Dim wkbkSourceWasOpen As Boolean
Dim wkbkSource As Workbook

myPath = "C:\"
myFileName = "debit.xls"

Set wkbkSource = Nothing
On Error Resume Next
Set wkbkSource = Workbooks(myFileName)
On Error GoTo 0

wkbkSourceWasOpen = True
If wkbkSource Is Nothing Then
wkbkSourceWasOpen = False
On Error Resume Next
Set wkbkSource = Workbooks.Open(Filename:=myPath & myFileName, ReadOnly:=True)
On Error GoTo 0

If wkbkSource Is Nothing Then
MsgBox "Run for your life"
End If
End If

wkbkSource.Sheets("ddd").Select

'Start search in row 71
LSearchRow = 1

'Start copying data to row 5 in urea (row counter variable)
LCopyToRow = 1

While Len(Range("A" & CStr(LSearchRow)).Value) > 0

'If value in column E = "---", copy entire row to Sheet2
If Range("A" & CStr(LSearchRow)).Value = "ST" Then


'Select row in Sheet1 to copy
wkbkSource.Sheets("ddd").Select
Range("C" & CStr(LSearchRow)).Select
Selection.Copy

'Paste row into Sheet2 in next row
Sheets("Ëèñò2").Select
Range("B" & CStr(LCopyToRow)).Select
ActiveSheet.Paste


'Move counter to next row
LCopyToRow = LCopyToRow + 1

'Go back to Sheet1 to continue searching
Sheets("ddd").Select

End If

LSearchRow = LSearchRow + 1

Wend

'Position on cell A3
Application.CutCopyMode = False
Range("A1").Select

MsgBox "All matching data has been copied."

Exit Sub

Err_Execute:
MsgBox "An error occurred."

End Sub

and I need to copy data from a closed book to an active workbook sheet (where macro is written). It does copy data, but to a sheet in the same closed workbook, which it opens up. Could somebody give advice as to how change it to "ThisWorkbook.Sheet paste" function?

What's the name of the Closed workbook?

what's the name of the workbook with the macro?
 
Upvote 0
What's the name of the Closed workbook?

what's the name of the workbook with the macro?


The name of the closed workbook is "debit.xls" (sheet "ddd")
The name of the workbook with the macro is "bal.xls" (sheet "2011").

Sheets("2011").Select
Range("B" & CStr(LCopyToRow)).Select
ActiveSheet.Paste

(due to Russian characters it wasn't correctly displayed, so I changed it to "2011").
 
Upvote 0
The name of the closed workbook is "debit.xls" (sheet "ddd")
The name of the workbook with the macro is "bal.xls" (sheet "2011").

Sheets("2011").Select
Range("B" & CStr(LCopyToRow)).Select
ActiveSheet.Paste

(due to Russian characters it wasn't correctly displayed, so I changed it to "2011").


I think you need to change this:

'Paste row into Sheet2 in next row
Sheets("2011").Select
Range("B" & CStr(LCopyToRow)).Select
ActiveSheet.Paste

To this:

'Paste row into Sheet2 in next row
Workbooks("bal.xls").Sheets("2011").Select
Range("B" & CStr(LCopyToRow)).Select
ActiveSheet.Paste
 
Upvote 0
I think you need to change this:

'Paste row into Sheet2 in next row
Sheets("2011").Select
Range("B" & CStr(LCopyToRow)).Select
ActiveSheet.Paste

To this:

'Paste row into Sheet2 in next row
Workbooks("bal.xls").Sheets("2011").Select
Range("B" & CStr(LCopyToRow)).Select
ActiveSheet.Paste

Highlights "Workbooks("bal.xls").Sheets("2011").Select" and returns Select method of worksheet class failed (run-time error 1004)
 
Upvote 0
Highlights "Workbooks("bal.xls").Sheets("2011").Select" and returns Select method of worksheet class failed (run-time error 1004)

Maybe:

Workbooks("bal.xls").Activate
Sheets("2011").Range("B" & CStr(LCopyToRow)).Select
Activesheet.Paste
 
Upvote 0
Maybe:

Workbooks("bal.xls").Activate
Sheets("2011").Range("B" & CStr(LCopyToRow)).Select
Activesheet.Paste


Thank you very much for your cooperation! To avoid further mistakes and keep searching further I had to continue like this:

Workbooks("bal.xls").Activate
Sheets("2011").Range("B" & CStr(LCopyToRow)).Select
ActiveSheet.Paste

LCopyToRow = LCopyToRow + 1

Workbooks("debit.xls").Activate
Sheets("ddd").Select
 
Upvote 0
Thank you very much for your cooperation! To avoid further mistakes and keep searching further I had to continue like this:

Workbooks("bal.xls").Activate
Sheets("2011").Range("B" & CStr(LCopyToRow)).Select
ActiveSheet.Paste

LCopyToRow = LCopyToRow + 1

Workbooks("debit.xls").Activate
Sheets("ddd").Select


You're welcome. Glad it's working now.
 
Upvote 0

Forum statistics

Threads
1,224,590
Messages
6,179,753
Members
452,940
Latest member
rootytrip

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top