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:
Text-1Text-2Text-3Text-4Text-5

<tbody>
</tbody>

After code:
Text12345

<tbody>
</tbody>




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

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
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,215,139
Messages
6,123,264
Members
449,093
Latest member
Vincent Khandagale

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