Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

merge multiple sheets for one file into one file and sheet

0

hi  experts

I  search  way   to  merge  all data  of  sheets  from  file  into  one  file  and  one  sheet   but  the  code  doesn't  work   and  doesn't  give  me  the  error    the  source  data  in  source  file  it  begins  from A1 : N   

Sub CopySheets()
    Application.ScreenUpdating = False
    Dim flder As FileDialog, FileName As String, FileChosen As Integer, wkbDest As Workbook, wkbSource As Workbook, wsDest As Worksheet, WS As Worksheet
    Set wkbDest = ThisWorkbook
    Set wsDest = wkbDest.Sheets("Summary")
    Set flder = Application.FileDialog(msoFileDialogFilePicker)
    flder.Title = "Please Select a folder and file."
    FileChosen = flder.Show
    FileName = flder.SelectedItems(1)
    Set wkbSource = Workbooks.Open(FileName)
    With wkbSource
        For Each WS In .Sheets
            If Not IsError(Evaluate("=ISREF('[" & wkbDest.Name & "]" & WS.Name & "'!$A$1)")) Then
                WS.UsedRange.Cells.Copy
                With wsDest.Cells(wsDest.Rows.Count, "A").End(xlUp).Offset(2)
                    .PasteSpecial xlPasteValues
                    .PasteSpecial xlPasteFormats
                End With
            End If
        Next WS
    End With
    wkbSource.Close False
    Application.ScreenUpdating = True
End Sub

any  idea  what's  wrong  with  this  code

Answer
Discuss

Answers

0
Selected Answer

Hello Hasson,

What you want to achieve, unfortunately, isn't entirely clear but here is a start.

Sub CopySheets()
    ' 282

    Dim FileName    As String
    Dim wsDest      As Worksheet
    Dim Ws          As Worksheet
    Dim Rng         As Range                    ' range to be copied

    With Application.FileDialog(msoFileDialogFilePicker)
        .Title = "Please select a file"
        .AllowMultiSelect = False
        If Not .Show Then Exit Sub

        FileName = .SelectedItems(1)
    End With

    Set wsDest = ThisWorkbook.Worksheets("Summary")
    Application.ScreenUpdating = False

    With Workbooks.Open(FileName)
        For Each Ws In .Worksheets
            ' not clear what you want to check here
            If Not IsEmpty(Ws.Cells(1, 1)) Then
                ' define the range to be copied here
                Set Rng = Ws.Cells(1, 1).CurrentRegion
                Rng.Offset(1).Copy
                With wsDest.Cells(wsDest.Rows.Count, "A").End(xlUp).Offset(1)
                    .PasteSpecial xlPasteValues
                    .PasteSpecial xlPasteFormats
                End With
            End If
        Next Ws
        .Close False
    End With
    Application.ScreenUpdating = True
End Sub

My code largely follows your design but some lines were moved to their proper place in the logical working sequence. You had declared many more variables than are needed. I couldn't see that in your code because they were all bunched up. Perhaps that's the reason you couldn't see it, either.

I didn't understand what you wanted to check with the ISREF function. Therefore my code just checks if A1 is blank. This will be easy to modify if needed.

You said, you want to copy Range(A1:N ). That is insufficient information. In VBA you would start from A1 and look for the last used cell in column A, then go across to column N in that row. Or you would determine the last used row in column N. The difference is important if the column length isn't the same. But if they are of the same length then CurrentRegion will probably do fine. The UsedRange, that you used, can capture columns of different lengths but it will sometimes include blank cells the user deleted but that wouldn't necessarily be a problem because End(xlUp) would overwrite them. In the above code I prepared the Rng variable. You can set it the way best suited to your needs.

Discuss

Discussion


thanks .   this  is  not  what I  want .  when  merge  the  data  in all sheets   your code  splits  data into multiple tables individualy when  run     the macro from  multiple  sheets  what  I  want  the  all  of  data  shold  copy  to  the  bottom consecutively and  it  should  not  repeat  the  topics headers  . I  put  the  result  after  run  yourcode  in sheet1   and  what  I  want  in  sheet  desired result  please  see my  file
Hasson (rep: 30) Jul 8, '21 at 4:48 am
Hasson,
I'm afraid you didn't do a good job here. The code only needed two really very tiny changes, and these changes were necessary at the exact spot where I told you in my answer that you should expect to need modifications. Now I have made the changes for you and marked them bold in my answer.
BTW, the destination sheet must have the column headers in it before import is started.
Variatus (rep: 4889) Jul 8, '21 at 8:40 pm
thanks for your cooperation
Hasson (rep: 30) Jul 9, '21 at 12:44 pm
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login