Can't run multiple macros but alone they work

elementqka

New Member
Joined
Apr 10, 2022
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Hi,

I have a first macro that compile data using relative and absolute reference recording.

My second macro consolidate data

My third macro clean the table after the consolidation.

I can run them fine alone but one after the other one its not working.

Call doesnt work. Application wait doesnt really work ...


First macro :

Sub XX()
Application.ScreenUpdating = False
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Worksheets
ws.Activate

If InStr(1, ws.Name, "XXXX") > 0 And Range("C10").Value = "XXXX" Then


If Range("J17").Value <> "" Then

Sheets(ws.Name).Select


Range("C25").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Tableau enrobé").Select
Range("M4").Select
Selection.End(xlDown).Select
ActiveCell.Offset(1, 0).Range("A1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("M4").Select

End If

GoTo ReStart:

End If

ReStart:

Next ws


End Sub


Second macro :

Sub merge()

Dim lastRow As Long, iCntr As Long, rngDel As Range, dict As Object, yo As Range, iCntr2 As Long

Application.ScreenUpdating = False

lastRow = Range("B" & Rows.Count).End(xlUp).Row

Set dict = CreateObject("Scripting.Dictionary")

For iCntr = 6 To lastRow

If Cells(iCntr, 2) <> "" Then

If Not dict.Exists(Cells(iCntr, 2).Value) Then

dict.Add Cells(iCntr, 2).Value, iCntr

Else

Range("C" & dict(Cells(iCntr, 2).Value) & ":M" & dict(Cells(iCntr, 2).Value)).Value = _
Range("C" & iCntr & ":M" & iCntr).Value

If rngDel Is Nothing Then


Set rngDel = Cells(iCntr, 2)
Set yo = Cells(iCntr, 2)

Else

Set rngDel = Union(rngDel, Cells(iCntr, 2))


End If


End If

End If


Next
End Sub

Third macro :


Sub clean()

Dim lastRow As Long
Dim myRow As Long

Dim lastRow2 As Long
Dim myRow2 As Long


Application.ScreenUpdating = False


lastRow = Cells(150, "B").End(xlUp).Row
lastRow2 = Cells(150, "AA").End(xlUp).Row


For myRow = lastRow To 6 Step -1


If Cells(myRow, "B") = Cells(myRow - 1, "B") Then


For myRow2 = lastRow2 To 6 Step -1

If Cells(myRow2, "AA") = " " Then


Rows(myRow2).Delete

End If

Next myRow2

End If



Next myRow



End Sub



Thank you !
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
i think if the last worksheet had a matching J17 then your activesheet is "Tableau enrobé"), otherwise the last worksheet.
so be sure that you're also work in the desired worksheet, which is that tableau enrobé.
Activate and select are commands to avoid, they slow down your colde and are unnecessary.

It's better to work with variables, like
set shT=Sheets("Tableau enrobé")
or
with Sheets("Tableau enrobé")
....
end with
 
Upvote 0
Your code uses many unqualified range and cell references, which means that if the active sheet ( ActiveSheet) is not as you expect, your code will go wrong. This is almost certainly what is going wrong when you try to run all macros one after the other. The quickest way to fix that is to put a line of code

VBA Code:
    'Define the active worksheet
    Worksheets("Tableau enrobé").Select '<--- add code to select a worksheet
at the top of the second and third macros to select the worksheet those macros should run on. In my example, I chose Worksheets("Tableau enrobé"), but that's only a guess. You must choose the correct worksheet.

VBA Code:
'Second macro:
Sub merge()
    Dim lastRow As Long, iCntr As Long, rngDel As Range, dict As Object, yo As Range, iCntr2 As Long
    Application.ScreenUpdating = False
      
    'Define the active worksheet
    Worksheets("Tableau enrobé").Select '<--- add code to select a worksheet
    
    lastRow = Range("B" & Rows.Count).End(xlUp).Row
    Set dict = CreateObject("Scripting.Dictionary")
    For iCntr = 6 To lastRow
        If Cells(iCntr, 2) <> "" Then
            If Not dict.Exists(Cells(iCntr, 2).Value) Then
                dict.Add Cells(iCntr, 2).Value, iCntr
            Else
                Range("C" & dict(Cells(iCntr, 2).Value) & ":M" & dict(Cells(iCntr, 2).Value)).Value = _
                Range("C" & iCntr & ":M" & iCntr).Value
                If rngDel Is Nothing Then
                    Set rngDel = Cells(iCntr, 2)
                    Set yo = Cells(iCntr, 2)
                Else
                    Set rngDel = Union(rngDel, Cells(iCntr, 2))
                End If
            End If
        End If
    Next
End Sub
VBA Code:
'Third macro:
Sub clean()
    Dim lastRow As Long
    Dim myRow As Long
    Dim lastRow2 As Long
    Dim myRow2 As Long
    Application.ScreenUpdating = False
    
    'Define the active worksheet
    Worksheets("Tableau enrobé").Select '<--- add code to select a worksheet
        
    
    lastRow = Cells(150, "B").End(xlUp).Row
    lastRow2 = Cells(150, "AA").End(xlUp).Row
    For myRow = lastRow To 6 Step -1
        If Cells(myRow, "B") = Cells(myRow - 1, "B") Then
            For myRow2 = lastRow2 To 6 Step -1
                If Cells(myRow2, "AA") = " " Then
                    Rows(myRow2).Delete
                End If
            Next myRow2
        End If
    Next myRow
End Sub

(Tip: when posting code, please try to use 'code tags' to format the code as I have done above
 
Upvote 0
Solution

Forum statistics

Threads
1,223,893
Messages
6,175,239
Members
452,621
Latest member
Laura_PinksBTHFT

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top