Get macro to work before moving sheets?

danbates

Active Member
Joined
Oct 8, 2017
Messages
377
Office Version
  1. 2016
Platform
  1. Windows
Hi,

I the following code that checks for any blank cells within the ranges and it works fine when calling it manually.

VBA Code:
Sub CheckCells()

    Dim cell As Range
    Dim str As String
    Dim bIsEmpty As Boolean
    Dim blank As Range
    
Dim Daily, Daily1, Daily2, MultiDaily As Range

Set Daily = Range(Range("C" & Selection.Row), Range("F" & Selection.Row))
Set Daily1 = Range("H" & Selection.Row)
Set Daily2 = Range(Range("J" & Selection.Row), Range("L" & Selection.Row))

Set MultiDaily = Union(Daily, Daily1, Daily2)

For Each cell In MultiDaily
    If IsEmpty(cell) = True Then
    
    'THE 2 BELOW IS FOR THE HEADERS TO APPEAR IN THE MESSAGE BOX FROM ROW 2
            str = str & Cells(2, cell.Column).Value & vbCrLf
        bIsEmpty = True
    'FINDS THE FIRST BLANK CELL AND SELECTS
        For Each blank In MultiDaily
        If blank = "" Then
            blank.Select
            Exit For
        End If
    Next
    End If

Next cell
    
If bIsEmpty = True Then
MsgBox "PLEASE COMPLETE THE FOLLOWING BEFORE SIGNING OFF:  " & vbNewLine & vbNewLine & str, vbInformation, "Palletiser Operator"
    End If

End Sub

I would like to use it when I move to a different sheet so I added it to the Worksheet Deactivate sub but the code works on the sheet that I moved to and not the sheet I left.

What can I do to make the code work before leaving the sheet?

Thanks

Dan
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
that's happening because when you do this:
Code:
RANGE(cell, cell)
you're pointing to the active sheet only. in that case, you're using the RANGE function without a qualifying statement, such as:
Code:
activesheet.
if you want to run the code in a routine behind one sheet, but actually make the code manipulate the data in another sheet, you have to do this:
Code:
Set Daily = worksheets("sheet name here").Range(cell, cell)
but per your words:
What can I do to make the code work before leaving the sheet?
if you want to run the code on the current sheet before clicking on the next sheet name and putting the focus on that new sheet, you will need a button on the activesheet to run the code first, then move the focus to the new sheet.
 
Upvote 0
If you put the code in that Deactivate event, rather than just calling it from the event, then you should be ok.
 
Upvote 0

Forum statistics

Threads
1,214,943
Messages
6,122,376
Members
449,080
Latest member
Armadillos

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