VBA code: copy string from right up to first space

thedogg

Board Regular
Joined
Sep 22, 2015
Messages
154
I am looking for the code which will extract the string from right side of the worksheet name up to first space in entire string and paste it in cell B11.

Example of the string (worksheet name) : my sheet.1 - 31111111C --------> result in cell B11 should be 31111111C
for that I have code below and it works:
Code:
targetWorkbook.Worksheets(ii).Range("B11").value = Right(TargetSheet.name, 9)

but if there is more than 9 digits from the right side up to the first space I have no idea.
but it doesnt work in that case
Code:
targetWorkbook.Worksheets(ii).Range("B11").value = Right(TargetSheet.name, InStr(TargetSheet.name, " - "))

I need a code who will works for all casses, doesnt mater how many digits I will have from the right side up to first space.
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Try
Code:
Right(TargetSheet.Name, Len(TargetSheet.Name) - InStrRev(TargetSheet.Name, " "))
 
Upvote 0
Another option:
Code:
targetWorkbook.Worksheets(ii).Range("B11").Value = Split(TargetSheet.Name)(UBound(Split(TargetSheet.Name)))
 
Upvote 0

Forum statistics

Threads
1,223,908
Messages
6,175,307
Members
452,633
Latest member
DougMo

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