Assigning cell contents after specific text to a variable

akg742

New Member
Joined
Mar 13, 2014
Messages
39
I have 2 issues. First, I need to identify the text after " - " in worksheet1 A1 (this cell is always Title - "local site" with "local site" changing based on where the user is). The formula I thought was working (Right(rng, InStr(rng, " ") - 6).Value) grabbed the last 3 digits (my site is ABC) on the right but the length of the site will vary.

Once I have that, I need to use it as part of the subject for an email I want to write using a macro: site " summary " date (for example, ABC Summary April 26)

Dim subj As String
Dim site As String
Dim rng As Range

Sheets("worksheet1").Select
Set rng = Range("A1")

site = Right(rng, InStr(rng, " ") - 6).Value ' this formula won't work for all sites. For now, it returns "ABC"

DateDM = Format(Date, "MMMM DD")

subj = site & " Summary - " & DateDM

When I run the code to start my email, my Subject starts with Summary and nothing I've tried adds on the site.
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Try his:

The code will get what is after the "-" regardless of the length.
VBA Code:
Sub test()
  Dim site As String
  site = Split(Sheets("worksheet1").Range("A1").Value, "-")(1)
End Sub

--------------
Let me know the result and I'll get back to you as soon as I can.
Sincerely
Dante Amor
--------------
 
Upvote 1
Solution
Try his:

The code will get what is after the "-" regardless of the length.
VBA Code:
Sub test()
  Dim site As String
  site = Split(Sheets("worksheet1").Range("A1").Value, "-")(1)
End Sub

--------------
Let me know the result and I'll get back to you as soon as I can.
Sincerely
Dante Amor
--------------
That worked! Thank you so much
 
Upvote 1

Forum statistics

Threads
1,215,159
Messages
6,123,345
Members
449,097
Latest member
thnirmitha

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