VBA for Case with Like must have wildcard

Livin404

Well-known Member
Joined
Jan 7, 2019
Messages
774
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Good evening, I start out with a formula then determined it would not be efficient since I did not want the end values in my "Result" Column.

This VBA is set directly into Sheet 1, we need it to update automatically.

The following code I think is getting me in the right direction. I do see ////KG being placed in the first one; however, my Excel then quickly shuts done. I also need to add one line that the only time this macro works is when it meets the "TE*" requirement. I think I'll need an else option.

I added the Excel to the Dropbox: Worksheet VBA DROPBOX.xlsm


My code now is


VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

Dim rng As Range
Dim result As String
For Each rng In Range("A1:A" & Cells(Rows.Count, 1).End(xlUp).Row)
Select Case rng.Value
Case rng.Value Like "TE*"
result = "///KG"
End Select
rng.Offset(0, 2).Value = result
Next
End Sub

Thank you very much indeed.
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
See if this works any better:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Range("A:A"), Target) Is Nothing Then
        On Error GoTo Escape
        Application.EnableEvents = False
        Application.ScreenUpdating = False
            Dim r As Range
            For Each r In Range("A1:A" & Cells(Rows.Count, 1).End(xlUp).Row)
                If r Like "TE*" Then
                    r.Offset(0, 2).Value = "///KG"
                End If
            Next r
    End If
Continue:
    Application.EnableEvents = True
    Application.ScreenUpdating = True
    Exit Sub
Escape:
    MsgBox "Error " & Err.Number & ": " & Err.Description
    Resume Continue
End Sub
 
Upvote 1
Solution
See if this works any better:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Range("A:A"), Target) Is Nothing Then
        On Error GoTo Escape
        Application.EnableEvents = False
        Application.ScreenUpdating = False
            Dim r As Range
            For Each r In Range("A1:A" & Cells(Rows.Count, 1).End(xlUp).Row)
                If r Like "TE*" Then
                    r.Offset(0, 2).Value = "///KG"
                End If
            Next r
    End If
Continue:
    Application.EnableEvents = True
    Application.ScreenUpdating = True
    Exit Sub
Escape:
    MsgBox "Error " & Err.Number & ": " & Err.Description
    Resume Continue
End Sub
Thank you for the quick response. I feel bad, because mine was nots even close. I notice we have a dialog box. Does that come up when there is no TE at all?
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,853
Members
452,361
Latest member
d3ad3y3

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