Message box for user input to run macro on a range VBA

seattletimebandit

Board Regular
Joined
Apr 11, 2013
Messages
69
Hello!

I have a bit of code that manipulates data starting in a specific cell. Trying to figure out how to do this by asking the user to select the range, not matter where the table of data is located. The code I have now:

Code:
Sub SplitDepthsFromSampleID()
  Dim ID As String
  ID = Left(Range("B1").Value, InStrRev(Range("B1").Value, "-") - 1)
  Range("A1").Value = ID
  Range("B1", Cells(1, Columns.Count).End(xlToLeft)).Replace ID & "-", "", xlPart, , , , False
End Sub

Before code:
[TABLE="class: grid, width: 370"]
<tbody>[TR]
[TD="width: 74"][/TD]
[TD="width: 74"]Text-1[/TD]
[TD="width: 74"]Text-2[/TD]
[TD="width: 74"]Text-3[/TD]
[TD="width: 74"]Text-4[/TD]
[TD="width: 74"]Text-5[/TD]
[/TR]
</tbody>[/TABLE]

After code:
[TABLE="class: grid, width: 370"]
<tbody>[TR]
[TD]Text[/TD]
[TD]1[/TD]
[TD]2[/TD]
[TD]3[/TD]
[TD]4[/TD]
[TD]5[/TD]
[/TR]
</tbody>[/TABLE]




I'm working on it, and have the Msg Box bit for selecting the range, but not sure how to run the macro on the selection. There would only be 1 row, but number of columns is variable.

Thanks!

Russell
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
How about
Code:
Sub SplitDepthsFromSampleID()
  Dim ID As String
  Dim Rng As Range
  
  Set Rng = Application.InputBox("Please select the range", Default:="$A$1", Type:=8)
  ID = Split(Rng.Resize(1, 1), "-")(0)
  Rng.Offset(, -1).Resize(1, 1) = ID
  Rng.Replace ID & "-", "", xlPart, , , , False
End Sub
 
Upvote 0
Fluff, that works great!

But i forgot one thing, sometimes the data will be something like "text-1-2, text-1-5, text-1-10, etc.". Your code worked with the single dash, but when i added the second dash, the result was a date: "2-Jan, 5-Jan, 10-Jan, etc." I need to be able to strip out the second dash as well. My static macro did that, but for some reason your code doesn't replace the "-" with a "". Thoughts?
 
Upvote 0
Ok, how about
Code:
Sub SplitDepthsFromSampleID()
  Dim ID As String
  Dim Rng As Range
  
  Set Rng = Application.InputBox("Please select the range", Default:="$A$1", Type:=8)
  ID = Left(Rng.Resize(1, 1), InStrRev(Rng.Resize(1, 1), "-") - 1)
  Rng.Offset(, -1).Resize(1, 1) = ID
  Rng.Replace ID & "-", "", xlPart, , , , False
End Sub
 
Upvote 0
Glad to help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,175
Members
453,021
Latest member
Justyna P

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