+ Reply to Thread
Results 1 to 2 of 2

Help Identifying Coding Errors - Any Suggestions Greatly Appreciated

Hybrid View

  1. #1
    Registered User
    Join Date
    01-04-2017
    Location
    Wellington, NZ
    MS-Off Ver
    2013
    Posts
    8

    Help Identifying Coding Errors - Any Suggestions Greatly Appreciated

    Hi there, I am wondering if there are any experts that can have a look and advise what I am doing wrong with the codes below. I've adapted the codes from www.TheSpreadsheetGuru.com which basically opens up all the Excel spreadsheets in a given folder and copy/paste the information into the master spreadsheet.

    If I open my master spreadsheet and run the macro it actually works fine. However, if I clear the contents in the master spreadsheet first or run the macro more than once then Excel would just shut itself down - I can't see anything that's obviously wrong with the codes so would appreciate any help with this

    Thanks in advance

    Sub SI_Report()
    'PURPOSE: To copy strategic initiatives report into the master table
    'SOURCE: Codes here are modified based on codes obtained from www.TheSpreadsheetGuru.com
    
    Check = MsgBox("This will copy all the strategic initiatives from spreadsheets stored in a folder you will now choose, are you sure?", vbOKCancel)
    
        If Check = vbOK Then
    
            Dim wb As Workbook
            Dim myPath As String
            Dim myFile As String
            Dim myExtension As String
            Dim FldrPicker As FileDialog
            
            'Optimise Macro Speed
            Application.ScreenUpdating = False
            Application.EnableEvents = False
            Application.Calculation = xlCalculationManual
            Application.DisplayAlerts = False
            
            'Retrieve Target Folder Path From User
              Set FldrPicker = Application.FileDialog(msoFileDialogFolderPicker)
            
                With FldrPicker
                  .Title = "Select A Target Folder"
                  .AllowMultiSelect = False
                    If .Show <> -1 Then GoTo NextCode
                    myPath = .SelectedItems(1) & "\"
                End With
            
            'In Case of Cancel
    NextCode:
              myPath = myPath
              If myPath = "" Then GoTo ResetSettings
            
            'Target File Extension
              myExtension = "*.xls*"
            
            'Target Path with Ending Extention
              myFile = Dir(myPath & myExtension)
            
            ' Clear contents first
              Windows("Strategic Initiatives Master.xlsm").Activate
              Sheets("Strategic Initiatives").Select
              Range("A2:W201").Select
              Selection.ClearContents
            
            'Loop through each Excel file in folder
              Do While myFile <> ""
                'Set variable equal to opened workbook
                  Set wb = Workbooks.Open(Filename:=myPath & myFile, UpdateLinks:=0)
                
                'Ensure Workbook has opened before moving on to next line of code
                  DoEvents
                
                'Copy data
                  wb.Sheets("Strategic Initiatives").Select
                  Range("A2", Range("W2").End(xlDown)).Select
                  Selection.Copy
                
                'Paste data
                  Windows("Strategic Initiatives Master.xlsm").Activate
                  Sheets("Strategic Initiatives").Select
                  Range("A" & Rows.Count).End(xlUp).Offset(1).Select
                  Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
                  :=False, Transpose:=False
                
                'Close Workbook without Saving
                  wb.Close SaveChanges:=False
                  
                'Ensure Workbook has closed before moving on to next line of code
                  DoEvents
            
                'Get next file name
                  myFile = Dir
              Loop
               
              Sheets("Instruction").Select
               
    ResetSettings:
          'Reset Macro Optimisation Settings
            Application.EnableEvents = True
            Application.Calculation = xlCalculationAutomatic
            Application.ScreenUpdating = True
            Application.DisplayAlerts = True
    
            Else: Exit Sub
            End If
    
    End Sub


  2. #2
    Forum Expert
    Join Date
    04-01-2013
    Location
    East Auckland
    MS-Off Ver
    Excel 365
    Posts
    1,347

    Re: Help Identifying Coding Errors - Any Suggestions Greatly Appreciated

    To all other users - Note this is the same issue reposted as this thread

    http://www.excelforum.com/showthread...t=#post4553440

+ 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. Excel Spreadsheet Problem, any help GREATLY appreciated!!
    By blastmewar2 in forum Excel General
    Replies: 1
    Last Post: 09-03-2016, 10:09 AM
  2. Greatly Appreciated: Help with Scaling
    By kissfist in forum Excel Charting & Pivots
    Replies: 2
    Last Post: 09-13-2013, 06:22 AM
  3. Replies: 3
    Last Post: 12-31-2012, 12:03 PM
  4. Help with an error 91 issue - assistance greatly appreciated.
    By Bighitch in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-02-2012, 02:08 PM
  5. Help would be greatly appreciated
    By lostonexcel in forum Excel General
    Replies: 7
    Last Post: 08-30-2011, 03:01 AM
  6. Replies: 2
    Last Post: 10-15-2010, 09:20 AM
  7. [SOLVED] If then Help is greatly appreciated!
    By hansjhamm@yahoo.com in forum Excel General
    Replies: 11
    Last Post: 02-28-2006, 08:00 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