VBA Code - Trim function

KatKitKat

New Member
Joined
Apr 27, 2022
Messages
30
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

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.

Anthony47

Well-known Member
Joined
Mar 29, 2006
Messages
2,985
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
For example:
VBA Code:
Target.Value = chr(39) & strInput

Or format the cell as Number /Customized, type=0000
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
77,590
Office Version
  1. 365
Platform
  1. Windows
How about
VBA Code:
   Application.EnableEvents = False
      Target.NumberFormat = "@"
      Target.Value = strInput
   Application.EnableEvents = True
 
Solution

KatKitKat

New Member
Joined
Apr 27, 2022
Messages
30
Office Version
  1. 365
Platform
  1. Windows
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.
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
77,590
Office Version
  1. 365
Platform
  1. Windows
As that is a different question, it needs a new thread. Thanks
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
77,590
Office Version
  1. 365
Platform
  1. Windows
You're welcome.
 

Forum statistics

Threads
1,175,738
Messages
5,899,182
Members
434,754
Latest member
veersalaria

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
Top