Get only text from Inputbox value

gssachin

Board Regular
Joined
Nov 14, 2013
Messages
155
Hi,

I want to enter the cell number and later same will get separated in character (i.e. BA5 it will get BA or if B5 then will get "B" ) But I m getting an error for the below codes due to "{"

Basically, I want the user will give a Cell reference based on that cell first and last row will get indentify.
my old code was "
"For Each locationCell In .Range("A2:A" & .Cells(.Rows.Count, "A").End(xlUp).Row)"

cellref = InputBox("Enter cell ref here")
newfilename = InputBox("Enter New File name")
For Each locationCell In .Range((cellref) & ":" & (Left(cellref, MIN(FIND({0,1,2,3,4,5,6,7,8,9},cellref &"0123456789"))-1)) & .Cells(.Rows.Count, (Left(cellref, MIN(FIND({0,1,2,3,4,5,6,7,8,9},cellref&"0123456789"))-1))).End(xlUp).Row)
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Hi gssachin,

maybe try

VBA Code:
Sub MrE1221260()
Dim rngStart As Range
Dim rngCell As Range
Dim strNewFile As String
'....

'...

With ActiveSheet
  Set rngStart = Application.InputBox("Enter cell ref here", "Start Cell", Type:=8)
  strNewFile = Application.InputBox("Enter New File name", "New File", Type:=2)
  
  For Each rngCell In .Range(rngStart, .Cells(.Rows.Count, rngStart.Column).End(xlUp))
    '
  Next rngCell
End With
'...

Set rngStart = Nothing
End Sub

Ciao,
Holger
 
Upvote 0
Hi gssachin,

maybe try

VBA Code:
Sub MrE1221260()
Dim rngStart As Range
Dim rngCell As Range
Dim strNewFile As String
'....

'...

With ActiveSheet
  Set rngStart = Application.InputBox("Enter cell ref here", "Start Cell", Type:=8)
  strNewFile = Application.InputBox("Enter New File name", "New File", Type:=2)
 
  For Each rngCell In .Range(rngStart, .Cells(.Rows.Count, rngStart.Column).End(xlUp))
    '
  Next rngCell
End With
'...

Set rngStart = Nothing
End Sub

Ciao,
Holger
No sir it's not working....
 
Upvote 0
Hi gssachin,

could you please be more specific about "it's not working": what is it you want the code to do, what isn't delivered? The code sniplet supplied should deliver a range-object for the start cell from which you can refer to the column for the whole range.

Ciao,
Holger
 
Upvote 0
Sir have macro which filter the specific column based on input given (cell reference) by user and then copy data to another sheet and also saving that sheet as new file in a specific folder...

I want replace A2 to user input (cell ref)

For Each locationCell In .Range("A2:A" & .Cells(.Rows.Count, "A").End(xlUp).Row)"
I above line A2 can be changed as input value but "A" should get replaced with charecter only....
Hi gssachin,

could you please be more specific about "it's not working": what is it you want the code to do, what isn't delivered? The code sniplet supplied should deliver a range-object for the start cell from which you can refer to the column for the whole range.

Ciao,
Holger
 
Upvote 0
Hi gssachin,

if you think you need the column character(s) for building a range to loop this should do the trick:

VBA Code:
Sub MrE1221260_V2()
Dim rngStart As Range
Dim locationCell As Range
Dim varNewFile As Variant
Dim strColLetter As String
'....

'...

With ActiveSheet
  On Error Resume Next
  Set rngStart = Application.InputBox("Choose the starting cell in the sheet or enter address like 'A2'", "Start Cell", Type:=8)
  If Err.Number <> 0 Then
    MsgBox "No proper selection to start with", vbInformation, "Exit procedure"
    GoTo end_here
  End If
  On Error GoTo 0
  strColLetter = Left(.Cells(1, rngStart.Column).Address(0, 0), Len(.Cells(1, rngStart.Column).Address(0, 0)) - 1)
  MsgBox strColLetter
  varNewFile = Application.InputBox("Enter New File name", "New File", Type:=2)
  If varNewFile = False Then
    MsgBox "No new file name entered", vbInformation, "Exit procedure"
    GoTo end_here
  End If
  
  For Each locationCell In .Range(.Cells(2, strColLetter), .Cells(.Rows.Count, strColLetter).End(xlUp))
'  For Each locationCell In .Range(.Cells(2, rngStart.Column), .Cells(.Rows.Count, rngStart.Column).End(xlUp))
'
  Next locationCell
End With
'...

end_here:
Err.Clear
Set rngStart = Nothing
End Sub

Ciao,
Holger
 
Upvote 0
Hi gssachin,

if you think you need the column character(s) for building a range to loop this should do the trick:

