Results 1 to 2 of 2

Thread: [RESOLVED] Excel 2013 - How to have the macro check if already executed

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2012
    Posts
    10

    Resolved [RESOLVED] Excel 2013 - How to have the macro check if already executed

    Hi Guys,

    I have run into a situation where folks at my end forget they have already executed the macro and click on it again.

    Is there a way to incorporate a message box to pop up if they run the macro second time around?

    This is the macro being run on raw data download. Firstly it checks for any rows with missing data and secondly it introduces two blank rows between each row of data.

    Sub DeleteEmptyRowsInActiveSheet()

    Application.ScreenUpdating = False

    Dim lngI As Long

    For lngI = ActiveCell.SpecialCells(xlCellTypeLastCell).Row To 1 Step -1


    If Application.WorksheetFunction.CountA(ActiveSheet.Rows(lngI)) = 0 Then

    ActiveSheet.Rows(lngI).Delete

    End If

    Next



    Cells.Replace What:="NULL", Replacement:="", LookAt:=xlPart, SearchOrder _
    :=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False

    For x = Cells(Rows.Count, 1).End(xlUp).Row To 1 Step -1

    Cells(x, 1).Offset(1).EntireRow.Insert

    Cells(x, 1).Offset(1).EntireRow.Insert

    Next x



    Application.ScreenUpdating = True



    End Sub

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Excel 2013 - How to have the macro check if already executed

    you can use a static variable within the procedure like

    Code:
    Sub DeleteEmptyRowsInActiveSheet()
    
    Application.ScreenUpdating = False
    
    Dim lngI As Long
    static runalready
    if not runalready then
    runalready = true
    
    For lngI = ActiveCell.SpecialCells(xlCellTypeLastCell).Row To 1 Step -1
    
    
    If Application.WorksheetFunction.CountA(ActiveSheet.Rows(lngI)) = 0 Then
    
    ActiveSheet.Rows(lngI).Delete
    
    End If
    
    Next
    
    
    
    Cells.Replace What:="NULL", Replacement:="", LookAt:=xlPart, SearchOrder _
    :=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
    
    For x = Cells(Rows.Count, 1).End(xlUp).Row To 1 Step -1
    
    Cells(x, 1).Offset(1).EntireRow.Insert
    
    Cells(x, 1).Offset(1).EntireRow.Insert
    
    Next x
    
    
    
    Application.ScreenUpdating = True
    else 
    msgbox "this has already been run"
    
    End Sub
    this will work on a per session basis, when ever the workbook is opened runalready will be false
    for a permanent method save some value to a cell or customdocumentproperty, then check that instead of the variable
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width