VBA TextBox Value to Range - vba

I'm struggling to do what I believe is a simple task
I have a textbox on a user form that contains a numeric value which I wish to be used as a row number reference in a range.
Sheets("Sheet1").Range("A" & TextBox1.Value).Select
This fails with the error "Select Method of Range Class Failed"
How do I convert these values into a range?

You should look to use error handling for this
Sub Recut()
Dim rng1 As Range
If IsNumeric(textbox1.Value) Then
On Error Resume Next
Set rng1 = Sheets("Sheet1").Range("A" & textbox1.Value)
On Error GoTo 0
If rng1 Is Nothing Then MsgBox "A" & textbox1.Value & " is invalid"
Else
MsgBox "Textbox does not contain a number"
End If
End Sub

Related

Find cell containing greater 255 characters

My code below works perfectly to find a cell on a different worksheet when the string is small, however large text strings pull up an error. I have tried using error handling even just to give a MsgBox rather than open a VBA window when it errors.
Can anyone help, preferably find the cell with many characters or if not possible, put an error handler in to say something like, too large to search.
What the code does, is a have a range of cells with text in each cell. I can click on that cell, or a cell 2 columns to the right, then click the FIND button, to go in the next worksheet to find the exact same cell value. All cells are unique.
Sub Find_Cell()
Dim NA As Worksheet
Set NA = Worksheets("Notes Analysis")
LastRow = NA.Cells(Rows.Count, 2).End(xlUp).Row
On Error Resume Next
If Not Intersect(ActiveCell, Range("G19:G" & LastRow)) Is Nothing Then
Dim value As String 'Declare a string
value = ActiveCell.Offset(, -2) 'Get the value of the selected Cell
Dim ws As Worksheet
'ws is the worksheet from we are searching the value
'You have to change myWorkSheetName for you worksheet name
Set ws = ThisWorkbook.Worksheets("DEBT_SALE_ACTIVITY")
ws.Activate
Dim c As Range 'Declare a cell
Set c = ws.Cells.Find(value, LookIn:=xlValues) 'Search the value
If Not c Is Nothing Then 'If value found
c.Activate 'Activate the cell, select it
Else
MsgBox "Not found" 'shows a message "Not Found"
End If
Else
If Not Intersect(ActiveCell, Range("E19:E" & LastRow)) Is Nothing Then
Dim value2 As String 'Declare a string
value2 = ActiveCell 'Get the value of the selected Cell
Dim ws2 As Worksheet
'ws is the worksheet from we are searching the value
'You have to change myWorkSheetName for you worksheet name
Set ws2 = ThisWorkbook.Worksheets("DEBT_SALE_ACTIVITY")
ws2.Activate
Dim c2 As Range 'Declare a cell
Set c2 = ws2.Cells.Find(value2, LookIn:=xlValues) 'Search the value
If Not c2 Is Nothing Then 'If value found
c2.Activate 'Activate the cell, select it
Else
MsgBox "Not found" 'shows a message "Not Found"
End If
Else
MsgBox "Select an Account Note"
End If 'end the If for if active cell is in our notes
End If 'end the If for if active cell is in Account note
End Sub
To provide an error message indicating the text is too long you could do the following:
Add this after each statement where you assign value its value:
value = ActiveCell.Offset(, -2) 'Get the value of the selected Cell
If Len(value) > 255 Then
MsgBox "Text in cell " & CStr(ActiveCell.Address) & " is too long", vbOKOnly, "Search Text Too Long"
Exit Sub
End If
Also, you might want to change your if...then...else code structure.
Currently your code is operating like this:
If Not Intersect(ActiveCell, Range("G19:G" & LastRow)) Is Nothing Then
do things
exit sub
Else
If Not Intersect(ActiveCell, Range("E19:E" & LastRow)) Is Nothing Then
do things
exit sub
Else
MsgBox "Select an Account Note"
exit sub
Which, based on your comments for your End If's isn't exactly what your message box says. If your first if statement is Account Notes and your second if statement is notes, then a better structure would be the following.
Change this code
Else
If Not Intersect(ActiveCell, Range("E19:E" & LastRow)) Is Nothing Then
To look like this
ElseIf Not Intersect(ActiveCell, Range("E19:E" & LastRow)) Is Nothing Then
Then the statement `MsgBox "Select an Account Note" will be accurate. You also be able to delete one of your End If statements.
Your code will operate like this:
If Not Intersect(ActiveCell, Range("G19:G" & LastRow)) Is Nothing Then
do things
exit sub
ElseIf Not Intersect(ActiveCell, Range("E19:E" & LastRow)) Is Nothing Then
do things
exit sub
Else
MsgBox "Select an Account Note"
exit sub

how to get user to input a range from a dialog box

so i need to get a range from the user, how is it possible to query the user to select a range, something like"
dim x as range
x = getrange("Select Range to Compare")
msgbox "The range selected is " & x
is there a way to do this?
You may try something like this. Tweak it as per your requirement.
Sub AskUserToSelectARangeToWorkWith()
Dim Rng As Range
On Error Resume Next
Set Rng = Application.InputBox("Select a Range to compare.", "Select A Range!", Type:=8)
If Rng Is Nothing Then
MsgBox "You didn't select a Range.", vbCritical, "No Range Selected!"
Exit Sub
End If
MsgBox "The Range selected is " & Rng.Address(0, 0)
End Sub

use inputbox as excel range

I'd like the user to input a range of cells such as A1:Z26. I've tried adding quotations, I've tried have 2 inputboxes, one for beginning and end of the range. But it errors out everytime with: 'method range of object_global failed'
I know it's a simple syntax issue (I think) so can anyone point me in the right direction in terms of how to have the user input a range that works in the set rng = range(msg)
Sub iterationLoop()
Dim rng As Range, iteration As Range
msg = "What is the range you'd like to look at: (e.g. A1:B2)"
InputBox (msg)
Set rng = Range(msg)
For Each iteration In rng
iteration.Select
If iteration = vbNullString Then
iteration = "add value"
MsgBox ("Cell: " & Selection.Address & " has no value")
End If
Next
End Sub
Application.InputBox allows you to specify the input type. Type 8 corresponds to a range. This will allow the user to either select the range with a mouse or type it in manually:
Sub test()
Dim rng As Range
Set rng = Application.InputBox("Select by mouse or enter (e.g. A1:B2) the range you'd like to look at:", Type:=8)
MsgBox rng.Address
End Sub
If you intend your code to be used by others, you should probably wrap the Application.InputBox call in some error-handling code since the above code raises a run-time error if the user presses Cancel. Something like:
On Error Resume Next
Set rng = Application.InputBox("Select by mouse or enter (e.g. A1:B2) the range you'd like to look at:", Type:=8)
If Err.Number > 0 Then
MsgBox "No Range Selected"
Exit Sub
End If
On Error GoTo 0
(though you might want to do something more useful than just quitting the sub)
aAdd
Dim rngstr as string
Then with the inputbox use this:
rngstr = inputbox(msg)
set rng = Range(rngstr)

excel to create a new sheet for each row

The Data
Sheet one will be for data entry. Each row will represent a Service Ticket. Each column will represent data about the service incident such as serial number or model number.
Desired Result
For each row containing data in a particular field (Column A ~ "Ticket Number) Excel will create a new sheet (The service ticket) based on a template and place the data from the corresponding row into the designated cells.
Thank you in advance for any assistance you may be able to provide.
I'll start by saying that you should be careful with this as there is a limit to the number of sheets in a workbook. But here is some code in vb. It should give you the logic to get it done in vba. There will just be some difference in referring to the sheet and maybe cells.
You will need to declare the worksheet that you are reading through
Dim ws As Excel.Worksheet
Set ws = ea.Worksheets(1)
It may start at sheet index 0 so Set ws = ea.Worksheets(0)
Or there is something like Excel.Application.Activsheet
Here is the logic to loop through the rows and check the value of column A.
dim lRow as integer
Do While lRow <= ws.UsedRange.Rows.Count
If ws.Range("A" & lRow).Value <> "" Then Then
'If cell A is not blank we then call the worksheet add function.
'Pass the name you want the worksheet and the page setup arguments.
WorksheetAdd ws.Range("A" & lRow).Value, xlPaperLetter, xlPortrait
ws.name = ws.Range("A" & lRow).Value
End If
lRow = lRow + 1
ws.Range("A" & lRow).Activate
Loop
You will want a worksheetAdd function like this
Private Sub WorksheetAdd(szJobNumber As String, Papersize As XlPaperSize, PageOrientation As XlPageOrientation)
Dim bDisplayAlerts As Boolean
On Error GoTo ErrorHandler
'Add worksheet to workbook.
Set ws = ea.Worksheets.Add
ws.Name = szJobNumber
With ws.PageSetup
.Orientation = PageOrientation
.LeftFooter = "&D"
.CenterFooter = "&A"
.RightFooter = "Page &P of &N"
.Papersize = Papersize
End With
On Error GoTo 0
Exit Sub
ErrorHandler:
If Err.Number = 1004 Then
If MsgBox("There has been an error(#1004). Contact support. Excel is not installed or produced an error. Also, check for default printer.",vbCritical, "Information") = vbOK Then
'Unload frmPTReports
Exit Sub
End If
Else
Err.Raise Err.Number, Err.Source, Err.Description
End If
End Sub

excel VBA - return cell value that includes the escaping apostrophe

Given two cells with contents '=Foo and =Foo, getting the properties .Value,.Value2, .Text, .Formula all give me =Foo for both ranges.
How do I include the escaping apostrophe so that I get '=Foo when returning the cell value?
Use .PrefixCharacter to get the hidden apostrophe.
Like this: Debug.Print Range("A1").PrefixCharacter
Depending on what your trying to do with it, this Microsoft Post might be helpful.
Full Example:
Sub test()
Dim wks As Worksheet
Set wks = Worksheets("Sheet1")
Dim rng As Range
Set rng = wks.Range("A1")
If Not rng.HasFormula Then
If rng.PrefixCharacter <> "" Then
MsgBox "Cell value is: " & rng.PrefixCharacter & rng.Text
Else
MsgBox "No prefix value in cell " & rng.Address
End If
End If
End Sub