VBA Code:
Sub MrE1221260_V2()
Dim rngStart As Range
Dim locationCell As Range
Dim varNewFile As Variant
Dim strColLetter As String
'....

'...

With ActiveSheet
  On Error Resume Next
  Set rngStart = Application.InputBox("Choose the starting cell in the sheet or enter address like 'A2'", "Start Cell", Type:=8)
  If Err.Number <> 0 Then
    MsgBox "No proper selection to start with", vbInformation, "Exit procedure"
    GoTo end_here
  End If
  On Error GoTo 0
  strColLetter = Left(.Cells(1, rngStart.Column).Address(0, 0), Len(.Cells(1, rngStart.Column).Address(0, 0)) - 1)
  MsgBox strColLetter
  varNewFile = Application.InputBox("Enter New File name", "New File", Type:=2)
  If varNewFile = False Then
    MsgBox "No new file name entered", vbInformation, "Exit procedure"
    GoTo end_here
  End If
 
  For Each locationCell In .Range(.Cells(2, strColLetter), .Cells(.Rows.Count, strColLetter).End(xlUp))
'  For Each locationCell In .Range(.Cells(2, rngStart.Column), .Cells(.Rows.Count, rngStart.Column).End(xlUp))
'
  Next locationCell
End With
'...

end_here:
Err.Clear
Set rngStart = Nothing
End Sub

Ciao,
Holger
Sir,

It works the way I want but I have the following macro where I have to incorporate the above, which I m not able to do, can u guide me on this
In the below mail highlighted line I want to change, where if the user selects BA2 then "rowcount" should take as "AB" instead of "A" only)

Also, I m impressed with how you used to stop the Input box in the above macro. When I tried the same it get the error "Lable not found", Please help me on that also

Thanks in advance....

Public Sub Split_Sheet_By_Location()

Dim saveInFolder As String
Dim locations As Collection
Dim locationCell As Range, locationKey As Variant
Dim locationSheet As Worksheet

saveInFolder = InputBox("Enter the Folder Name (path i.e. C:\Users\10600740\Desktop\New folder ) where you want to save Files")
If Right(Trim(saveInFolder), 1) <> "\" Then saveInFolder = Trim(saveInFolder) & "\"
Shname = InputBox("Enter sheet Name")
Application.ScreenUpdating = False

With ThisWorkbook.Worksheets(Shname)

'Create collection of unique locations from column A

Set locations = New Collection
On Error Resume Next
cellref = InputBox("Enter cell ref here")
newfilename = InputBox("Enter New File name")

For Each locationCell In .Range((cellref) & ":" & (Left(cellref, 1)) & .Cells(.Rows.Count, (Left(cellref, 1))).End(xlUp).Row)

locations.Add locationCell.Value, CStr(locationCell.Value)
Next
On Error GoTo 0

'Autofilter column A by each location and copy results to location sheet

For Each locationKey In locations

Set locationSheet = Get_Sheet(ThisWorkbook, CStr(locationKey))
.UsedRange.AutoFilter Field:=Range(cellref).Column, Operator:=xlFilterValues, Criteria1:="=" & locationKey 'Field:=1 means column A
.UsedRange.Copy locationSheet.Range("A1")
ActiveSheet.UsedRange.EntireColumn.AutoFit
Application.DisplayAlerts = False 'suppress warning if .xlsx file already exists - file is replaced
locationSheet.Copy
'ActiveSheet.UsedRange.EntireColumn.AutoFit
ActiveWorkbook.SaveAs saveInFolder & newfilename & "_" & locationKey & ".xlsx"
ActiveWorkbook.Close SaveChanges:=False
Application.DisplayAlerts = True

Next

'Remove autofilter

.UsedRange.AutoFilter
.Activate

End With

Application.ScreenUpdating = True

MsgBox "Done"

End Sub


Private Function Get_Sheet(wb As Workbook, sheetName As String) As Worksheet

Set Get_Sheet = Nothing
With wb
On Error Resume Next
Set Get_Sheet = .Worksheets(sheetName)
On Error GoTo 0
If Get_Sheet Is Nothing Then
Set Get_Sheet = .Worksheets.Add(After:=.Worksheets(.Worksheets.Count))
Get_Sheet.Name = sheetName
Else
Get_Sheet.Cells.Clear
End If
End With

End Function
 
Upvote 0
Hi gssachin,

you should highlight code pasted here and apply code-tags via VBA-button above.

I doubt you will recognize your code in this one... ;)

