Hi, I wonder whether someone may be able to help me please.
Firstly, I'd like to apologise because my VB is not my strongest subject, so to some this may be a very basic error.
From a tutorial I've found, I've adapted the code (below) to allow the user to copy from multiple Excel files, combining them into one 'Master' spreadsheet.
The macro works fine when it is copying the data from one range, but I'd now like to add to this and copy data from another range within the same sheet. The two ranges are A:AJ and AL:AX.
In it's current form, the section of script below deals with the setting of the range, but I'm really unsure about how to adapt this to include the second range.
Could someone perhaps offer a little guidance please on how I may go about copying the data from two, rather than the one range please.
Any help would be gratefully received.
Many thanks and kind regards
Firstly, I'd like to apologise because my VB is not my strongest subject, so to some this may be a very basic error.
From a tutorial I've found, I've adapted the code (below) to allow the user to copy from multiple Excel files, combining them into one 'Master' spreadsheet.
Code:
Sub BigMerge()
Dim DestCell As Range
Dim DataColumn As Variant
Dim NumberOfColumns As Variant
Dim WB As Workbook
Dim DestWB As Workbook
Dim WS As Worksheet
Dim FileNames As Variant
Dim N As Long
Dim R As Range
Dim StartRow As Long
Dim Lastrow As Long
Dim RowNdx As Long
Dim r1, r2, myMultipleRange As Range
' Create a new workbook for the consolidated
' data.
'Set DestWB = Workbooks.Add
' OR use the ActiveWorkbook:
Set DestWB = ActiveWorkbook
' OR use an open workbook
' Set DestWB = Workbooks("Book1.xls")
' DestCell is the first cell where the consolidated
' data will be written.
Set DestCell = DestWB.Worksheets(1).Range("A" & Rows.Count).End(xlUp).Offset(1, 0)
With DestCell
'With .Borders(xlInsideVertical)
' .LineStyle = xlContinuous
' .Weight = xlHairline
' .ColorIndex = xlAutomatic
' End With
With .Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.Weight = xlHairline
.ColorIndex = xlAutomatic
End With
With .Borders(xlEdgeTop)
.LineStyle = xlContinuous
.Weight = xlHairline
.ColorIndex = xlAutomatic
End With
With .Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.Weight = xlHairline
.ColorIndex = xlAutomatic
End With
With .Borders(xlEdgeRight)
.LineStyle = xlContinuous
.Weight = xlHairline
.ColorIndex = xlAutomatic
End With
End With
' DataColumn is the column on the worksheets to be
' consolidated where the actual data is. Data will
' be copied from this column.
DataColumn = "A"
' NumberOfColumns is the number of columns on each
' worksheet to be consolidated from which data will
' be copied. E.g., if your data is in range A1:J100,
' NumberOfColumns would be 10.
NumberOfColumns = 36
' StartRow is the row on the worksheets to be consolidated
' where the data starts. If your worksheet have heading/summary
' rows at the top, set this value to the row number where
' the actual data starts.
StartRow = 5
' Get the workbooks to consolidate
FileNames = Application.GetOpenFilename( _
filefilter:="Excel Files (*.xls*),*.xls*", _
Title:="Select the workbooks to merge.", MultiSelect:=True)
If IsArray(FileNames) = False Then
If FileNames = False Then
' User cancelled open dialog. get out.
Exit Sub
End If
End If
' Loop through all the selected files.
For N = LBound(FileNames) To UBound(FileNames)
' Open the workbook
Set WB = Workbooks.Open(Filename:=FileNames(N), ReadOnly:=True)
' Loop through all the worksheets in the workbook
For Each WS In WB.Worksheets
With WS
' Test if worksheet has content. It must have
' at least two cells with content. Otherwise,
' it is assumed to be empty and will not be
' processed.
If WS.UsedRange.Cells.Count > 1 Then
' Get the last row in DataColumn
' that has data.
Lastrow = .Cells(.Rows.Count, DataColumn). _
End(xlUp).Row
' Loop through the rows, statring at StartRow
' and going down to LastRow.
For RowNdx = StartRow To Lastrow
' Copy the cells on row RowNdx
' starting in DataColumn for NumberOfColumns'
' columns wide. Data is copied to
' DestCell.
.Cells(RowNdx, DataColumn). _
Resize(1, NumberOfColumns).Copy _
Destination:=DestCell
' Move the DestCell down one row.
Set DestCell = DestCell(2, 1)
Next RowNdx
End If
End With
Next WS
' close the workbook.
WB.Close savechanges:=False
Next N
End Sub
In it's current form, the section of script below deals with the setting of the range, but I'm really unsure about how to adapt this to include the second range.
Code:
' DataColumn is the column on the worksheets to be
' consolidated where the actual data is. Data will
' be copied from this column.
DataColumn = "A"
' NumberOfColumns is the number of columns on each
' worksheet to be consolidated from which data will
' be copied. E.g., if your data is in range A1:J100,
' NumberOfColumns would be 10.
NumberOfColumns = 36
' StartRow is the row on the worksheets to be consolidated
' where the data starts. If your worksheet have heading/summary
' rows at the top, set this value to the row number where
' the actual data starts.
StartRow = 5
Could someone perhaps offer a little guidance please on how I may go about copying the data from two, rather than the one range please.
Any help would be gratefully received.
Many thanks and kind regards