+ Reply to Thread
Results 1 to 9 of 9

Error msg pops out when a button is clicked more than once

Hybrid View

  1. #1
    Registered User
    Join Date
    10-21-2015
    Location
    Singapore
    MS-Off Ver
    2013
    Posts
    27

    Error msg pops out when a button is clicked more than once

    Sub CC()
    Dim lngLastRow As Long, lngRow As Long
        
        With Application
            .ScreenUpdating = False
            .Calculation = xlCalculationManual
        End With
        
        lngLastRow = Range("A" & Rows.Count).End(xlUp).Row
        
        For lngRow = lngLastRow To 2 Step -1
            If Right(Range("B" & lngRow), 5) = "Total" Then Rows(lngRow).Delete
        Next lngRow
    
        For lngRow = lngLastRow To 2 Step -1
            If Right(Range("A" & lngRow), 5) = "Total" Then Rows(lngRow).Delete
        Next lngRow
        
        For lngRow = lngLastRow To 2 Step -1
             If Right(Range("B" & lngRow), 5) = "total" Then Rows(lngRow).Delete
        Next lngRow
    
        For lngRow = lngLastRow To 2 Step -1
            If Right(Range("A" & lngRow), 5) = "total" Then Rows(lngRow).Delete
        Next lngRow
        
        For lngRow = lngLastRow To 2 Step -1
            If Left(Range("B" & lngRow), 5) = "Total" Then Rows(lngRow).Delete
        Next lngRow
    
        For lngRow = lngLastRow To 2 Step -1
            If Left(Range("A" & lngRow), 5) = "Total" Then Rows(lngRow).Delete
        Next lngRow
        
        For lngRow = lngLastRow To 2 Step -1
             If Left(Range("B" & lngRow), 5) = "total" Then Rows(lngRow).Delete
        Next lngRow
    
        For lngRow = lngLastRow To 2 Step -1
            If Left(Range("A" & lngRow), 5) = "total" Then Rows(lngRow).Delete
        Next lngRow
        
        With Application
            .Calculation = xlCalculationAutomatic
            .ScreenUpdating = True
        End With
        
    Lastrow = Range("E" & Rows.Count).End(xlUp).Row
    Range("A7 : C" & Lastrow).Select
    Selection.Copy
    Selection.PasteSpecial Paste:=xlValues, SkipBlanks:=False, Transpose:=False
    Selection.SpecialCells(xlCellTypeBlanks).Select
    Selection.FormulaR1C1 = "=R[-1]C"
    
    With Range("A7:C" & Lastrow)
        .Cells.Copy
        .Cells.PasteSpecial xlPasteValues
        .Cells(1).Select
    End With
    Application.CutCopyMode = False
    
    
    With Range("A7:C" & Lastrow)
        .Cells.Copy
        .Cells.PasteSpecial xlPasteValues
        .Cells(1).Select
    End With
    Application.CutCopyMode = False
    
    End Sub
    If the user clicks on the button more than once, a msg box woould appear stating "Runtime Error '1004': Method Range of Object '_Global' Failed"/ However, nothing would happen to the fields or data. Is there a way to add a line of code such that when user decides or accidentally presses the button more than once, no error msg would appear and noting will happen to the data?

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Error msg pops out when a button is clicked more than once

    Hello jlyh11,

    Welcome to the Forum!

    To better understand your problem, please post a copy of the workbook for review. It will save a lot time if you do.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,937

    Re: Error msg pops out when a button is clicked more than once

    Hi Leth,
    I know the problem.
    What I do is define Public variable isBusy as boolean
    The routine wher you press the button should be some like this:

    Sub Button_Click()
    if isBusy = True then exit Sub
    isBusy = True
    the rest of you code goes here
    
    
    isBusy=False
    Exit Sub
    Hope this gives you an idea
    ---
    Hans
    "IT" Always crosses your path!
    May the (vba) code be with you... if it isn't; start debugging!
    If you like my answer, Click the * below to say thank-you

  4. #4
    Registered User
    Join Date
    10-21-2015
    Location
    Singapore
    MS-Off Ver
    2013
    Posts
    27

    Re: Error msg pops out when a button is clicked more than once

    Hello Keebellah,
    I tried to put this in together with the exisiting code but it didnt work. The error box still came out. Could you see if the order is correct?

    Sub CC()
    Application.DisplayAlerts = False
    If isBusy = True Then Exit Sub
    isBusy = True
    
    
    
    Dim lngLastRow As Long, lngRow As Long
        
        With Application
            .ScreenUpdating = False
            .Calculation = xlCalculationManual
        End With
        
        lngLastRow = Range("A" & Rows.Count).End(xlUp).Row
        
        For lngRow = lngLastRow To 2 Step -1
            If Right(Range("B" & lngRow), 5) = "Total" Then Rows(lngRow).Delete
        Next lngRow
    
        For lngRow = lngLastRow To 2 Step -1
            If Right(Range("A" & lngRow), 5) = "Total" Then Rows(lngRow).Delete
        Next lngRow
        
        For lngRow = lngLastRow To 2 Step -1
             If Right(Range("B" & lngRow), 5) = "total" Then Rows(lngRow).Delete
        Next lngRow
    
        For lngRow = lngLastRow To 2 Step -1
            If Right(Range("A" & lngRow), 5) = "total" Then Rows(lngRow).Delete
        Next lngRow
        
        For lngRow = lngLastRow To 2 Step -1
            If Left(Range("B" & lngRow), 5) = "Total" Then Rows(lngRow).Delete
        Next lngRow
    
        For lngRow = lngLastRow To 2 Step -1
            If Left(Range("A" & lngRow), 5) = "Total" Then Rows(lngRow).Delete
        Next lngRow
        
        For lngRow = lngLastRow To 2 Step -1
             If Left(Range("B" & lngRow), 5) = "total" Then Rows(lngRow).Delete
        Next lngRow
    
        For lngRow = lngLastRow To 2 Step -1
            If Left(Range("A" & lngRow), 5) = "total" Then Rows(lngRow).Delete
        Next lngRow
        
        With Application
            .Calculation = xlCalculationAutomatic
            .ScreenUpdating = True
        End With
        
    Lastrow = Range("E" & Rows.Count).End(xlUp).Row
    Range("A7 : C" & Lastrow).Select
    Selection.Copy
    Selection.PasteSpecial Paste:=xlValues, SkipBlanks:=False, Transpose:=False
    Selection.SpecialCells(xlCellTypeBlanks).Select
    Application.DisplayAlerts = False
    Selection.FormulaR1C1 = "=R[-1]C"
    
    With Range("A7:C" & Lastrow)
        .Cells.Copy
        .Cells.PasteSpecial xlPasteValues
        .Cells(1).Select
    End With
    Application.CutCopyMode = False
    
    
    With Range("A7:C" & Lastrow)
        .Cells.Copy
        .Cells.PasteSpecial xlPasteValues
        .Cells(1).Select
    End With
    Application.CutCopyMode = False
    
    
    isBusy = False
    Exit Sub
    
    End Sub
    Last edited by jlyh11; 10-22-2015 at 02:03 AM.

  5. #5
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Error msg pops out when a button is clicked more than once

    Hello Keebellah,

    I am waiting for the OP to respond before proposing any solutions. Assumptions made on a code fragment can waste a lot of time. Perhaps you have a lot of time but do not assume the same for the OP.

  6. #6
    Registered User
    Join Date
    10-21-2015
    Location
    Singapore
    MS-Off Ver
    2013
    Posts
    27

    Re: Error msg pops out when a button is clicked more than once

    Hi Leith, I cant post a copy of the workbook as the information of the company is private and confidential. Let me try to explain the issue again.

    When the user presses a macro button more than once, an error will pop up saying: Error 1004, no cells were found. I do not want to see this error box even if no cells were found. Thus, what I need is that no error box will appear no matter how many times the user presses the macro button.

  7. #7
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,937

    Re: Error msg pops out when a button is clicked more than once

    Quote Originally Posted by Leith Ross View Post
    Hello Keebellah,

    I am waiting for the OP to respond before proposing any solutions. Assumptions made on a code fragment can waste a lot of time. Perhaps you have a lot of time but do not assume the same for the OP.
    Could you explain your "Perhaps you have a lot of time but ..."
    The idea is not an assumption, it's reading what the OP says.
    A button can be pressed many times and can trigger error. It was just an idea.
    And also yes: I DO HAVE A LOT OF TIME that's the advantage of being and old and retired person

  8. #8
    Forum Guru sktneer's Avatar
    Join Date
    04-30-2011
    Location
    Kanpur, India
    MS-Off Ver
    Office 365
    Posts
    9,655

    Re: Error msg pops out when a button is clicked more than once

    Without seeing your workbook the max I can suggest you to use one line of code in the beginning On Error Resume Next and see if the code doesn't produce any undesired output if it bypasses that error and gets executed again.
    Regards
    sktneer


    Treat people the way you want to be treated. Talk to people the way you want to be talked to.
    Respect is earned NOT given.

  9. #9
    Forum Guru sktneer's Avatar
    Join Date
    04-30-2011
    Location
    Kanpur, India
    MS-Off Ver
    Office 365
    Posts
    9,655

    Re: Error msg pops out when a button is clicked more than once

    Or after looking at your code, it seems that you might get an error because of the following line if no blank cell is found
    Selection.SpecialCells(xlCellTypeBlanks).Select
    So to avoid that error you may replace the following lines of code

    Selection.SpecialCells(xlCellTypeBlanks).Select
    Application.DisplayAlerts = False
    Selection.FormulaR1C1 = "=R[-1]C"
    With this

    On Error Resume Next
    Selection.SpecialCells(xlCellTypeBlanks).Select
    If Err = 0 Then
       Selection.FormulaR1C1 = "=R[-1]C"
    End If
    On Error GoTo 0
    See if that resolves your issue.

+ 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. Run-time error 70 when button is clicked to run macro that opens email
    By blacklotus0014 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 12-02-2013, 09:54 AM
  2. Runtime error 1004 while refresh button clicked and focus changed
    By sonypjoy in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-19-2013, 01:43 PM
  3. Error pops up on line On Error Resume Next
    By Erusso in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 12-20-2012, 04:57 PM
  4. Modifying a Macro Button to time stamp when the button was clicked.
    By jonvanwyk in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 12-22-2010, 04:46 PM
  5. Error when previous button clicked when on First Multipage
    By jwala in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 12-04-2010, 12:44 AM
  6. Error-the message 'Device I/O error' pops up.
    By lehainam in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-08-2006, 01:26 AM
  7. Button Pops up Graph
    By pete3589 in forum Excel General
    Replies: 3
    Last Post: 11-05-2005, 03:10 PM
  8. Insert Visio Button sheet pops up automatically
    By quailhunter in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-07-2005, 07:57 PM

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