Macro Recorder to write Macro - Determine a string within a string

Peteor

Board Regular
Joined
Mar 16, 2018
Messages
152
I have the following code (from the macro recorder)

Sub Macro3()


Range("A1").Select
ActiveCell.FormulaR1C1 = _
"blahisfgkjdfgvhdzlghfdzgkljdfzghkldfzghdkgh[String to Find]xfkdfhgfdkghfcklgfdlogjud"
Range("F6").Select
ActiveSheet.Paste


End Sub


When I recorded the macro I double clicked cell A1, highlighted "String to Find", and pressed CTRL+C. When I run this macro it pastes "String to Find" to cell F6.

Now though, I need to edit the code to make it work for my purposes. I tried the following:

Sub Macro3()


Range("A1").Select
Contents = Cell.Value
ActiveCell.FormulaR1C1 = Contents
Range("F6").Select
ActiveSheet.Paste


End Sub



As one would imagine, it pastes "blahisfgkjdfgvhdzlghfdzgkljdfzghkldfzghdkgh[String to Find]xfkdfhgfdkghfcklgfdlogjud" to cell F6.

The only difference I know of in the code is the underscore at the beginning of the string for the macro recorder version (line 4). All I would like to do is read in "String to Find" from selected cell A1 to a variable. Does anyone have any assistance here?
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
If the string is always between[] try
Code:
Sub Peteor()
   Range("F6").Value = Split(Split(Range("A1").Value, "[")(1), "]")(0)
End Sub
 
Upvote 0
It is always between []. I missed just a small tid-bit in the explanation, which is causing an error for my application. I need to make this work if the [String to Find] is part of a formula within the cell. I see how this works perfectly if Cell.Value is a string (as I stated). So cell A1 would actually look like:

=IF(B1="blahisfgkjdfgvhdzlghfdzgkljdfzghkldfzghdkgh[String to Find]xfkdfhgfdkghfcklgfdlogjud", "SWEET!","UGH")
 
Upvote 0
In that case try
Code:
Sub Peteor()
   Range("F6").Value = Split(Split(Range("A1").Formula, "[")(1), "]")(0)
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,904
Messages
6,175,295
Members
452,631
Latest member
a_potato

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