Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

Proper Singular Macro Code to Highlight/Unhighlight a Cell in Another Sheet

0

I want to know if there is a singular code that can be used to highlight cells in a different worksheet in the same workbook.  In the attached Sample Workbook, you will find 4 tabs.  A "Summary Sheet" Tab and 3 Bid Package Data Tabs.  Each Data tab has an "Alert" Button on Row 3 that when pressed, will highlight the corresponding "Last Updated" timestamp on the Summary Sheet.  For example, if the Alert button is pressed on the 02A Demo Data tab, then the "Last Updated" timestamp will highlight yellow in cell "O8" on the Summary Sheet for 02A Demo.  In order to make this work I have used the following code:

Sub Alert02A()
'
' Alert00 Macro
' Alert Summary Sheet that Line has Been Updated
'

'
    Sheets("Summary Sheet").Select
    Range("O8").Select
    With Selection.Interior
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        .Color = 65535
        .TintAndShade = 0
        .PatternTintAndShade = 0
    End With
End Sub

'

This works fine, but in order to set this up for the whole sheet I will have to create an individual macro for each Data Tab and its corresponding timestamp cell on the Summary Sheet.  There are 174 data tabs in the original document, and doing that would be extremely time consuming.  So I want to know if there is one singular macro code that I could use for "Alert" buttons on all Data Tabs that would be able to highlight the appropriate cell on the summary sheetthat corresponds to the Data tab that the Alert button was pressed on.  This would allow me to have just one macro code as opposed to 174 macro codes.

I have a similar issue with the "Acknowledge" Button on the Summary Sheet.  Currently I have that button set up to UN-highlight the corresponding timestamp cell, but again I  am using the following code that would have to be duplicated and slightly midified for each row, therefore creating an additional 174 macros.

Sub Acknowledge02A()
'
' Acknowledge02A Macro
' Acknowledge Most Recent Update
'

'
    Range("O8").Select
    With Selection.Interior
        .Pattern = xlNone
        .TintAndShade = 0
        .PatternTintAndShade = 0
    End With
End Sub

Is there also a singular macro code that could be applied to all "Acknowledge" Buttons on the Summary Sheet that would UN-highlight the adjacent Timestamp cell?

I'm a novice when it comes to this VBA coding and macros, and have only gotten this far by doing research and watching videos, so please take that into account when replying, and don't get too technical on me.  Thanks

Answer
Discuss

Answers

0

You have really done your homework. It will be a pleasure helping you. Start with understanding event procedures. They save you the trouble of clicking buttons. Therefore you don't need buttons - or much fewer, at least - if you understand Excel's application events.

The design I will suggest to you replaces your existing procedures in the Bid sheets with this one.

Option Explicit

Private Sub WorkSheet_Change(ByVal Target As Range)
    ' 095
    ApplyTimeStamp Target
End Sub

Observe the "Option Explicit" instruction at the top. You should have this in every code module. It's the sure-fire remedy against pulled or premature gray hair.The single line of code calls a procedure and passes the Target (the clicked cell) to that procedure. The above procedure is the same in all sheets because the procedure called is the same for all sheets which is possible because all your bid sheets enjoy an identical structure. From the point of view of the called procedure every call is different because Target is different.

The procedure responding all is the one below. It must be in a standard code module. Excel doesn't have a standard code module by default. You will need to insert it. Its default name will be Module1 but I strongly urge you to give it a meaningful name. I usually name them, like, TXL_4712 where the number is the thread number here (no spaces permitted) but you may prfer a name like "EventManager". Keep code that doesn't match the description in another module.

Option Explicit

' identify columns on the Summary tab
Public Const AlertClm       As String = "B"     ' column to highlight
Public Const AcknClm        As String = "Q"     ' "Acknowledge" column

Sub ApplyTimeStamp(Target As Range)
    ' 095

    Dim TriggerRng      As Range        ' action only if change occurred here
    Dim BidId           As String       ' Bid Package ID extracted from STab name
    Dim Rt              As Long         ' Target column (in Summary sheet)

    With Target
        With .Worksheet
            ' action only if change occurred here
            Set TriggerRng = .Range(.Cells(10, "B"), .Cells(14, "B"))
            If Not Application.Intersect(Target, TriggerRng) Is Nothing Then
                Application.EnableEvents = False
                .Cells(3, "B").Value = Format(Now(), "mm-dd-yyyy hh:mm AM/PM")
                BidId = Trim(Split(.Name)(0))
            End If
        End With
    End With

    If Len(BidId) Then                  ' blank if no time stamp was applied
        With Worksheets("Summary Sheet")
            On Error Resume Next
            Rt = WorksheetFunction.Match(BidId, .Columns(AlertClm), 0)
            If Rt Then                  ' do nothing if a match wasn't found
                .Cells(Rt, AlertClm).Interior.Color = vbYellow
                .Cells(Rt, AcknClm).Value = "Acknowledge"
            End If
            Err.Clear
            Application.EnableEvents = True
        End With
    End If
End Sub

There are two constants at the top of the code which you need to set. They are used throughout the project. Therefore they are Public. (Set them once and for all, not everywhere interspersed into the code.) Normally, I would use column numbers but for now letters will do. In your Summary Sheet the ''Acknowledgement' column actually is column P. I used the next column because P is occupied. You can change the column later. No need to look into the code. One change here will do the job.

The code first sets the time stamp. Avoid defining target cells as offsets from the Target. Observe that Target.Offset(-6) isn't the same as Cells(Target.Row, "B"). That's because Target.Row depends upon the cell where the change occurred. Your code will place the time stamp all over the place, always 6 rows up from where you changed something. What if your change was in Row 42?

In its second part the code highlights the corresponding row on the summary sheet.. It would but take a minor chage to really highlight the entire row because the MATCH function already found its number. However, for now the code just highlights the reference in the BidID column. And it writes the word "Acknowledge" to the Acknowledgement column, in deference to your button of the same name.

As you see, the Alert buttons have become obsolete. To do the same to the Acknowledge buttons please install the event procedure below in the code sheet of your Summary tab.

Option Explicit

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    ' 095

    Dim TriggerRng      As Range            ' the range where double-click triggers action

    Set TriggerRng = Range(Cells(6, AlertClm), _
                           Cells(Rows.Count, AlertClm).End(xlUp)) _
                          .Offset(0, Columns(AcknClm).Column - Columns(AlertClm).Column)
    If Not Application.Intersect(Target, TriggerRng) Is Nothing Then
        With Target
            If .Value = "Acknowledge" Then
                .Value = ""
                With Cells(.Row, AlertClm)
                    .Interior.Pattern = xlNone
                    .Select
                End With
                Cancel = True
            End If
        End With
    End If
End Sub

Observe that this code uses the same constants that were declared in the standard code module. This event procedure responds to the double-click of a cell (not the change). Just double-click on the word "Acknowledge" in column Q and the word disappears along with the highlight. 

Here is an overview of the workflow.

  1. Make a change in B10:B14 in any of the bid sheets.
    1. The time stamp will be applied to B3 of that sheet
  2. The corresponding cell in the Summary sheet (column B) will be lit and the word "Acknowledge" added in column Q.
    1. The macro identifies the row using the first word of the sheet name.
    2. You can add/delete sheets and items at will.
  3. Double-click on the word "Acknowledge" to delete the word and the highlight.
Discuss


Answer the Question

You must create an account to use the forum. Create an Account or Login