Help with this line of macro code

chuckles1066

Banned
Joined
Dec 20, 2004
Messages
372
Hope someone can help, here's the code:
Code:
If Left(Cells(myrowcounter, 1).Value, 20) = "OUTSTANDING PPM FOR:" Then
            workmannamestart = Right(Cells(myrowcounter, 1).Value, Len(Cells(myrowcounter, 1).Value) - 21)
            workmanname = Left(workmannamestart, 25)

This code then goes on to save various files with the filename workmanname.

And it all works well except there are trailing blank spaces (the original file that the data came from was a text file which may explain it).

I've tried to tidy it by adding the workmanname variable (25 is the longest length of any of the files) but it would be handy to be able to save straightaway without any spaces in the filename.

Something to look at the value of workmannamestart and work out at which position the last character is?

Thanks in advance.
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Code:
If Left(Cells(myrowcounter, 1).Value, 20) = "OUTSTANDING PPM FOR:" Then
            workmannamestart = Right(Cells(myrowcounter, 1).Value, Len(Cells(myrowcounter, 1).Value) - 21)
            workmanname = TRIM(workmannamestart)

Does this work?
</pre>
 
Upvote 0
Code:
If Left(Cells(myrowcounter, 1).Value, 20) = "OUTSTANDING PPM FOR:" Then
            workmannamestart = Right(Cells(myrowcounter, 1).Value, Len(Cells(myrowcounter, 1).Value) - 21)
            workmanname = TRIM(workmannamestart)

Does this work?
</pre>

I will try it in the morning, many thanks :-)
 
Upvote 0
I don't think VBA has a native "Proper" function. There is "UCase" & "LCase" for all upper or lower case.

You could use the "WorksheetFunction" like this:

Code:
Public Sub Test()
 
Dim sName As String
 
sName = "JOHN SMITH"
sName = Application.WorksheetFunction.Proper(sName)
 
MsgBox sName
 
End Sub

Of course the "WorksheetFunction" would not work in other VBA enabled applications like AutoCAD because it is not a native VBA function.

For your other problem you may have to use various combinations of "Trim", "Replace", "Split" etc to massage your string exactly the way you want it. Maybe replace 2 spaces with 1 space then trim etc.

Gary
 
Upvote 0

Forum statistics

Threads
1,224,507
Messages
6,179,181
Members
452,893
Latest member
denay

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