Hello Everybody,
My VBA skills aren't really good. As for now I've arguing with my laptop for 2 days and I am still not able to get it to work. So hopefully you can help me.
let me explain what I am trying to do.
I want te make a selection (by hand all files are in the same folder) and copy cells B2:Z2 from every worksheet in the selected workbooks to a new file where all these "B2:Z2" ranges have to be on one single worksheet. Below each other.
This is what i came up with in de last two days
I really hope you guys can help me.
My VBA skills aren't really good. As for now I've arguing with my laptop for 2 days and I am still not able to get it to work. So hopefully you can help me.
let me explain what I am trying to do.
I want te make a selection (by hand all files are in the same folder) and copy cells B2:Z2 from every worksheet in the selected workbooks to a new file where all these "B2:Z2" ranges have to be on one single worksheet. Below each other.
This is what i came up with in de last two days
Code:
Sub CombineWorkbooks() Dim xlWkbk As String
Dim xlWkshName As String
Dim xlWksh As Object
Dim xlWkshM As Object
Dim FilesToOpen
Dim x As Integer
Dim xn As Long
Dim n As Integer
Dim i As Integer
Dim sourceRange As Range
Dim destrange As Range
Dim a As Long
Dim basebook As Workbook
Set basebook = ThisWorkbook
On Error GoTo ErrHandler
Application.ScreenUpdating = False
FilesToOpen = Application.GetOpenFilename _
(MultiSelect:=True, Title:="Files to Merge")
If TypeName(FilesToOpen) = "Boolean" Then
MsgBox "No Files were selected"
GoTo ExitHandler
End If
xn = 1
x = 1
While x <= UBound(FilesToOpen)
Workbooks.Open Filename:=FilesToOpen(x)
xlWkbk = ActiveWorkbook.Name
n = 1
For n = 1 To ActiveWorkbook.Worksheets.Count
Set sourceRange = ActiveWorkbook.Worksheets(n).Range("b2:z2")
a = sourceRange.Rows.Count
With sourceRange
Set destrange = basebook.Worksheets(1).Cells(xn, 1). _
Resize(.Rows.Count, .Columns.Count)
End With
destrange.Value = sourceRange.Value
xn = xn + a
Next n
Workbooks(xlWkbk).Close False
x = x + 1
Wend
ExitHandler:
Application.ScreenUpdating = True
Exit Sub
ErrHandler:
MsgBox Err.Description
Resume ExitHandler
End Sub
I really hope you guys can help me.