+ Reply to Thread
Results 1 to 5 of 5

Macro not working while running multiple others macro..

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    01-16-2014
    Location
    Canada
    MS-Off Ver
    Excel 2007
    Posts
    440

    Macro not working while running multiple others macro..

    Hi,

    I am actually using 1 macro to open a folder using a command button and run multiple macro on the file. The macro that run on the file does split worksheet, and split each worksheet into separate excel file. But when I try to do it, it doesnt work.

    This is the open macro:
    Sub Split()
       Dim MyFolder As String 'Path collected from the folder picker dialog
       Dim MyFile As String 'Filename obtained by DIR function
       Dim wbk As Workbook 'Used to loop through each workbook
    
    On Error Resume Next
    
    Application.ScreenUpdating = False
    'Opens the folder picker dialog to allow user selection
    With Application.FileDialog(msoFileDialogFolderPicker)
    
    .Title = "Please select a folder"
    .Show
    .AllowMultiSelect = False
    
       If .SelectedItems.Count = 0 Then 'If no folder is selected, abort
    MsgBox "You did not select a folder"
          Exit Sub
    End If
        MyFolder = .SelectedItems(1) & Application.PathSeparator   'Assign selected folder to MyFolder
    End With
    MyFile = Dir(MyFolder) 'DIR gets the first file of the folder
    
       'Opens the file and assigns to the wbk variable for future use
       Set wbk = Workbooks.Open(Filename:=MyFolder & MyFile)
       'Replace the line below with the statements you would want your macro to perform
    
    Call SplitFiles
    
    Application.ScreenUpdating = True
    
    End Sub
    and the part that is not working is:
    'Sub excelsplit()
    Dim wbk1 As Workbook
    Dim l_str, l_end, l_row As Long
                                            
    Set wbk1 = ThisWorkbook
                                            
    'to remove unwanted worksheets on the workbook
    Application.DisplayAlerts = False
        Do Until wbk1.Sheets.Count = 1
            wbk1.Sheets(wbk1.Sheets.Count).Delete
        Loop
    Application.DisplayAlerts = True
                                            
    'to read the data from first sheet
    l_str = 2
    l_row = 2
    Do While l_row <= wbk1.Sheets(1).Range("A65536").End(xlUp).Row + 1
        If wbk1.Sheets(1).Range("A" & l_row).Value = "" And _
            wbk1.Sheets(1).Range("B" & l_row).Value = "" And _
                wbk1.Sheets(1).Range("C" & l_row).Value = "" Then
           wbk1.Sheets.Add after:=wbk1.Sheets(wbk1.Sheets.Count)
           wbk1.Sheets(wbk1.Sheets.Count).Range("A2:Z" & l_row - l_str + 1).Value = wbk1.Sheets(1).Range("A" & l_str & ":Z" & l_row).Value
           l_str = l_row + 1
        End If
        l_row = l_row + 1
    Loop
                                            
    'End Sub
                                            
    'Sub DeleteNoData
    For Each sht In Sheets
        If Not sht.UsedRange.Find("NO DATA", , , 1) Is Nothing Then
            Application.DisplayAlerts = False
                sht.Delete
            Application.DisplayAlerts = True
        End If
    Next
    'End Sub
                                            
    'Sub RenameTabs()
        Dim l As Long
        For l = 1 To Sheets.Count
            With Worksheets(l)
                If .Range("B8").Value <> "" And _
                   .Range("B9").Value <> "" And _
                   .Range("B10").Value <> "" Then
                        .Name = "DMO_" & Right(.Range("B10").Value, 5)
                End If
            End With
        Next l
    'End Sub
                                            
    'Sub Splitbook()
    'Split separate workbook into separate spreadsheet.
    MyPath = ThisWorkbook.Path
    For Each shtg In ThisWorkbook.Sheets
    shtg.Copy
    ActiveSheet.Cells.Copy
    ActiveSheet.Cells.PasteSpecial Paste:=xlPasteValues
    ActiveSheet.Cells.PasteSpecial Paste:=xlPasteFormats
    ActiveWorkbook.SaveAs _
    Filename:=MyPath & "\" & shtg.Name & ".xls"
    ActiveWorkbook.Close savechanges:=False
    Next shtg
    'End Sub
    Any help will be thankful!

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Macro not working while running multiple others macro..

    The SplitFiles sub is setup to work on the workbook the code is in, ThisWorkbook, not the workbook that's been opened, wbk.

    Try passing wbk to SplitFiles as an argument.

    That might look something like this, though I can't be sure as you've not posted the header for SplitFiles.
    Option Explicit
    
    Sub SplitFiles(wbk1 As Workbook)
    'Sub excelsplit()
    Dim sht As Worksheet
    Dim MyPath As String
    Dim l_str As Long, l_end As Long, l_row As Long
    
        'to remove unwanted worksheets on the workbook
        Application.DisplayAlerts = False
        Do Until wbk1.Sheets.Count = 1
            wbk1.Sheets(wbk1.Sheets.Count).Delete
        Loop
        Application.DisplayAlerts = True
    
        'to read the data from first sheet
        l_str = 2
        l_row = 2
        Do While l_row <= wbk1.Sheets(1).Range("A65536").End(xlUp).Row + 1
            If wbk1.Sheets(1).Range("A" & l_row).Value = "" And _
               wbk1.Sheets(1).Range("B" & l_row).Value = "" And _
               wbk1.Sheets(1).Range("C" & l_row).Value = "" Then
                wbk1.Sheets.Add After:=wbk1.Sheets(wbk1.Sheets.Count)
                wbk1.Sheets(wbk1.Sheets.Count).Range("A2:Z" & l_row - l_str + 1).Value = wbk1.Sheets(1).Range("A" & l_str & ":Z" & l_row).Value
                l_str = l_row + 1
            End If
            l_row = l_row + 1
        Loop
    
        'End Sub
    
        'Sub DeleteNoData
        For Each sht In wbk1.Sheets
            If Not sht.UsedRange.Find("NO DATA", , , 1) Is Nothing Then
                Application.DisplayAlerts = False
                sht.Delete
                Application.DisplayAlerts = True
            End If
        Next
        'End Sub
    
        'Sub RenameTabs()
        Dim l As Long
        For l = 1 To wbk1.Sheets.Count
            With wbk1.Worksheets(l)
                If .Range("B8").Value <> "" And _
                   .Range("B9").Value <> "" And _
                   .Range("B10").Value <> "" Then
                    .Name = "DMO_" & Right(.Range("B10").Value, 5)
                End If
            End With
        Next l
        'End Sub
    
        'Sub Splitbook()
        'Split separate workbook into separate spreadsheet.
        MyPath = wbk1.Path
    
        For Each sht In wbk1.Sheets
            sht.Copy
            ActiveSheet.Cells.Copy
            ActiveSheet.Cells.PasteSpecial Paste:=xlPasteValues
            ActiveSheet.Cells.PasteSpecial Paste:=xlPasteFormats
            ActiveWorkbook.SaveAs _
                    Filename:=MyPath & "\" & sht.Name & ".xls"
            ActiveWorkbook.Close savechanges:=False
        Next sht
        'End Sub
        
    End Sub
    You would now call SplitFiles from Split like this.
    Call SplitFiles(wbk)
    If posting code please use code tags, see here.

  3. #3
    Forum Contributor
    Join Date
    01-16-2014
    Location
    Canada
    MS-Off Ver
    Excel 2007
    Posts
    440

    Re: Macro not working while running multiple others macro..

    Quote Originally Posted by Norie View Post
    The SplitFiles sub is setup to work on the workbook the code is in, ThisWorkbook, not the workbook that's been opened, wbk.

    Try passing wbk to SplitFiles as an argument.

    That might look something like this, though I can't be sure as you've not posted the header for SplitFiles.
    Option Explicit
    
    Sub SplitFiles(wbk1 As Workbook)
    'Sub excelsplit()
    Dim sht As Worksheet
    Dim MyPath As String
    Dim l_str As Long, l_end As Long, l_row As Long
    
        'to remove unwanted worksheets on the workbook
        Application.DisplayAlerts = False
        Do Until wbk1.Sheets.Count = 1
            wbk1.Sheets(wbk1.Sheets.Count).Delete
        Loop
        Application.DisplayAlerts = True
    
        'to read the data from first sheet
        l_str = 2
        l_row = 2
        Do While l_row <= wbk1.Sheets(1).Range("A65536").End(xlUp).Row + 1
            If wbk1.Sheets(1).Range("A" & l_row).Value = "" And _
               wbk1.Sheets(1).Range("B" & l_row).Value = "" And _
               wbk1.Sheets(1).Range("C" & l_row).Value = "" Then
                wbk1.Sheets.Add After:=wbk1.Sheets(wbk1.Sheets.Count)
                wbk1.Sheets(wbk1.Sheets.Count).Range("A2:Z" & l_row - l_str + 1).Value = wbk1.Sheets(1).Range("A" & l_str & ":Z" & l_row).Value
                l_str = l_row + 1
            End If
            l_row = l_row + 1
        Loop
    
        'End Sub
    
        'Sub DeleteNoData
        For Each sht In wbk1.Sheets
            If Not sht.UsedRange.Find("NO DATA", , , 1) Is Nothing Then
                Application.DisplayAlerts = False
                sht.Delete
                Application.DisplayAlerts = True
            End If
        Next
        'End Sub
    
        'Sub RenameTabs()
        Dim l As Long
        For l = 1 To wbk1.Sheets.Count
            With wbk1.Worksheets(l)
                If .Range("B8").Value <> "" And _
                   .Range("B9").Value <> "" And _
                   .Range("B10").Value <> "" Then
                    .Name = "DMO_" & Right(.Range("B10").Value, 5)
                End If
            End With
        Next l
        'End Sub
    
        'Sub Splitbook()
        'Split separate workbook into separate spreadsheet.
        MyPath = wbk1.Path
    
        For Each sht In wbk1.Sheets
            sht.Copy
            ActiveSheet.Cells.Copy
            ActiveSheet.Cells.PasteSpecial Paste:=xlPasteValues
            ActiveSheet.Cells.PasteSpecial Paste:=xlPasteFormats
            ActiveWorkbook.SaveAs _
                    Filename:=MyPath & "\" & sht.Name & ".xls"
            ActiveWorkbook.Close savechanges:=False
        Next sht
        'End Sub
        
    End Sub
    You would now call SplitFiles from Split like this.
    Call SplitFiles(wbk)
    Sorry, I did not totally paste the whole format macro, these doesn't work after I change the sub to wbk1:
        Columns("A:A").Select
        Selection.TextToColumns Destination:=Range("A1"), DataType:=xlDelimited, _
            TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
            Semicolon:=False, Comma:=True, Space:=False, Other:=False, FieldInfo _
            :=Array(Array(1, 1), Array(2, 1)), TrailingMinusNumbers:=True
                                            
    'Sub DeleteRows()
                                            
       Set SrchRngFr = ActiveSheet.UsedRange
       Do
           Set a = SrchRngFr.Find("Type:", LookIn:=xlValues)
           If Not a Is Nothing Then a.EntireRow.Delete
       Loop While Not a Is Nothing
    'End Sub
                                            
    'Sub DeleteRows()
                                            
       Set SrchRngEn = ActiveSheet.UsedRange
       Do
           Set b = SrchRngEn.Find("Plan:", LookIn:=xlValues)
           If Not b Is Nothing Then b.EntireRow.Delete
       Loop While Not b Is Nothing
    'End Sub

  4. #4
    Forum Contributor
    Join Date
    06-22-2011
    Location
    somerset
    MS-Off Ver
    365
    Posts
    328

    Re: Macro not working while running multiple others macro..

    you can replace all the sheet copy, paste and delete stuf with just.
    For Each sht In wbk1.Sheets
        sht.Move 'this will creat a new workbook from that sheet and delete the original
        ActiveWorkbook.SaveAs Filename:=MyPath & "\" & sht.Name & ".xls"
        ActiveWorkbook.Close savechanges:=False
    Next Sht
    Sub Reputation()
    Dim Problem as Variant
    Dim Reputation as Integer
    For Each Problem in Forum.Threads
        If Problem.Title = "*[Solved]*" and Solver.Name = "Leon V (AW)" Then Reputation = Reputation + 1
    Next Problem
    End Sub

  5. #5
    Forum Contributor
    Join Date
    01-16-2014
    Location
    Canada
    MS-Off Ver
    Excel 2007
    Posts
    440

    Re: Macro not working while running multiple others macro..

    I have tried to add this but it doesn't work..:
         
        Columns("A:A").Select
        Selection.TextToColumns Destination:=Range("A1"), DataType:=xlDelimited, _
            TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
            Semicolon:=False, Comma:=True, Space:=False, Other:=False, FieldInfo _
            :=Array(Array(1, 1), Array(2, 1)), TrailingMinusNumbers:=True
                                       
    'Sub DeleteRows()
                                            
       Set SrchRngEn = wbk1.Sheets.UsedRange
       Do
           Set b = SrchRngEn.Find("Type:", LookIn:=xlValues)
           If Not b Is Nothing Then b.EntireRow.Delete
       Loop While Not b Is Nothing
    'End Sub
                                            
    'Sub InsertRowEng()
    'Insert row above a field
        Dim i As Long
        Dim c As Range
                                            
        Do While i < wbk1.Sheets.UsedRange.Rows.Count
            i = i + 1
            Set c = Cells(i, 1)
                                            
            If c.Value Like "*Services*" Then
                Rows(c.Row & ":" & c.Row).EntireRow.Insert Shift:=xlDown
                i = i + 1
            End If
            Debug.Print i
        Loop
                                            
    'End Sub

+ 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. Need some help on keeping UNDO/REDO working after running macro's. Almost there ;-)
    By onidarbe in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-12-2013, 02:00 PM
  2. Macro for running the SQL multiple times
    By sawoodalam1989 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-27-2013, 07:00 AM
  3. [SOLVED] Allow users to edit ranges not working after running a Macro?!
    By Margate in forum Excel General
    Replies: 2
    Last Post: 02-15-2013, 07:42 AM
  4. Running Macro on multiple sheets
    By VKS in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 01-21-2013, 12:10 PM
  5. running macro on time not working
    By SJDANIELS in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-31-2008, 10:33 AM

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