VBA Code - Trim function

KatKitKat

New Member
Joined
Apr 27, 2022
Messages
33
Office Version
  1. 365
Platform
  1. Windows
Hi there, I need some help with the trim function in a code.

I have a drop-down validation list in Excel with a 5 or 6 character number, hyphen, then text (see sample list below). I added the following code to the worksheet tab because I want to trim anything from the hyphen to the right. Basically, I only want the number to show. Note that number codes are 5 or 6 characters long.

Using the VBA code that I have shown below, when making the selection (using the first item in the list for example), instead of "00890" appearing in the cell, I get "890". Any ideas on how to correct the code or change my data to make this work? Thanks, Kat


Sample Listing for drop down
1652105300341.png


VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
   Dim strInput As String
   
   'column F
   If Target.Column <> 6 Then Exit Sub
   If Target.Value = "" Then Exit Sub
   
   'find the hyphen
      strInput = Trim(Mid(Target.Value, 1, Application.Find("-", Target.Value) - 1))
   
   'disable events before writing to worksheet
   On Error Resume Next
   Application.EnableEvents = False
      Target.Value = strInput
   Application.EnableEvents = True
End Sub
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
For example:
VBA Code:
Target.Value = chr(39) & strInput

Or format the cell as Number /Customized, type=0000
 
Upvote 0
How about
VBA Code:
   Application.EnableEvents = False
      Target.NumberFormat = "@"
      Target.Value = strInput
   Application.EnableEvents = True
 
Upvote 0
Solution
How about
VBA Code:
   Application.EnableEvents = False
      Target.NumberFormat = "@"
      Target.Value = strInput
   Application.EnableEvents = True
This worked but created another issue: I have vlookup formulas in other cells based on this cell (which is also why I needed the number format to be correct). The vlookups work when I select an item from the list. However, the first option in the selection list is a blank (empty, no data) and when the blank is selected, the fields with the vlookup formulas are producing a #N/A. The blank or empty option is necessary for the blank rows.
 
Upvote 0
As that is a different question, it needs a new thread. Thanks
 
Upvote 0

Forum statistics

Threads
1,223,885
Messages
6,175,184
Members
452,615
Latest member
bogeys2birdies

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