VBA to paste range from closed workbook to master workbook

CutterSoilMixing

New Member
Joined
Jun 8, 2019
Messages
17
Office Version
  1. 365
Platform
  1. 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!

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
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.

Forum statistics

Threads
1,214,893
Messages
6,122,118
Members
449,066
Latest member
Andyg666

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