Macro to check if another macro was activate

Alwina

Board Regular
Joined
Oct 3, 2013
Messages
61
Hi

Is it possible to have a macro to check if another macro was activated? I have the following macro that insert the starting time in a column when you click on the start button.
Code:
Sub setStart()
    Call unprotect
    
    If Range("F1") = Empty Or Range("F2") = Empty Then
   ' MsgBox "Cell A5 is empty!"
    Else
    Dim NextRow As Long
    NextRow = GetLastRow("K") + 1

    With Range("K" & NextRow)
        .Value = Now
        .NumberFormat = "yyyy/mm/dd HH:mm:ss"
    End With
    End If
 '  ActiveSheet.CommandButton1.Caption = "STOP"
    Call protect
End Sub

and the following macro to insert the stop time when clicking on the Stop button
Code:
Sub setStop()
    Call unprotect
   ' Call Score
     If Range("F1") = Empty Or Range("F2") = Empty Then
    MsgBox "Please Ensure that you clicked on 'Play' before you click on 'Stop'!"
    Else
    Dim NextRow As Long
    NextRow = GetLastRow("L") + 1

    With Range("l" & NextRow)
        .Value = Now
        .NumberFormat = "yyyy/mm/dd HH:mm:ss"
    End With
    calcElapsedTime (NextRow)
'    ActiveSheet.CommandButton1.Caption = "START"
    Call Score
    End If
    Call protect
    
End Sub

Basically i want it before it insert the start time in column K it must check that the last entry in column k must have a value in the same row in column l, if there is no entry in column L in that row it must give an error message "You already pressed Start".
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Basicly you want a criteria for the macro to run?

Will the entry(col K) have a specific range each time? You have column K, I and L.
So, something like the below?

Code:
[COLOR=#0000ff]'Just assuming last entry(downwards in col K) is in column K then check[/COLOR]
StartCheckRow = range("K:" & rows.count).end(xlup).row
If range("L:" & StartCheckRow) <> "" and range("I:" & StartCheckRow) then
Msxbox "error", vbcritical
end if
 
Upvote 0
HI there Arithos

No, the range will vary.

Thanks for your reply, I tried to run you code but i get a Compile Error - Variable not defined at this line
Code:
StartCheckRow =

Should i put it like this?

Code:
Sub setStart()
   
    Call unprotect
    
    StartCheckRow = Range("K:" & Rows.count).End(xlUp).Row
    If Range("L:" & StartCheckRow) <> "" Then
    Msxbox "You already clicked on Start!", vbCritical
    End If
    
    If Range("F1") = Empty Or Range("F2") = Empty Then
   ' MsgBox "Cell A5 is empty!"
    Else
    Dim NextRow As Long
    NextRow = GetLastRow("K") + 1

    With Range("K" & NextRow)
        .Value = Now
        .NumberFormat = "yyyy/mm/dd HH:mm:ss"
    End With
    End If
 '  ActiveSheet.CommandButton1.Caption = "STOP"
    Call protect
End Sub
</pre>
 
Upvote 0
Yes, I didnt test it, just typed it in, but I see I added ":" just remove that and it should be okay,
You can change StartCheckRow to LRK ( lastrow K), and "dim LRK as long" should speed it up.

This basicly cheks if the last row in L, has a value, which corresponds with there beeing a value in K, (LRK will alway s have a value)
 
Upvote 0
I have it like this now
Code:
Sub setStart()
    Call unprotect
    Dim LastRowK As Long
    
    LastRowK = Range("K" & Rows.Count).End(xlUp).Row
    If Range("L" & LastRowK) <> "" Then
    MsgBox "error", vbCritical
    End If
    
    If Range("F1") = Empty Or Range("F2") = Empty Then
   ' MsgBox "Cell A5 is empty!"
    Else
    Dim NextRow As Long
    NextRow = GetLastRow("K") + 1

    With Range("K" & NextRow)
        .Value = Now
        .NumberFormat = "yyyy/mm/dd HH:mm:ss"
    End With
    'End If
    End If
 '  ActiveSheet.CommandButton1.Caption = "STOP"
    Call protect
End Sub

It seems to work now, however it does not seem to test what i want it to test. Lets say i have the following rows in colum K and L

K................................. L
21/01/2015 11:20..........21/01/2015 11:21
21/01/2015 11:23

If I press the button that activates the SetStart button now, it should give me the error message and stop the macro.

If the Last row in K and L looks like this
K ................................... L
21/01/2015 11:20 ............21/01/2015 11:21

and i press the button that activates SetStart, it should insert the date,time in then next available row in K - Like this
K .................................. L
21/01/2015 11:20 .......... 21/01/2015 11:21
21/01/2015 11:23

Hope it make sense.
 
Upvote 0
Aha!, I misunderstood something =)

Before you run the start again, you want to check if it was stopped last time.
See if this wont do the trick, changed and removed some bits

Code:
Sub setStart()    Call unprotect
    Dim NextRow As Long
    
    NextRow = GetLastRow("K") [COLOR=#0000ff]' I just use the same variable name, changes nothing further down. [/COLOR]
    If Range("L" & NextRow) = "" Then
        MsgBox "No Stop value for last runthrough, exiting macro", vbCritical
        exit sub
    End If
        
    If Range("F1") = Empty Or Range("F2") = Empty Then
        msgbox "Fill F1 and F2 then run the macro again." , vbinformation
        exit sub
    Else
    NextRow = GetLastRow("K") + 1
    With Range("K" & NextRow)
        .Value = Now
        .NumberFormat = "yyyy/mm/dd HH:mm:ss"
    End With
    End If
    
    Call protect
End Sub
 
Upvote 0
Just another question.

On my setStop Macro.

Code:
Sub setStop()
    Call unprotect
   ' Call Score
    Dim NextRow As Long
            
    If Range("F1") = Empty Or Range("F2") = Empty Then
    MsgBox "Please Ensure that you clicked on 'Play' before you click on 'Stop'!"
    Else
    'Dim NextRow As Long
    NextRow = GetLastRow("L") + 1

    With Range("l" & NextRow)
        .Value = Now
        .NumberFormat = "yyyy/mm/dd HH:mm:ss"
    End With
    calcElapsedTime (NextRow)
'    ActiveSheet.CommandButton1.Caption = "START"
    Call Score
    End If
    Call protect
    
End Sub

If the Last row in K and L looks like this
K ................................... L
21/01/2015 11:20 ............21/01/2015 11:21

and user click on "stop" it should give an error message, "You already stop the game"

How do i change this to fit the above

Code:
 NextRow = GetLastRow("K") [COLOR=#0000ff]' I just use the same variable name, changes nothing further down. [/COLOR]
    If Range("L" & NextRow) = "" Then
        MsgBox "No Stop value for last runthrough, exiting macro", vbCritical
        exit sub
    End If
 
Upvote 0
I would think like this,
Code:
 If [COLOR=#ff0000]Not[/COLOR] Range("L" & NextRow) = "" Then
Or,
Code:
 If Range("L" & NextRow) [COLOR=#ff0000]<>[/COLOR] "" Then
Which does the same thing...
 
Upvote 0

Forum statistics

Threads
1,214,944
Messages
6,122,392
Members
449,081
Latest member
JAMES KECULAH

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