Macro is skipping steps.

Macro_Nerd99

Board Regular
Joined
Nov 13, 2021
Messages
61
Office Version
  1. 365
When I put a breakpoint and run through this macro manually everything works fine, but when I run it with a button it skips this step in the for-loop:
ThisWorkbook.Sheets(strTabName).Range("J" & thisrow).Interior.Color = Range("I" & thisrow).DisplayFormat.Interior.Color
This step is important in order for the SumByColor formula to work.
How can I fix this?

VBA Code:
Public Sub Total()
    Application.ScreenUpdating = False
    Dim lr As Long
    Dim lr2 As Long
    Dim lr3 As Long
    lr = ThisWorkbook.Worksheets(strTabName).Cells(Rows.Count, "C").End(xlUp).Row
    Dim thisrow As Integer
 
 
    ThisWorkbook.Worksheets("Control2").Visible = True
 
    ThisWorkbook.Worksheets("Control2").Range(Team_Range2).Copy Destination:=Worksheets(strTabName).Range("I20")
  RangeCopyPic rng:=Selection, Appearance:=xlScreen, Format:=xlPicture
  Worksheets(strTabName).Range("I20").PasteSpecial
 
    ThisWorkbook.Sheets("Control").Range("A31:A39").Copy Destination:=Worksheets(strTabName).Range("L20")
       ActiveSheet.UsedRange.EntireColumn.AutoFit
      lr2 = ThisWorkbook.Worksheets(strTabName).Cells(Rows.Count, "I").End(xlUp).Row
       For thisrow = 20 To lr2
            Sheets(strTabName).Range("J" & thisrow).NumberFormat = "[h]:mm:ss"
            ThisWorkbook.Sheets(strTabName).Range("J" & thisrow).Interior.Color = Range("I" & thisrow).DisplayFormat.Interior.Color
            ThisWorkbook.Sheets(strTabName).Range("J" & thisrow).Formula = "=SUMIF($C$20" & ":$C$" & lr & ",I" & thisrow & ",$D$20:$D$" & lr & ")"
       Next

       ThisWorkbook.Sheets(strTabName).Range("M20:M28").NumberFormat = "[h]:mm:ss"
       ThisWorkbook.Sheets(strTabName).Range("J" & lr2 + 1).Formula = "=SUM(J20:J" & lr2 & ")"
       ThisWorkbook.Sheets(strTabName).Range("M20").Formula = "=SumByColor(J20:J" & lr2 & ",L20)"
       ThisWorkbook.Sheets(strTabName).Range("M21").Formula = "=SumByColor(J20:J" & lr2 & ",L21)"
       ThisWorkbook.Sheets(strTabName).Range("M22").Formula = "=SumByColor(J20:J" & lr2 & ",L22)"
 
       ThisWorkbook.Sheets(strTabName).Range("M23").Formula = "=SumByColor(J20:J" & lr2 & ",L23)"
        ThisWorkbook.Sheets(strTabName).Range("M24:M28").Merge
      '   Sheets(strTabName).Range("L19:M24").Copy Sheets(strTabName).Range("A1")
     
         ThisWorkbook.Sheets(strTabName).Activate
         ActiveSheet.Range("L20:M29").Select
        RangeCopyPic rng:=Selection, Appearance:=xlScreen, Format:=xlPicture
    
        ActiveSheet.Range("A1").Select
         ActiveSheet.PasteSpecial
         ActiveSheet.UsedRange.EntireColumn.AutoFit

  
     
     
       ThisWorkbook.Sheets(strTabName).Range("J37").Font.Bold = True
 
       ActiveSheet.UsedRange.EntireColumn.AutoFit
       Create_Headings
       ThisWorkbook.Sheets(strTabName).Rows("19:40").Hidden = True
   
     '  ThisWorkbook.Sheets(strTabName).Range("B2") = ThisWorkbook.Sheets(strTabName).Range("M20").Text
      ' ThisWorkbook.Sheets(strTabName).Range("B3") = ThisWorkbook.Sheets(strTabName).Range("M21").Text
       'ThisWorkbook.Sheets(strTabName).Range("B4") = ThisWorkbook.Sheets(strTabName).Range("M22").Text
       'ThisWorkbook.Sheets(strTabName).Range("B5") = ThisWorkbook.Sheets(strTabName).Range("M23").Text
       'ThisWorkbook.Sheets(strTabName).Range("B6:b10") = "0"
    
       Create_button2
       Range("A1").Select
        ActiveSheet.UsedRange.EntireColumn.AutoFit
       
           Application.ScreenUpdating = True
End Sub
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Your macro won't be skipping steps.

I suspect the problem is the unqualified reference to Range("I" & thisrow), so the code will use the ActiveSheet, whatever that happens to be.

From your other code, I'm guessing you actually want this to be Worksheets(strTabName).Range("I" & thisrow)?

(You're also qualifying most, but not all, range references with ThisWorkbook. But I assume your code is dealing with just the one workbook?)
 
Upvote 0
Solution
Thanks for your response. When I put a worksheets(strTabname).activate before the loop, it worked great!
 
Upvote 0
It would be better to qualify your references, possibly using With, instead of activating things to force the default. That is not reliable.
 
Upvote 0

Forum statistics

Threads
1,214,651
Messages
6,120,744
Members
448,989
Latest member
mariah3

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