+ Reply to Thread
Results 1 to 8 of 8

MAcro works OK in Step into debug mode but not doing job when run from command button

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    12-25-2011
    Location
    Lahore, Punjab, Pakistan
    MS-Off Ver
    Excel 2010
    Posts
    287

    MAcro works OK in Step into debug mode but not doing job when run from command button

    I have macro that runs OK when run by continually pressing F8 key and imports about 100 rows

    However when I run with command button, it only imports 2 lines and then stops imemdiately without any error.

    Sub SDrops()
    Dim ws As Worksheet
    
    Sheets("SunDps").Activate
    Range("A3:G" & Application.Max(3, Range("A1000000").End(xlUp).Row)).ClearContents
    empt = Cells(1, 4).Value
    i = 3
    For Each ws In ThisWorkbook.Worksheets
        If ws.Name = "G" Or ws.Name = "H" Then
        Lrw = ws.Range("F1000000").End(xlUp).Row
        ws.Activate
            For rs = 2 To Lrw
                If Cells(rs, 3).Value = empt And Cells(rs, 7).Value <> "y" Then
                    Cells(rs, 3).EntireRow.Copy
                    Sheets("SunDps").Range("A" & i).PasteSpecial Paste:=xlPasteValues
                    i = i + 1
                End If
            Next rs
        End If
    Next ws
    End Sub
    Last edited by caabdul; 03-10-2018 at 11:34 AM.

  2. #2
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: MAcro works OK in Step into debug mode but not doing job when run from command button

    You are using select and active sheet and when you run the code, it picks the sheet names wherever the cursor happens to be, not necessarily the correct sheet name. You need to properly reference your sheet names and range.
    It is also faster to use a filter function than a loop.

    Option Explicit
    
    Sub SDrops()
    Dim ws As Worksheet
    Dim I As Long
    Dim Lrw As Long
    Dim rs As Long
    Dim empt
    
    Sheets("SunDps").Range("A3:G" & Application.Max(3, Sheets("SunDps").Range("A" & Rows.Count).End(xlUp).Row)).ClearContents
    empt = Sheets("SunDps").Cells(1, 4).Value
    I = 3
    For Each ws In ThisWorkbook.Worksheets
        With ws
            If .Name = "G" Or .Name = "H" Then
            Lrw = .Range("F" & .Rows.Count).End(xlUp).Row
                For rs = 2 To Lrw
                    If .Cells(rs, 3).Value = empt And .Cells(rs, 7).Value <> "y" Then
                        .Cells(rs, 3).EntireRow.Copy
                        Sheets("SunDps").Range("A" & I).PasteSpecial Paste:=xlPasteValues
                        I = I + 1
                    End If
                Next rs
            End If
        End With
    Next ws
    End Sub

  3. #3
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Re: MAcro works OK in Step into debug mode but not doing job when run from command button


    Hi !

    Better & faster execution just using a filter or an advanced filter …

  4. #4
    Forum Contributor
    Join Date
    12-25-2011
    Location
    Lahore, Punjab, Pakistan
    MS-Off Ver
    Excel 2010
    Posts
    287

    Re: MAcro works OK in Step into debug mode but not doing job when run from command button

    ah, I forgot to remove "ws.Activate", in "With ws" section. It works Now. Thanks

  5. #5
    Forum Contributor
    Join Date
    12-25-2011
    Location
    Lahore, Punjab, Pakistan
    MS-Off Ver
    Excel 2010
    Posts
    287

    Re: MAcro works OK in Step into debug mode but not doing job when run from command button

    Hi AB33

    I did this;

    empt = Sheets("SunDps").Cells(1, 4).Value
    and also put
    With ws
    but it doesn't help. Even when I run macro from Visual Basic window's Run button, it works.

  6. #6
    Forum Contributor
    Join Date
    12-25-2011
    Location
    Lahore, Punjab, Pakistan
    MS-Off Ver
    Excel 2010
    Posts
    287

    Re: MAcro works OK in Step into debug mode but not doing job when run from command button

    Same problem happening to another code. Code won't run using button in sheet. What is issue in it now?

    Sub Shfts()
    
    Dim SMin As Long, SMax As Long
    Dim wb As Workbook
    Dim ws As Worksheet
    
    Set wb = ActiveWorkbook
    
    With wb
        For Each ws In wb.Worksheets
            With ws
                If .Name = "G" Or .Name = "H" Then
                    SMin = Application.Min(.Range("B:B"))
                    SMax = Application.Max(.Range("B:B"))
            
        'Filling Shift tables with all shifts and deleting extra shifts
                    If SMin + SMax <> 0 Then
                        With wb.Sheets("Shifts")
                            If ws.Name = "G" Then
                                tb = "Table1"
                            ElseIf ws.Name = "H" Then
                                tb = "Table2"
                            End If
                            
                            With .ListObjects(tb)
                                If (tb = "Table1" And Application.Sum(Range("A:A")) > 0) Or _
                                (tb = "Table2" And Application.Sum(Range("F:F")) > 0) Then
                                    Do While .DataBodyRange(1, 1).Value > SMin
                                        .ListRows.Add (1)
                                        .DataBodyRange(1, 1).Value = .DataBodyRange(2, 1).Value - 1
                                    Loop
                                End If
                                
                                If (tb = "Table1" And Application.Sum(Range("A:A")) = 0) Or _
                                (tb = "Table2" And Application.Sum(Range("F:F")) = 0) Then
                                    LstRc = 1
                                    TMax = 0
                                    .ListRows.Add AlwaysInsert:=True
                                    .DataBodyRange(LstRc, 1).Value = SMin
                                Else
                                    ' To get last record for a table we exclude 2 heading rows.
                                    LstRc = .DataBodyRange(1, 1).End(xlDown).Row - 1 - 2
                                    TMax = .DataBodyRange(LstRc, 1).Value
                                End If
    
                                If TMax < SMax Then
                                    For i = TMax To SMax - 1
                                        .ListRows.Add AlwaysInsert:=True
                                        .DataBodyRange(LstRc + 1, 1).Value = .DataBodyRange(LstRc, 1).Value + 1
                                        LstRc = .DataBodyRange(1, 1).End(xlDown).Row - 1 - 2
                                    Next i
                                End If
                            End With
                        End With
                    End If
                        If .Name = "G" Then
                            co = 0
                        Else
                            co = 5
                        End If
                            .Range("C2:C" & Range("C2").End(xlDown).Row).FormulaR1C1 = "=IF(RC[-1]=""Open"", ""x"",VLOOKUP(RC[-1],Shifts!C[" & -2 + co & "]:C[" & -1 + co & "],2,FALSE))"
                End If
            End With
        Next ws
    End With
    
    End Sub

  7. #7
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MSO Home and Business 2024
    Posts
    7,366

    Re: MAcro works OK in Step into debug mode but not doing job when run from command button

    In the application.Sum part you have to specify which sheet you want this to be executed.
    Avoid using Select, Selection and Activate in your code. Use With ... End With instead.
    You can show your appreciation for those that have helped you by clicking the * at the bottom left of any of their posts.

  8. #8
    Forum Contributor
    Join Date
    12-25-2011
    Location
    Lahore, Punjab, Pakistan
    MS-Off Ver
    Excel 2010
    Posts
    287

    Re: MAcro works OK in Step into debug mode but not doing job when run from command button

    It worked, Thanks

    I changed all 4 instances of .Sum to ....Sum(.Sheets("Shifts").Range......
    Last edited by caabdul; 03-10-2018 at 11:34 AM.

+ 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. Single step works fine but command button has errors
    By CptCrunch51 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-11-2017, 01:07 PM
  2. [SOLVED] VBA code works in debug mode but misses steps running in normal mode
    By supereeg in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-18-2016, 05:42 AM
  3. [SOLVED] A command button that works while in manual mode??
    By TDeRanger in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 01-12-2014, 07:14 AM
  4. VBA code only works correctly in Debug mode
    By tpthatsme in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 02-09-2012, 12:51 PM
  5. Macro Works but not in Debug Step mode
    By Bob Smedley in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-31-2006, 12:35 PM
  6. [SOLVED] How show value of variable by mouse-over in debug single step mode
    By Chet Shannon in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 01-06-2006, 11:35 AM
  7. How show value of variable in single step debug mode?
    By Chet Shannon in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-28-2005, 03:00 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