CutterSoilMixing
New Member
- Joined
- Jun 8, 2019
- Messages
- 17
- Office Version
- 365
- Platform
- Windows
Hello everyone! I have VBA code that does the following:
- open file dialog to select folder
- copy values from individual cells from all files in folder
- paste values to specified cells in master workbook
I've used this VBA successfully for many different applications in which the copied data has to be pasted into a single row in the master workbook. Now I'm trying to copy a range (A2:N4) in the source files and paste that range into A2:N4 in the master workbook but the code pastes all values from the range in the source file into one single row in the master.
I've tried specifying every individual cell Range("A2, B2, C2...L4, M4,N4") as well as just the range Range("A2:N4").
Any help would be greatly appreciated!
Thanks!
- open file dialog to select folder
- copy values from individual cells from all files in folder
- paste values to specified cells in master workbook
I've used this VBA successfully for many different applications in which the copied data has to be pasted into a single row in the master workbook. Now I'm trying to copy a range (A2:N4) in the source files and paste that range into A2:N4 in the master workbook but the code pastes all values from the range in the source file into one single row in the master.
I've tried specifying every individual cell Range("A2, B2, C2...L4, M4,N4") as well as just the range Range("A2:N4").
Any help would be greatly appreciated!
Thanks!
VBA Code:
Public Sub subPullDataFromSelectCellsInMultipleWorkbooks()
Dim strFileName As String
Dim strFolder As String
Dim WbDestination As Workbook
Dim WsDestination As Worksheet
Dim WsSource As Worksheet
Dim rngSource As Range
Dim rng As Range
Dim intLoop As Integer
Dim lngNextRow As Long
Dim Wbsource As Workbook
Dim rngTarget As Range
Dim StartTime As Double
Dim MinutesElapsed As String
'Remember time when macro starts
StartTime = Timer
ActiveWorkbook.Save
Set WbDestination = ActiveWorkbook
Set WsDestination = WbDestination.Worksheets("Data")
With Application.FileDialog(msoFileDialogFolderPicker)
If .Show = -1 Then
strFolder = .SelectedItems(1)
End If
End With
If strFolder = "" Then
Exit Sub
End If
If Right(strFolder, 1) <> "\" Then
strFolder = strFolder & "\"
End If
strFileName = Dir(strFolder & "*.xls*")
Do While strFileName <> ""
Workbooks.Open strFolder & strFileName
Set Wbsource = ActiveWorkbook
Set WsSource = Wbsource.Sheets("summary")
' Source cells.
Set rngSource = WsSource.Range("B2,B3,B4")
' Used to indicate the columns to copy data to.
Set rngTarget = WsDestination.Range("B2,B3,B4")
intLoop = 0
' Loop through each of the source cells.
For Each rng In rngSource.Cells
intLoop = intLoop + 1
lngNextRow = WsDestination.Cells(Rows.Count, rngTarget.Cells(1, intLoop).Column).End(xlUp).Row + 1
If Len(Trim(rng.Value)) = 0 Then
WsDestination.Cells(lngNextRow, rngTarget.Cells(1, intLoop).Column).Value = "x"
Else
WsDestination.Cells(lngNextRow, rngTarget.Cells(1, intLoop).Column).Value = rng.Value
End If
Next rng
Wbsource.Close
strFileName = Dir
Loop
WbDestination.Save
'Determine how many seconds code took to run
MinutesElapsed = Format((Timer - StartTime) / 86400, "hh:mm:ss")
MsgBox "This code ran successfully in " & MinutesElapsed, vbInformation, "Confirmation"
End Sub