Results 1 to 5 of 5

Thread: Run Macro one after the other

  1. #1
    Member
    Join Date
    Jun 2012
    Posts
    80
    Rep Power
    13

    Run Macro one after the other

    Hi,

    I am trying a VBA, with multiple functions. When I run the macro, before one process is finished the second one start which does not give the desired result.

    I want it to pause till the previous Sub is complete and then run the next line of commands.

    Here is the code:

    Code:
    Sub UnmergeAllCells()
    ActiveSheet.Cells.UnMerge
    End Sub
    
    
    Sub UnWrapTextAllCells()
    Cells.WrapText = False
    End Sub
    
    
    Sub PasteSpecial_ValuesOnly()
    'Copy A Range of Data
      Worksheets("Sheet1").Range("S2").Copy
    'PasteSpecial Values Only
      Worksheets("Sheet2").Range("T2").PasteSpecial Paste:=xlPasteValues
    'Clear Clipboard (removes "marching ants" around your original data set)
      Application.CutCopyMode = False
    End Sub
    
    
    Sub RowAndColumnAutoFit()
    Worksheets("Sheet1").Columns("A:BF").AutoFit
    End Sub
    
    
    Sub deleteMultipleRows()
    Rows("3:6").Delete
    End Sub
    
    
    Sub DeleteColumns()
        Worksheets("Sheet1").Range("A:C,E:F,H:J,L:S,U:X,Z:AB,AD:AH,AJ:BF").EntireColumn.Delete
    End Sub

  2. #2
    Fuhrer, Vierte Reich DocAElstein's Avatar
    Join Date
    Aug 2014
    Posts
    9,446
    Rep Power
    10
    Hi msiyab,
    I do not really understand most of your post…



    Quote Originally Posted by msiyab View Post
    .... When I run the macro, ...
    What macro are you talking about??



    Quote Originally Posted by msiyab View Post
    ...When I run the macro, before one process is finished the second one start which does not give the desired result.
    I want it to pause till the previous Sub is complete and then run the next line of commands.....
    I don’t understand. That does not really make any sense. You are explaining very badly.



    I think you need to try to explain everything again.



    What is the problem in doing this?
    Code:
    Sub CallEmAll()
     Call UnmergeAllCells
     Call UnWrapTextAllCells
     Call PasteSpecial_ValuesOnly
     Call RowAndColumnAutoFit
     Call deleteMultipleRows
     Call DeleteColumns
    End Sub


    What is the problem in doing this?
    Code:
    Sub DoItAll()
    'Sub UnmergeAllCells()
     ActiveSheet.Cells.UnMerge
    'End Sub
    'Sub UnWrapTextAllCells()
     Cells.WrapText = False
    'End Sub
    'Sub PasteSpecial_ValuesOnly()
    'Copy A Range of Data
     Worksheets("Sheet1").Range("S2").Copy
    'PasteSpecial Values Only
     Worksheets("Sheet2").Range("T2").PasteSpecial Paste:=xlPasteValues
    'Clear Clipboard (removes "marching ants" around your original data set)
     Application.CutCopyMode = False
    'End Sub
    'Sub RowAndColumnAutoFit()
     Worksheets("Sheet1").Columns("A:BF").AutoFit
    'End Sub
    'Sub deleteMultipleRows()
     Rows("3:6").Delete
    'End Sub
    'Sub DeleteColumns()
     Worksheets("Sheet1").Range("A:C,E:F,H:J,L:S,U:X,Z:AB,AD:AH,AJ:BF").EntireColumn.Delete
    'End Sub
    End Sub







    Alan
    Last edited by DocAElstein; 10-22-2020 at 03:37 PM.
    ….If you are my competitor, I will try all I can to beat you. But if I do, I will not belittle you. I will Salute you, because without you, I am nothing.
    If you are my enemy, we will try to kick the fucking shit out of you…..
    Winston Churchill, 1939
    Save your Forum..._
    _...KILL A MODERATOR!!

  3. #3
    Member
    Join Date
    Jun 2012
    Posts
    80
    Rep Power
    13
    Quote Originally Posted by DocAElstein View Post



    What is the problem in doing this?
    Code:
    Sub DoItAll()
    'Sub UnmergeAllCells()
     ActiveSheet.Cells.UnMerge
    'End Sub
    'Sub UnWrapTextAllCells()
     Cells.WrapText = False
    'End Sub
    'Sub PasteSpecial_ValuesOnly()
    'Copy A Range of Data
     Worksheets("Sheet1").Range("S2").Copy
    'PasteSpecial Values Only
     Worksheets("Sheet2").Range("T2").PasteSpecial Paste:=xlPasteValues
    'Clear Clipboard (removes "marching ants" around your original data set)
     Application.CutCopyMode = False
    'End Sub
    'Sub RowAndColumnAutoFit()
     Worksheets("Sheet1").Columns("A:BF").AutoFit
    'End Sub
    'Sub deleteMultipleRows()
     Rows("3:6").Delete
    'End Sub
    'Sub DeleteColumns()
     Worksheets("Sheet1").Range("A:C,E:F,H:J,L:S,U:X,Z:AB,AD:AH,AJ:BF").EntireColumn.Delete
    'End Sub
    End Sub




    Alan
    This part of the code does not seem to work. After un-merging and un-wrapping text, I want the text in cell S2 to be cut/copy/pasted to T2, before the blank columns & rows are deleted.

    Code:
    'Sub PasteSpecial_ValuesOnly()
    'Copy A Range of Data
     Worksheets("Sheet1").Range("S2").Copy
    'PasteSpecial Values Only
     Worksheets("Sheet2").Range("T2").PasteSpecial Paste:=xlPasteValues
    'Clear Clipboard (removes "marching ants" around your original data set)
     Application.CutCopyMode = False
    'End Sub
    Last edited by DocAElstein; 10-22-2020 at 03:38 PM.

  4. #4
    Fuhrer, Vierte Reich DocAElstein's Avatar
    Join Date
    Aug 2014
    Posts
    9,446
    Rep Power
    10
    Hi
    I think that the problem that you are suggesting is not possible.

    The text in cell S2 is pasted to T2 before any deleting is done.
    Excel will never do those deleting commands until it has finished the copy and paste.
    It is impossible for Excel to do those deleting commands until it has finished the copy and paste.




    I expect you have a different problem.
    I expect the macro is not doing what you think it is doing.
    I expect the macro is working perfectly, but it is doing something different to what you want.




    I think you need to explain very carefully and very fully , exactly what it is that you are trying to do
    Important is : You must always say exactly what worksheet is having any actions to be taken.
    Last edited by DocAElstein; 10-22-2020 at 01:16 PM.
    ….If you are my competitor, I will try all I can to beat you. But if I do, I will not belittle you. I will Salute you, because without you, I am nothing.
    If you are my enemy, we will try to kick the fucking shit out of you…..
    Winston Churchill, 1939
    Save your Forum..._
    _...KILL A MODERATOR!!

  5. #5
    Junior Member
    Join Date
    Jan 2012
    Posts
    10
    Rep Power
    0
    Like DocAElstein suggested. Explain in detail.
    But also:
    In "Sub UnmergeAllCells()" you reference ActiveSheet
    In "Sub UnwrapTextAllCells()" you don't reference any sheet.
    In "Sub PasteSpecial_ValuesOnly()" you reference 2 sheets (Sheet1 and Sheet2)
    In "Sub RowAndColumnAutoFit()" you reference Sheet1
    In "Sub deleteMultipleRows()" you again don't reference any sheet
    In "Sub DeleteColumns()" you reference sheet1
    Always reference properly. That gives you an advantage that code can be run from any sheet and still work as it was designed to do.
    Your 6 little macros could be in one like below.
    Change Sheet references where required as we don't know which sheets you're talking about.
    Code:
    Sub msiyab()
    With Sheets("Sheet1")    '<---- Everything refers to Sheet1 except the first part of the "Sheet2" line. (Obvious I would say)
        .Cells.UnMerge    '<----- Do away with using merged cells. They give you guanranteed problems at one time or another
        .Cells.WrapText = False
        Sheets("Sheet2").Range("T2").Value = .Range("S2").Value
        .Columns("A:BF").AutoFit
        .Rows("3:6").Delete
        .Range("A:C,E:F,H:J,L:S,U:X,Z:AB,AD:AH,AJ:BF").EntireColumn.Delete
    End With
    End Sub
    Now you have half the amout of lines plus you have the advantage that you can run the macro from any sheet you like.
    And I don't think you need the "Reply With Quote" Just extra not needed clutter.

Similar Threads

  1. Replies: 6
    Last Post: 09-03-2019, 10:26 AM
  2. Macro to Auto Run When Excel Cell Data Changes
    By vwallace in forum Excel Help
    Replies: 5
    Last Post: 11-05-2014, 11:04 PM
  3. Selecting workbook to run macro
    By Rajesh Kr Joshi in forum Excel Help
    Replies: 2
    Last Post: 08-24-2012, 08:21 PM
  4. Click Run cycle
    By PcMax in forum Excel Help
    Replies: 6
    Last Post: 11-01-2011, 04:50 AM

Posting Permissions

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