Excuse me if you guys see this question again. However, I have searched for similar topics in this site but can't figure out my problem.
I have a VBA code snipet with a line throwing back an error:
Sub test()
Dim rng As Range
Set rng = Application.InputBox("Select range: ", "Select range", Type:=8)
MsgBox (Range("rng").Rows.Count)
End Sub
My intention is to prompt user to select a range and to count number of rows in that range.
If I pre-define the name range "rng" instead of selecting range in run-time like the code below, it will return the number of rows without errors.
Sub test()
Dim rng As Range
MsgBox (Range("rng").Rows.Count)
End Sub
Could someone please tell me the problem with the range selected by user so that it can't return number of rows? Doesn't the "rng" range defined by user have a "Rows" property?
Thank you for your help.
Change:
MsgBox (Range("rng").Rows.Count)
To:
MsgBox rng.Rows.Count
rng is already a valid range and it has it's own Rows propery which you can access like this...
Also, consider to handle the case if user doesn't select any range and hit Cancel on InputBox.
Dim rng As Range
On Error Resume Next
Set rng = Application.InputBox("Select range: ", "Select range", Type:=8)
On Error GoTo 0
If Not rng Is Nothing Then MsgBox rng.Rows.Count
Related
Perhaps I just haven't used the right search terms, I am still new to VBA, but I just can't find the solution to my problem:
I am trying to find a value (format 'yyyy-ww') from one worksheet in the row of another worksheet, and then select the cell (the next step would then be to then select and copy the respective column, and then paste the values).
I have the following code:
Private Sub Button5_Click()
'Define previous week and the search range
Dim prevwk As Object
Dim SrchRng As Range
Set prevwk = ActiveWorkbook.Worksheets("Values").Range("B1")
Set SrchRng = ActiveWorkbook.Worksheets("DE").Rows(1)
'If previous week is found, select the cell
With SrchRng
Dim prevwkf As Range
Set prevwkf = SrchRng.Find(What:=prevwk)
prevwkf.Select '<----- Error is here
End Sub
I keep receiving the error message:
'Run-time error '91': Object variable or With block variable not set'.
I have tried many changes but it keeps coming down to this error message.
Many thanks for your help!
before selecting a cell you have to activate the sheet, just as you would do manually:
SrchRng.parent.Activate
prevwkf.Select
BTW you don't need that With SrchRng, and you could check for actual match found
Private Sub Button5_Click()
'Define previous week and the search range
Dim prevwk As Object
Dim SrchRng As Range
Set prevwk = ActiveWorkbook.Worksheets("Values").Range("B1")
Set SrchRng = ActiveWorkbook.Worksheets("DE").Rows(1)
'If previous week is found, select the cell
Dim prevwkf As Range
Set prevwkf = SrchRng.Find(What:=prevwk)
If Not prevwkf Is Nothing Then ' check you actually found something
SrchRng.Parent.Activate
prevwkf.Select '<----- Error is here
End If
End Sub
I am having trouble creating a macro that will find a specified value in my active sheet within a range in my "Information" sheet. If the cell value us not found in the range then it will give me a message box stating "Value not Found" I have the following but it is not working:
Sub testrot()
Dim i As String
Dim srchrng As Range
Sheets(ActiveSheet.Name).Select
i = Cells(2, 5)
Sheets("Information").Select
Set srchrng = Range("j8:j17").Find(what = i)
If Not srchrng Is Nothing Then
MsgBox "Not Found"
End If
End Sub
The cell (2,5) in my active sheet is for example #16, and in the range (j8:j17) it is a list of different strings #16, #17, etc.
I appreciate any advice.
Thanks.
You want to avoid .Select/.Activate
Sub testRot2()
Dim str As String
Dim srchRng As Range
With Worksheets(ActiveSheet.Name)
str = .Cells(2, 5).Value
Set srchRng = .Range("J8:J17").Find(what:=str)
If srchRng Is Nothing Then
MsgBox "Not found"
End If
End With
End Sub
Also, note that I changed i to str. This is a personal choice, as I typically see i as a Long/Integer for looping. You can keep it i though, just thought to mention it. Also, note the colon required in Find(what:=str).
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)
I've been extensively searching for the answer to this, but no one seems to have an answer for me.
What I want my code to do is simple - I want to remove the background color from a section of cells that the user chooses. This would be triggered when the user presses a button.
My code is:
Private Sub clrhi_Click()
Dim rng As Range
Set rng = Application.InputBox("Select a range from which you wish to remove the highlight", "Select a Range", Type:=8)
Range(rng.Address).Interior.ColorIndex = 0
End Sub
When I run it, it hits an error (Run-time error '1004': Application-defined or object-defined error) on the last line right above End Sub.
I've also tried:
Private Sub clrhi_Click()
Dim rng As Range
Set rng = Application.InputBox("Select a range from which you wish to remove the highlight", "Select a Range", Type:=8)
rng.Interior.ColorIndex = 0
End Sub
and get the same error in the same place.
The last bit I tried was:
Private Sub clrhi_Click()
Dim rng As Range
Set rng = Application.InputBox("Select a range from which you wish to remove the highlight", "Select a Range", Type:=8)
Range(rng).Interior.ColorIndex = 0
End Sub
and I get a different error (Method 'Range' of object '_Worksheet' failed).
Can anyone help me out on this one? Much appreciated.
Refer to this question: Let the user click on the cells as their input for an Excel InputBox using VBA.
I use this line of code Set rng = Application.InputBox(Prompt:="Select the cell you want to edit.", Title:="CELL TO EDIT", Type:=8) to ask the user to select a cell. If the cell that is selected is in column G then I will need to call a subroutine. So, I need to figure out what column the selected cell is in.
Thanks for the help!
Use rng.Column property to return column number:
Sub test()
Dim rng As Range
On Error Resume Next
Set rng = Application.InputBox(Prompt:="Select the cell you want to edit.", title:="CELL TO EDIT", Type:=8)
On Error GoTo 0
If rng Is Nothing Then Exit Sub
'column G=7
If rng.Column = 7 Then
'do something
End If
End Sub
If user can select more than one cell, change
If rng.Column = 7 Then
to
If Not Application.Intersect(rng, Sheets("Sheet1").Range("G:G")) Is Nothing Then