VBA Code:
Public Sub Split_Sheet_By_Location_mod()
'https://www.mrexcel.com/board/threads/get-only-text-from-inputbox-value.1221260/#post-5972824
  Dim objCollection       As Collection
  Dim strPathNewFile      As String
  Dim strSaveFolder       As String
  Dim rngCell             As Range
  Dim rngStart            As Range
  Dim varKey              As Variant
  Dim varNewFile          As Variant
  Dim wsTargSheet         As Worksheet

  Application.ScreenUpdating = False
  With Application.FileDialog(msoFileDialogFolderPicker)
    .AllowMultiSelect = False
    .Title = "Choose a folder"
    If .Show = -1 Then
      strSaveFolder = .SelectedItems(1)
      If Right(Trim(strSaveFolder), 1) <> "\" Then strSaveFolder = Trim(strSaveFolder) & "\"
    Else
      MsgBox "No folder selected", vbInformation, "Ending..."
      GoTo end_here
    End If
  End With

retry:
  varNewFile = Application.InputBox("Enter Sheet Name", "Sheet Nmae", Type:=2)
  If varNewFile = False Then
    MsgBox "No sheet name entered", vbInformation, "Exit procedure"
    GoTo end_here
  Else
    If Not Evaluate("ISREF('" & varNewFile & "'!A1)") Then
      If MsgBox("Can't find sheet '" & varNewFile & "'." & vbCrLf & "Try Again or Cancel?", vbOKCancel, "Typo?..") = vbYes Then
        GoTo retry
      Else
        GoTo end_here
      End If
    End If
  End If
  With ThisWorkbook.Worksheets(varNewFile)
    If .AutoFilterMode Then .AutoFilterMode = False
    Set objCollection = New Collection
    On Error Resume Next
    Set rngStart = Application.InputBox("Choose the starting cell in the sheet or enter address like 'A2'", "Start Cell", Type:=8)
    If Err.Number <> 0 Then
      MsgBox "No proper selection to start with", vbInformation, "Exit procedure"
      GoTo end_here
    End If
    
    varNewFile = Application.InputBox("Enter New File name", "New File", Type:=2)
    If varNewFile = False Then
      MsgBox "No new file name entered", vbInformation, "Exit procedure"
      GoTo end_here
    End If
    
    For Each rngCell In .Range(.Cells(2, rngStart.Column), .Cells(.Rows.Count, rngStart.Column).End(xlUp))
      objCollection.Add rngCell.Value, CStr(rngCell.Value)
    Next rngCell
    On Error GoTo 0
    
    'Autofilter column A by each location and copy results to location sheet
    For Each varKey In objCollection
      If wsTargSheet Is Nothing Then
        Set wsTargSheet = Worksheets.Add(after:=ThisWorkbook.Worksheets(ThisWorkbook.Worksheets.Count))
      Else
        wsTargSheet.UsedRange.ClearContents
      End If
      wsTargSheet.Name = CStr(varKey)
      If .AutoFilterMode Then .AutoFilterMode = False
      .UsedRange.AutoFilter Field:=rngStart.Column, Operator:=xlFilterValues, Criteria1:="=" & varKey 'Field:=1 means column A
      .UsedRange.Copy wsTargSheet.Range("A1")
      wsTargSheet.UsedRange.EntireColumn.AutoFit
      strPathNewFile = strSaveFolder & varNewFile & "_" & varKey & ".xlsx"
      If Dir(strPathNewFile) <> "" Then Kill strPathNewFile
      wsTargSheet.Copy
      ActiveWorkbook.SaveAs strPathNewFile, FileFormat:=51
      ActiveWorkbook.Close SaveChanges:=False
    Next varKey
  
    'Remove autofilter
    .UsedRange.AutoFilter
    .Activate
  End With
  
  With Application
    .DisplayAlerts = False
    wsTargSheet.Delete
    .DisplayAlerts = True
    .ScreenUpdating = True
  End With
  
  MsgBox "Done"

end_here:
  Err.Clear
  Set wsTargSheet = Nothing
  Set rngStart = Nothing
End Sub

Ciao,
Holger
 
Upvote 0
Hi gssachin,

you should highlight code pasted here and apply code-tags via VBA-button above.

I doubt you will recognize your code in this one... ;)

VBA Code:
Public Sub Split_Sheet_By_Location_mod()
'https://www.mrexcel.com/board/threads/get-only-text-from-inputbox-value.1221260/#post-5972824
  Dim objCollection       As Collection
  Dim strPathNewFile      As String
  Dim strSaveFolder       As String
  Dim rngCell             As Range
  Dim rngStart            As Range
  Dim varKey              As Variant
  Dim varNewFile          As Variant
  Dim wsTargSheet         As Worksheet

  Application.ScreenUpdating = False
  With Application.FileDialog(msoFileDialogFolderPicker)
    .AllowMultiSelect = False
    .Title = "Choose a folder"
    If .Show = -1 Then
      strSaveFolder = .SelectedItems(1)
      If Right(Trim(strSaveFolder), 1) <> "\" Then strSaveFolder = Trim(strSaveFolder) & "\"
    Else
      MsgBox "No folder selected", vbInformation, "Ending..."
      GoTo end_here
    End If
  End With

