VBA call code if value is in Range, NOT every time it is in the Range.

cchart

New Member
Joined
Sep 7, 2017
Messages
10
Good Evening I am trying to call a code when a value appears in a column. I am trying the code below, but it works every time the value appears. I just want to call the code once if, say "4W" appears in the column. "4W" may appear multiple times in the column. Is there some way to return a True or False maybe to call the code? Thank you!

<style type="text/css"> p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #000000 ; background-color: #ffffff } p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; background-color: #ffffff ; min-height: 13.0px} p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #000000 ; background-color: #ffffff ; min-height: 13.0px} p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #011993 ; background-color: #ffffff } span.s1 {color: #011993 } span.s2 {color: #000000 } </style>Sub Main()
Dim wb1 As Worksheet
Set wb1 = Sheets("Sheet1")
With wb1
For i = 2 To .Cells(Rows.Count, 2).End(xlUp).Row


If .Cells(i, 2).Value = "4W" Then
Call TimeStampWest
ElseIf .Cells(i, 2).Value = "4E" Then
Call TimeStampEast
ElseIf .Cells(i, 2).Value = "CCU" Then
Call TimeStampCCU

End If


Next
End With

End Sub
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Try this:
Code:
Sub Main()
Dim wb1 As Worksheet
Set wb1 = Sheets("Sheet1")
With wb1
    For i = 2 To .Cells(Rows.Count, 2).End(xlUp).Row
        If .Cells(i, 2).Value = "4W" Then Call TimeStampWest: Exit Sub
        If .Cells(i, 2).Value = "4E" Then Call TimeStampEast: Exit Sub
        If .Cells(i, 2).Value = "CCU" Then Call TimeStampCCU: Exit Sub
    Next
End With
End Sub
 
Upvote 0
EDIT: Hmmm, should have refreshed the screen again first. Sorry M.A.I.T.


Does the below do what you want (I think it is probably more complicated than what I have posted)?

Code:
Sub Main()
    Dim wb1 As Worksheet
    Set wb1 = Sheets("Sheet1")
    With wb1
        For i = 2 To .Cells(Rows.Count, 2).End(xlUp).Row


            If .Cells(i, 2).Value = "4W" Then
                Call TimeStampWest
                Exit Sub
            ElseIf .Cells(i, 2).Value = "4E" Then
                Call TimeStampEast
                Exit Sub
            ElseIf .Cells(i, 2).Value = "CCU" Then
                Call TimeStampCCU
                Exit Sub
            End If


        Next
    End With

End Sub
 
Last edited:
Upvote 0
The match function will find only the first occurrence.
Looking at the entire column the match result will be the row number.
The call to the timestamp macro will only happen at the first occurrence and allow the main macro to continue.
Code:
Sub Main()
    Dim wb1 As Worksheet
    Dim i As Long
    
Set wb1 = Sheets("Sheet1")
With wb1
    For i = 2 To .Cells(Rows.Count, 2).End(xlUp).Row
        If .Cells(i, 2).Value = "4W" Then
            If Application.WorksheetFunction.Match(.Cells(i, 2).Value, .Columns(2), 0) = i Then Call TimeStampWest
        ElseIf .Cells(i, 2).Value = "4E" Then
            If Application.WorksheetFunction.Match(.Cells(i, 2).Value, .Columns(2), 0) = i Then Call TimeStampEast
        ElseIf .Cells(i, 2).Value = "CCU" Then
            If Application.WorksheetFunction.Match(.Cells(i, 2).Value, .Columns(2), 0) = i Then Call TimeStampCCU
        End If
    Next i
End With

End Sub
 
Upvote 0

Forum statistics

Threads
1,223,908
Messages
6,175,305
Members
452,633
Latest member
DougMo

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