Hello everyone,
I am completely new to VBA, but the process I now need to perform is forcing me into learning about it.
Currently I'm working on a summary workbook that needs to pull data from multiple different workbooks. All of the workbooks I need to pull from are formatted the exact same, and I have figured out how to copy the data I need. The problem is I need the file names too, but the closest I've gotten is either the filename of the summary workbook, or just false.
This is what I have so far:
Sub Import_Candidate()
Dim vFile As Variant
Dim wbCopyTo As Workbook
Dim wsCopyTo As Worksheet
Dim wbCopyFrom As Workbook
Dim wsCopyFrom As Worksheet
Dim FullFileName As String
Set wbCopyTo = ActiveWorkbook
Set wsCopyTo = ActiveSheet
'-------------------------------------------------------------
'Open file with data to be copied
vFile = Application.GetOpenFilename("Excel Files (*.xl*)," & _
"*.xl*", 1, "Select Excel File", "Open", False)
'If Cancel then Exit
If TypeName(vFile) = "Boolean" Then
Exit Sub
Else
Set wbCopyFrom = Workbooks.Open(vFile)
Set wsCopyFrom = wbCopyFrom.Worksheets(5)
End If
'--------------------------------------------------------------
'Copy Range
wsCopyFrom.Range("B6:D6").Copy
wsCopyTo.Range("F7:H7").PasteSpecial Paste:=xlPasteValues, _
Operation:=xlNone, SkipBlanks:=False, Transpose:=False
wsCopyFrom.Range("B12:D12").Copy
wsCopyTo.Range("I7:K7").PasteSpecial Paste:=xlPasteValues, _
Operation:=xlNone, SkipBlanks:=False, Transpose:=False
wsCopyFrom.Range("B20:D20").Copy
wsCopyTo.Range("L7:N7").PasteSpecial Paste:=xlPasteValues, _
Operation:=xlNone, SkipBlanks:=False, Transpose:=False
wsCopyFrom.Range("B28:D28").Copy
wsCopyTo.Range("O7:Q7").PasteSpecial Paste:=xlPasteValues, _
Operation:=xlNone, SkipBlanks:=False, Transpose:=False
wsCopyFrom.Range("B37:D37").Copy
wsCopyTo.Range("R7:T7").PasteSpecial Paste:=xlPasteValues, _
Operation:=xlNone, SkipBlanks:=False, Transpose:=False
wsCopyFrom.Range("B46:D46").Copy
wsCopyTo.Range("U7:W7").PasteSpecial Paste:=xlPasteValues, _
Operation:=xlNone, SkipBlanks:=False, Transpose:=False
'Pull Data Workbook File Name
FileName = ThisWorkbook.FullName
Range("X7").Value = FileName
'Close file that was opened
wbCopyFrom.Close SaveChanges:=False
'---------------------------------------------------------------
End Sub
Thanks for all your help!
I am completely new to VBA, but the process I now need to perform is forcing me into learning about it.
Currently I'm working on a summary workbook that needs to pull data from multiple different workbooks. All of the workbooks I need to pull from are formatted the exact same, and I have figured out how to copy the data I need. The problem is I need the file names too, but the closest I've gotten is either the filename of the summary workbook, or just false.
This is what I have so far:
Sub Import_Candidate()
Dim vFile As Variant
Dim wbCopyTo As Workbook
Dim wsCopyTo As Worksheet
Dim wbCopyFrom As Workbook
Dim wsCopyFrom As Worksheet
Dim FullFileName As String
Set wbCopyTo = ActiveWorkbook
Set wsCopyTo = ActiveSheet
'-------------------------------------------------------------
'Open file with data to be copied
vFile = Application.GetOpenFilename("Excel Files (*.xl*)," & _
"*.xl*", 1, "Select Excel File", "Open", False)
'If Cancel then Exit
If TypeName(vFile) = "Boolean" Then
Exit Sub
Else
Set wbCopyFrom = Workbooks.Open(vFile)
Set wsCopyFrom = wbCopyFrom.Worksheets(5)
End If
'--------------------------------------------------------------
'Copy Range
wsCopyFrom.Range("B6:D6").Copy
wsCopyTo.Range("F7:H7").PasteSpecial Paste:=xlPasteValues, _
Operation:=xlNone, SkipBlanks:=False, Transpose:=False
wsCopyFrom.Range("B12:D12").Copy
wsCopyTo.Range("I7:K7").PasteSpecial Paste:=xlPasteValues, _
Operation:=xlNone, SkipBlanks:=False, Transpose:=False
wsCopyFrom.Range("B20:D20").Copy
wsCopyTo.Range("L7:N7").PasteSpecial Paste:=xlPasteValues, _
Operation:=xlNone, SkipBlanks:=False, Transpose:=False
wsCopyFrom.Range("B28:D28").Copy
wsCopyTo.Range("O7:Q7").PasteSpecial Paste:=xlPasteValues, _
Operation:=xlNone, SkipBlanks:=False, Transpose:=False
wsCopyFrom.Range("B37:D37").Copy
wsCopyTo.Range("R7:T7").PasteSpecial Paste:=xlPasteValues, _
Operation:=xlNone, SkipBlanks:=False, Transpose:=False
wsCopyFrom.Range("B46:D46").Copy
wsCopyTo.Range("U7:W7").PasteSpecial Paste:=xlPasteValues, _
Operation:=xlNone, SkipBlanks:=False, Transpose:=False
'Pull Data Workbook File Name
FileName = ThisWorkbook.FullName
Range("X7").Value = FileName
'Close file that was opened
wbCopyFrom.Close SaveChanges:=False
'---------------------------------------------------------------
End Sub
Thanks for all your help!