VBA code for multiple columns

WobblyPenguin

New Member
Joined
Jan 17, 2025
Messages
2
Office Version
  1. 365
Platform
  1. Windows
The function is to put an 'X' on the row within that column when the keyword matches what is in column A. I have this working for column 'E' but I would like to apply it to columns F-K as well with the same function.

Any suggestions are appreciated! Thank you in advance!

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim KeyCell As Range
    Dim LookupRange As Range
    Dim MatchCell As Range

    ' Define the key cell (E39) and the lookup range (column A)
    Set KeyCell = Me.Range("E39")
    Set LookupRange = Me.Range("A1:A" & Me.Cells(Me.Rows.Count, "A").End(xlUp).Row)

    ' Check if E39 is the changed cell
    If Not Intersect(Target, KeyCell) Is Nothing Then
        Application.EnableEvents = False

        ' Only proceed if E39 has a value
        If KeyCell.Value <> "" Then
            For Each MatchCell In LookupRange
                If MatchCell.Value = KeyCell.Value Then
                    MatchCell.Offset(0, 4).Value = "X" ' Offset to column E
                End If
            Next MatchCell
        End If

        ' Do nothing if E39 is cleared; keep the existing "X" values
        Application.EnableEvents = True
    End If
End Sub
 

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
Hi @WobblyPenguin.
Try next updated code:
VBA Code:
Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim MatchCell   As Range

    ' Define the key cell (E39) and the lookup range (column A)
    Dim KeyCell     As Range
    Set KeyCell = Me.Range("E39")

    Dim LookupRange As Range
    Set LookupRange = Me.Range("A1:A" & Me.Cells(Me.Rows.Count, "A").End(xlUp).Row)

    ' Check if E39 is the changed cell
    If Not Intersect(Target, KeyCell) Is Nothing Then
        MsgBox "YES, Intersect! "
        Application.EnableEvents = False

        ' Only proceed if E39 has a value
        If KeyCell.Value <> "" Then
            For Each MatchCell In LookupRange

                If MatchCell.Value = KeyCell.Value Then
                    Dim i As Long
                    
                    For i = 4 To 10
                        MatchCell.Offset(0, i).Value = "X"
                    Next i
                
                End If

            Next MatchCell

        End If

        ' Do nothing if E39 is cleared; keep the existing "X" values
        Application.EnableEvents = True
    End If

End Sub
 
Upvote 0
Hi @WobblyPenguin.
Try next updated code:
VBA Code:
Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim MatchCell   As Range

    ' Define the key cell (E39) and the lookup range (column A)
    Dim KeyCell     As Range
    Set KeyCell = Me.Range("E39")

    Dim LookupRange As Range
    Set LookupRange = Me.Range("A1:A" & Me.Cells(Me.Rows.Count, "A").End(xlUp).Row)

    ' Check if E39 is the changed cell
    If Not Intersect(Target, KeyCell) Is Nothing Then
        MsgBox "YES, Intersect! "
        Application.EnableEvents = False

        ' Only proceed if E39 has a value
        If KeyCell.Value <> "" Then
            For Each MatchCell In LookupRange

                If MatchCell.Value = KeyCell.Value Then
                    Dim i As Long
                   
                    For i = 4 To 10
                        MatchCell.Offset(0, i).Value = "X"
                    Next i
               
                End If

            Next MatchCell

        End If

        ' Do nothing if E39 is cleared; keep the existing "X" values
        Application.EnableEvents = True
    End If

End Sub

Thank you very much for this @MikeVol . This is working but I should have been more specific when I asked for help. I am looking for each column to have their own input. For example, E39 marks the E column, F39 marks the F column, G39 marks the G column, etc. Any help with this would be helpful. Thank you again for the code you provided me.
 
Upvote 0
If I understood you correctly, try next
VBA Code:
Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim KeyCell     As Range
    Dim MatchCell   As Range

    Dim LookupRange As Range
    Set LookupRange = Me.Range("A1:A" & Me.Cells(Me.Rows.Count, "A").End(xlUp).Row)

    Application.EnableEvents = False
    On Error GoTo Ext

    For Each KeyCell In Target

        If Not Intersect(KeyCell, Me.Range("E39:K39")) Is Nothing Then

            Dim ColumnOffset As Long
            ColumnOffset = KeyCell.Column - Me.Range("E39").Column + 4

            If KeyCell.Value <> "" Then

                For Each MatchCell In LookupRange

                    If MatchCell.Value = KeyCell.Value Then
                        MatchCell.Offset(0, ColumnOffset).Value = "X"
                    End If

                Next MatchCell

            End If

        End If

    Next KeyCell

Ext:
    Application.EnableEvents = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,225,619
Messages
6,186,047
Members
453,335
Latest member
sfd039

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