VBA: copy rows of data from multiple WBs to Master

bubblystace

New Member
Joined
Dec 5, 2017
Messages
8
Hi All,

The below macro is currently grabbing a single column of data from selected WBs and pasting it into a master sheet. I want it changed so it grabs all rows from row 3 onwards. How would I modify it to do this and include .xls files? thanks!

Code:
Sub Main()
Dim wbMe As Workbook
Dim wbBatch As Workbook
Dim wsMe As Worksheet
Dim wsBatch As Worksheet
Dim arstrFiles() As String


Application.ScreenUpdating = False


i = 0
Set wbMe = ThisWorkbook
Set wsMe = wbMe.Sheets("Master")


'Select the batch of Files
With Application.FileDialog(msoFileDialogFilePicker)
    .AllowMultiSelect = True
    .Filters.Add "Spreadsheets", "*.xlsx", 1
    If .Show = -1 Then
        For Each vrtSelectedItem In .SelectedItems
            i = i + 1
            ReDim Preserve arstrFiles(i)
            arstrFiles(i - 1) = vrtSelectedItem
        Next vrtSelectedItem
    End If
End With


'get last row in this sheet
intFirstMTRow = ThisWorkbook.Sheets("Master").Range("A1048500").End(xlUp).Row + 1


For j = 1 To UBound(arstrFiles) Step 1
    'open file in batch and extract data
    Set wbBatch = Workbooks.Open(arstrFiles(j - 1))
    Set wsBatch = wbBatch.Sheets("Defects")
    intColumn = 1
    'Modify to copy all rows from 3 onwards
[COLOR=#ff0000]    For k = 4 To 300[/COLOR]
[COLOR=#ff0000]        If wsBatch.Range("a" & k).Value <> "" Then[/COLOR]
[COLOR=#ff0000]            wsMe.Cells(intFirstMTRow, intColumn) = wsBatch.Range("A" & k).Value[/COLOR]
[COLOR=#ff0000]            intColumn = intColumn + 1[/COLOR]
[COLOR=#ff0000]        End If[/COLOR]
[COLOR=#ff0000]    Next k[/COLOR]
[COLOR=#ff0000]    intFirstMTRow = intFirstMTRow + 1[/COLOR]
[COLOR=#ff0000]    wbBatch.Close savechanges:=False[/COLOR]

Next j




Application.ScreenUpdating = True


End Sub
 
Last edited:

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.

Forum statistics

Threads
1,214,782
Messages
6,121,532
Members
449,037
Latest member
tmmotairi

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