+ Reply to Thread
Results 1 to 4 of 4

Macro that combines serveral workbooks stops on .xlsm files

  1. #1
    Registered User
    Join Date
    12-04-2017
    Location
    London
    MS-Off Ver
    Office 365
    Posts
    16

    Macro that combines serveral workbooks stops on .xlsm files

    Hi All,

    I am new to the community so be gentle with me if I blunder in with bad terminology etc.

    I am really hoping someone can help me with something, google wasn't my freind for this one so I'm placing my faith in you all

    The Problem
    I have a dashboard file that opens all the files in a folder, takes a specific range from each file and pastes that range into my Dashboard file in the next column available.

    The problem is that while this works perfectly for xlsx files, it stops when opening xlsm files.

    The macro doesn't crash, it just stops on the open .xlsm file and goes no further.

    If I remove the xlsm files the macro runs to the end without an issue.

    I'd be so very grateful if someone can tell me why this is happening because I can't figure it out at all.

    Thanks so much for your help.

    The code

    Sub Basic_Example_3()
    Dim MyPath As String, FilesInPath As String
    Dim MyFiles() As String
    Dim SourceCcount As Long, Fnum As Long
    Dim mybook As Workbook, BaseWks As Worksheet
    Dim sourceRange As Range, destrange As Range
    Dim Cnum As Long, CalcMode As Long

    'Fill in the path\folder where the files are
    MyPath = "C:\Status\Status Worksheets\"

    'Add a slash at the end if the user forget it
    If Right(MyPath, 1) <> "\" Then
    MyPath = MyPath & "\"
    End If

    'If there are no Excel files in the folder exit the sub
    FilesInPath = dir(MyPath & "*.xls*")
    If FilesInPath = "" Then
    MsgBox "No files found"
    Exit Sub
    End If

    'Fill the array(myFiles)with the list of Excel files in the folder
    Fnum = 0
    Do While FilesInPath <> ""
    Fnum = Fnum + 1
    ReDim Preserve MyFiles(1 To Fnum)
    MyFiles(Fnum) = FilesInPath
    FilesInPath = dir()
    Loop

    'Change ScreenUpdating, Calculation and EnableEvents
    With Application
    CalcMode = .Calculation
    .Calculation = xlCalculationManual
    .ScreenUpdating = False
    .EnableEvents = True
    End With

    'Add a new workbook with one sheet
    Set BaseWks = ThisWorkbook.Worksheets(1)
    Cnum = 5

    'Loop through all files in the array(myFiles)
    If Fnum > 0 Then
    For Fnum = LBound(MyFiles) To UBound(MyFiles)
    Set mybook = Nothing
    On Error Resume Next
    Set mybook = Workbooks.Open(MyPath & MyFiles(Fnum))
    On Error GoTo 0

    If Not mybook Is Nothing Then

    On Error Resume Next
    Set sourceRange = mybook.Worksheets(1).Range("E1:E71")

    If Err.Number > 0 Then
    Err.Clear
    Set sourceRange = Nothing
    Else
    'if SourceRange use all rows then skip this file
    If sourceRange.Rows.Count >= BaseWks.Rows.Count Then
    Set sourceRange = Nothing
    End If
    End If
    On Error GoTo 0

    If Not sourceRange Is Nothing Then

    SourceCcount = sourceRange.Columns.Count

    If Cnum + SourceCcount >= BaseWks.Columns.Count Then
    MsgBox "Sorry there are not enough columns in the sheet"
    BaseWks.Columns.AutoFit
    mybook.Close savechanges:=False
    GoTo ExitTheSub
    Else

    'Copy the file name in the first row
    With sourceRange
    BaseWks.Cells(1, Cnum). _
    Resize(, .Columns.Count).Value = MyFiles(Fnum)
    End With

    'Set the destrange
    Set destrange = BaseWks.Cells(2, Cnum)

    'we copy the values from the sourceRange to the destrange
    With sourceRange
    Set destrange = destrange. _
    Resize(.Rows.Count, .Columns.Count)
    End With
    destrange.Value = sourceRange.Value

    Cnum = Cnum + SourceCcount
    End If
    End If
    mybook.Close savechanges:=False
    End If

    Next Fnum
    BaseWks.Columns.AutoFit
    End If

    ExitTheSub:
    'Restore ScreenUpdating, Calculation and EnableEvents
    With Application
    .ScreenUpdating = True
    .EnableEvents = True
    .Calculation = CalcMode
    End With

    End Sub

  2. #2
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,482

    Re: Macro that combines serveral workbooks stops on .xlsm files

    You will have to step through the code and see what is happening as it loops through the files. When you are in the VBA editor and have the cursor inside the macro, hit F8 to step through the code. Once it hits the .xlsm file see what is different than all the other files.

  3. #3
    Forum Expert
    Join Date
    10-06-2017
    Location
    drevni ruchadlo
    MS-Off Ver
    old
    Posts
    2,151

    Re: Macro that combines serveral workbooks stops on .xlsm files

    Maybe instead of:
    Please Login or Register  to view this content.
    try this code:
    Please Login or Register  to view this content.
    or:
    Please Login or Register  to view this content.
    a segment earlier/before (with the variable declaration of course).

    Check what will happen ... the same as before ?
    Last edited by mjr veverka; 12-04-2017 at 04:11 PM.

  4. #4
    Registered User
    Join Date
    12-04-2017
    Location
    London
    MS-Off Ver
    Office 365
    Posts
    16

    Re: Macro that combines serveral workbooks stops on .xlsm files

    I love you Perucha, you solved an issue that had me so stumped and frustrated. Thank you so much you code worked perfectly.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Macro that combines data from several workbooks in a folder.
    By taylorsm in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-18-2017, 12:26 PM
  2. Creating a macro that combines data from several CSV files into a single datasheet
    By Chimney343 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 04-13-2017, 11:39 AM
  3. 'Reading' data from csv files in .xlsm Workbooks
    By hammer2 in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 08-09-2015, 10:56 PM
  4. Split .xlsm workbook into multiple xlsm workbooks w/ 1 tab each
    By JenBR in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 01-23-2014, 07:05 PM
  5. Replies: 2
    Last Post: 10-04-2013, 10:12 AM
  6. Recording Macros over serveral workbooks
    By Tracyann in forum Excel General
    Replies: 0
    Last Post: 12-08-2011, 08:42 AM
  7. Need defined parameters on macro that combines workbooks
    By juniperjacobs in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 05-20-2011, 04:23 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1