retry:
  varNewFile = Application.InputBox("Enter Sheet Name", "Sheet Nmae", Type:=2)
  If varNewFile = False Then
    MsgBox "No sheet name entered", vbInformation, "Exit procedure"
    GoTo end_here
  Else
    If Not Evaluate("ISREF('" & varNewFile & "'!A1)") Then
      If MsgBox("Can't find sheet '" & varNewFile & "'." & vbCrLf & "Try Again or Cancel?", vbOKCancel, "Typo?..") = vbYes Then
        GoTo retry
      Else
        GoTo end_here
      End If
    End If
  End If
  With ThisWorkbook.Worksheets(varNewFile)
    If .AutoFilterMode Then .AutoFilterMode = False
    Set objCollection = New Collection
    On Error Resume Next
    Set rngStart = Application.InputBox("Choose the starting cell in the sheet or enter address like 'A2'", "Start Cell", Type:=8)
    If Err.Number <> 0 Then
      MsgBox "No proper selection to start with", vbInformation, "Exit procedure"
      GoTo end_here
    End If
   
    varNewFile = Application.InputBox("Enter New File name", "New File", Type:=2)
    If varNewFile = False Then
      MsgBox "No new file name entered", vbInformation, "Exit procedure"
      GoTo end_here
    End If
   
    For Each rngCell In .Range(.Cells(2, rngStart.Column), .Cells(.Rows.Count, rngStart.Column).End(xlUp))
      objCollection.Add rngCell.Value, CStr(rngCell.Value)
    Next rngCell
    On Error GoTo 0
   
    'Autofilter column A by each location and copy results to location sheet
    For Each varKey In objCollection
      If wsTargSheet Is Nothing Then
        Set wsTargSheet = Worksheets.Add(after:=ThisWorkbook.Worksheets(ThisWorkbook.Worksheets.Count))
      Else
        wsTargSheet.UsedRange.ClearContents
      End If
      wsTargSheet.Name = CStr(varKey)
      If .AutoFilterMode Then .AutoFilterMode = False
      .UsedRange.AutoFilter Field:=rngStart.Column, Operator:=xlFilterValues, Criteria1:="=" & varKey 'Field:=1 means column A
      .UsedRange.Copy wsTargSheet.Range("A1")
      wsTargSheet.UsedRange.EntireColumn.AutoFit
      strPathNewFile = strSaveFolder & varNewFile & "_" & varKey & ".xlsx"
      If Dir(strPathNewFile) <> "" Then Kill strPathNewFile
      wsTargSheet.Copy
      ActiveWorkbook.SaveAs strPathNewFile, FileFormat:=51
      ActiveWorkbook.Close SaveChanges:=False
    Next varKey
 
    'Remove autofilter
    .UsedRange.AutoFilter
    .Activate
  End With
 
  With Application
    .DisplayAlerts = False
    wsTargSheet.Delete
    .DisplayAlerts = True
    .ScreenUpdating = True
  End With
 
  MsgBox "Done"

end_here:
  Err.Clear
  Set wsTargSheet = Nothing
  Set rngStart = Nothing
End Sub

Ciao,
Holger
Dear sir,

You are simply GREAT 😍😘🙏🙏🙏

This works exactly how I want.... Only one humble request I want to keep all sheets in main file also... Is this possible...

Some times I jst want to keep sheets in main file and not save separate file.. BUT if above requirement fulfill then I can delete unwanted files and keep sheets only in main file.
 
Upvote 0
Hi gssachin,

in the code posted there is only worksheet added, used for all items and deleted at the end of the procedure. It's no big deal to change the code to get new worksheets and keep them. That will be okay on the first run of the macro. And if you run the code again it would crush as the renaming of the sheets could fail - sheet already exists. So any worksheet that already exists should be cleared and used again. Question from my side: how many sheets could be added to the workbook and kept in there?

For my part I would keep the workbook with the data and the snapshots from limited data strictly apart. Besides from having a single workbook for each item you could collect the sheets in one workbook. You could add a date and time stamp when saving the single sheets.

Which way do you want to go? And what about

Some times I jst want to keep sheets in main file and not save separate file

Holger
 
Upvote 0

Forum statistics

Threads
1,222,108
Messages
6,163,976
Members
451,867
Latest member
csktwyr

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