removing characters at end to save sheet based off cells in the sheet

B-Man

Board Regular
Joined
Dec 29, 2012
Messages
185
Office Version
  1. 2019
Platform
  1. Windows
I currently use this code below to grab data from my sheet to save the file. sometimes though C4 doesn't have 6 characters at the end I need to remove.
Is there a formula that will remove all the characters after the last letter... ie numbers, period and %? Sometimes there is no numbers or letter or they can change from a space and 3 characters to a space and 5 characters
ie. "2020 Simon 100%" vs "2020 Simon 80%" vs "2020 Simon 60.5%"

VBA Code:
If answer = vbYes Then
thisfile = ThisWorkbook.Path & Application.PathSeparator & "Batch " & Range("C3").Value & " (" & Left(Range("C4"), Len(Range("C4")) - 6) & ") " & Range("c5").Value & " L"
ActiveWorkbook.SaveAs Filename:=thisfile
End If
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
In your examples you are removing everything after the last space, are there any times when this isn't the case?
 
Upvote 0
Something like this will trim C4 to the last letter:
VBA Code:
For x = Len(Range("C4")) To 1 Step -1
    If Mid(Range("C4"), x, 1) Like "[A-Za-z]" Then name = Left(Range("C4"), x): Exit For
Next x
If answer = vbYes Then
    thisfile = ThisWorkbook.Path & Application.PathSeparator & "Batch " & Range("C3").Value & " (" & name & ") " & Range("C5").Value & " L"
    ActiveWorkbook.SaveAs Filename:=thisfile
End If
 
Upvote 0
Solution
Something like this will trim C4 to the last letter:
VBA Code:
For x = Len(Range("C4")) To 1 Step -1
    If Mid(Range("C4"), x, 1) Like "[A-Za-z]" Then name = Left(Range("C4"), x): Exit For
Next x
If answer = vbYes Then
    thisfile = ThisWorkbook.Path & Application.PathSeparator & "Batch " & Range("C3").Value & " (" & name & ") " & Range("C5").Value & " L"
    ActiveWorkbook.SaveAs Filename:=thisfile
End If
Thanks

I moved this line to the top of the code
VBA Code:
If answer = vbYes Then
and had to add
VBA Code:
dim Name as String
. Works perfectly. Cheers!
 
Upvote 0
Yep, I left you the homework ;).
Thanks for the positive feedback (y), glad having been of some help.
 
Upvote 0

Forum statistics

Threads
1,224,600
Messages
6,179,833
Members
452,947
Latest member
Gerry_F

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