Commander Vimes
New Member
- Joined
- Aug 8, 2016
- Messages
- 11
Hello,
I need help with some VBA coding, my requirement is: I need to copy and paste ranges from 21 excel docs in a folder to a Master workbook within relevant named sheets. I have some code, but want to loop it so it runs through each of the docs and copys+pastes the relevant parts into the relevant sections of the MasterWorkbook
Example:
MasterWorkbook has the below worksheets:
>>INPUT N1
>>INPUT N2
>>INPUT D1
>>INPUT C1
Document 1 of 21 (as do the other 20) has the below sheets:
N1
N2
D1
C1
I have code (see below) to copy range in N1 to reference cell in >>INPUT N1 (Application.Goto Reference:="R601C2") and so on for the other 3. Now I want to loop the code so it repeats the process for the remaining docs and changes the references so that they are correct.
Each doc 1-21 should paste the info into a different section eg
Doc 1 Application.Goto Reference:="R1C2"
Doc 2 Application.Goto Reference:="R101C2"
Doc 3 Application.Goto Reference:="R201C2"
Etc
So how do I get the Code to loop and change the document it opens and the reference it pastes to in the Master Workbook?
So I want the code to
Open doc 1
Copy N1 range
Paste into Master Workbook >>INPUT N1 specified range
Do this for N2, D1,C1 aswell.
Close Doc 1
Repeat process for Doc 2 etc
My current CODE:
I need help with some VBA coding, my requirement is: I need to copy and paste ranges from 21 excel docs in a folder to a Master workbook within relevant named sheets. I have some code, but want to loop it so it runs through each of the docs and copys+pastes the relevant parts into the relevant sections of the MasterWorkbook
Example:
MasterWorkbook has the below worksheets:
>>INPUT N1
>>INPUT N2
>>INPUT D1
>>INPUT C1
Document 1 of 21 (as do the other 20) has the below sheets:
N1
N2
D1
C1
I have code (see below) to copy range in N1 to reference cell in >>INPUT N1 (Application.Goto Reference:="R601C2") and so on for the other 3. Now I want to loop the code so it repeats the process for the remaining docs and changes the references so that they are correct.
Each doc 1-21 should paste the info into a different section eg
Doc 1 Application.Goto Reference:="R1C2"
Doc 2 Application.Goto Reference:="R101C2"
Doc 3 Application.Goto Reference:="R201C2"
Etc
So how do I get the Code to loop and change the document it opens and the reference it pastes to in the Master Workbook?
So I want the code to
Open doc 1
Copy N1 range
Paste into Master Workbook >>INPUT N1 specified range
Do this for N2, D1,C1 aswell.
Close Doc 1
Repeat process for Doc 2 etc
My current CODE:
Code:
Sub CopyInputSheetData()
'
' CopyInputSheetData Macro
'
'
' Prevents screen refreshing.
Application.ScreenUpdating = False
'Timer Start
Dim i As Integer
Dim StartTime As Double
Dim SecondsElapsed As Double
StartTime = Timer
'*************************************************************************************************
'Need to change File location to the correct one files are stored in
Workbooks.Open Filename:= _
"file location for Document 1.xlsm"
'N1
'This is the copy section to copy N1 from Sheet opened above
Sheets("N1").Select
Range("A5").Select
Selection.CurrentRegion.Select
Selection.Copy
Windows("MasterWorkbook.xlsm"). _
Activate
Sheets(">>INPUT N1").Select
Application.Goto Reference:="R601C2"
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
'N2
Windows("Document 1.xlsm"). _
Activate
Sheets("N2").Select
Range("A5").Select
Selection.CurrentRegion.Select
Selection.Copy
Windows("MasterWorkbook.xlsm"). _
Activate
Sheets(">>INPUT N2").Select
Application.Goto Reference:="R601C2"
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
'D1
Windows("Document 1.xlsm"). _
Activate
Sheets("D1").Select
Range("A5").Select
Selection.CurrentRegion.Select
Selection.Copy
Windows("MasterWorkbook.xlsm"). _
Activate
Sheets(">>INPUT D1").Select
Application.Goto Reference:="R601C2"
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
'C1
Windows("Document 1.xlsm"). _
Activate
Sheets("C1").Select
Range("A5").Select
Selection.CurrentRegion.Select
Selection.Copy
Windows("MasterWorkbook.xlsm"). _
Activate
Sheets(">>INPUT C1").Select
Application.Goto Reference:="R601C2"
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Workbooks("Document 1.xlsm").Close
'*************************************************************************************************
' Enables screen refreshing.
Application.ScreenUpdating = True
SecondsElapsed = Round(Timer - StartTime, 2)
MsgBox " Input Sheet Data has now been imported, in " & SecondsElapsed & " seconds", vbInformation
End Sub