VBA - Check and run macro only once

j4ymf

Well-known Member
Joined
Apr 28, 2003
Messages
741
Office Version
  1. 365
Platform
  1. Windows
Morning

I have a macro, can some one please help me change it and write the code to check if the macro have run before when the button is pressed
if it has please return a message saying sorry this has be run before. then end the macro

my thoughts were to have a look and see if anything was in cell """" Destination:=Sheets("P1").Range("A13")) ""
if not run the macro

many thanks jason

Code:
Sub GetWebPage_P1()
    'Copy web page
    On Error GoTo error
    Application.ScreenUpdating = False
    
        With Sheets("P1").QueryTables.Add(Connection:= _
            "URL;" & Sheets("Data").Range("B22").Value, _
            Destination:=Sheets("P1").Range("A13"))
        
            .BackgroundQuery = True
            .TablesOnlyFromHTML = True
            .Refresh BackgroundQuery:=False
            .SaveData = True
        End With
        
         Sheets("Test").Activate
    Range("L12:Y764").Select
    Application.CutCopyMode = False
    Selection.Copy
    Sheets("P1").Select
    Range("L13").Select
    ActiveSheet.Paste
    Columns("L:L").ColumnWidth = 16.83
    Columns("V:V").ColumnWidth = 14.5
    Range("A1").Select
        
     
error:
Application.ScreenUpdating = True
End Sub
 
Much more straightforward would be to disable the control...unfortunately, a forms control on a worksheet cannot be disabled in Excel 2010 or later.

So, maybe, change the caption to "already clicked; keeps your paws off this button" - or something more polite. Alternatively, just hide the button?
Code:
    Dim WS As Worksheet: Set WS = ActiveSheet
    Dim Shp As Shape: Set Shp = WS.Shapes("button 1")
    Shp.Visible = False
 
Upvote 0

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
I'd do something like:

Code:
    If Sheets("P2").Range("A1") = "X" Then 
    MsgBox "Sorry, This button has already been pressed!"
    Exit Sub
    End If

        With Sheets("P2").QueryTables.Add(Connection:= _
 
Upvote 0
THANK YOU Steph77

Thats just what i was looking for
thats great

jason
 
Upvote 0

Forum statistics

Threads
1,215,019
Messages
6,122,707
Members
449,093
Latest member
Mnur

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