Hi all, first post, here goes, hope someone can help urgently . . .
I have just started a new job, and have inherited some files that need updating that contain macros. Most I understand fine, and work no problem, but I have an issue with one pair of macros that should allow me to copy data from a worksheet ion one workbook, and capture the file name of that workbook, and then paste the data into another open workbook, with the filename (of the file where the data has come from) being pasted into the next available column cell at the end of each row of data pasted in.
The problem is that although the macro works to capture the data and the file name, and will allow you to paste it into a different worksheet on the same file, as soon as I switch to the other workbook and run the past macro, it pastes the data fine, but the file name is not added alongside each row. Instead it puts blank cells there. I really need to find a way of adding the file name from the one file to the other file when pasting in the data. Please help asap !!
Here are the two macro scripts :-
So it's this last bit of macro, the 'add toolkit name to returns, that doesn't work when you switch files.
Please, please, can anyone come up with a solution to resolve this? I've tried numerous things so far, and have searched the web for an appropriate post, but haven't been able to find anything even close. So I'd really appreciate your help. I need to get this up and running really urgently, so please, if you can, please help me with your solutions.
Many thanks
Glyn
I have just started a new job, and have inherited some files that need updating that contain macros. Most I understand fine, and work no problem, but I have an issue with one pair of macros that should allow me to copy data from a worksheet ion one workbook, and capture the file name of that workbook, and then paste the data into another open workbook, with the filename (of the file where the data has come from) being pasted into the next available column cell at the end of each row of data pasted in.
The problem is that although the macro works to capture the data and the file name, and will allow you to paste it into a different worksheet on the same file, as soon as I switch to the other workbook and run the past macro, it pastes the data fine, but the file name is not added alongside each row. Instead it puts blank cells there. I really need to find a way of adding the file name from the one file to the other file when pasting in the data. Please help asap !!
Here are the two macro scripts :-
Code:
Sub copyForReturn()
'unfilter to be safe
On Error GoTo 7
Worksheets("Toolkit").ShowAllData
7:
'copy data from toolkit output tab to return tab
Range("completeOutputRange").Copy
'get the file name for pasting
fileName = ActiveWorkbook.Name
End Sub
Sub pasteReturn()
'put copied returns data into the bottom of the returns tab
'fetch data last row
LastRow = Sheets("Returns").Cells(Sheets("Returns").Rows.Count, "A").End(xlUp).Row + 1
'paste the data in
Range("A" & LastRow).Select
'if error (ie no selction copied) tank out of the sub
On Error GoTo 5
'paste clipboard
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
'fetch data last row new
LastRow2 = Sheets("Returns").Cells(Sheets("Returns").Rows.Count, "A").End(xlUp).Row
'add toolkit name to returns
For i = LastRow To LastRow2
Range("AE" & i) = fileName
Next I
5:
End Sub
Please, please, can anyone come up with a solution to resolve this? I've tried numerous things so far, and have searched the web for an appropriate post, but haven't been able to find anything even close. So I'd really appreciate your help. I need to get this up and running really urgently, so please, if you can, please help me with your solutions.
Many thanks
Glyn
Last edited by a moderator: