VBA in Excel: Getting error using a range - vba

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.

Related

VBA - Run-time error '1004' - Method 'Range' of object'_Global' failed

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

VBA - How to delete all 20 rows after one specific value is found on a cell?

I have an excel file that is filled with 20 rows of useless information all over the table on a random interval. It's the exact same 20 rows repeated over and over, so it always start with the same cell value. Like this:
https://i.gyazo.com/fdec2c2f6c6244d108e6bd07ad6e5181.png
I need to remove the entire row in which they appear, so i've been using this VBA code:
Sub DeleteRows()
'Updateby20140314
Dim rng As Range
Dim InputRng As Range
Dim DeleteRng As Range
Dim DeleteStr As String
xTitleId = "KutoolsforExcel"
Set InputRng = Application.Selection
Set InputRng = Application.InputBox("Range :", xTitleId, InputRng.Address, Type:=8)
DeleteStr = Application.InputBox("Delete Text", xTitleId, Type:=2)
For Each rng In InputRng
If rng.Value = DeleteStr Then
If DeleteRng Is Nothing Then
Set DeleteRng = rng
Else
Set DeleteRng = Application.Union(DeleteRng, rng)
End If
End If
Next
DeleteRng.EntireRow.Delete
End Sub
But it's not quite useful since I have to do that 20 times for each file. So, i would like to know if there's a way to delete all the 20 following rows after the first row with useless information (the yellow on the image). I tried to do this by adding this after the "next" on the code above:
Set DeleteRng = DeleteRng.Resize(DeleteRng.Rows.Count + 20)
but didn't work (Run-time error '1004' Application-defined or object-defined error).
I've hundreds of other files with the same issue, so a code that could do that really would save me a bunch of time.
Thanks in advance
EDIT: Solved by arcadeprecinct below:
Do the Resize before (or during) adding it to DeleteRng.
Application.Union(DeleteRng, rng.Resize(20,1))

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)

Remove reference errors autmatically

i have designed a few codes to help remove reference errors however it does not automatically delete until i assign the macro to a button. i do not want it that way as it would seem unpleasant when i want to present the programme to my team members, and having to remove the errors on the spot with a button. I thought of combining my delete cells code and remove reference cell codes together so that they would run simultaneously but to no avail. Is it possible to combine these two codes to achieve my objective or are there any solutions or coding to remove/hide reference errors automatically? Here are the two codes. All of your help would be very much appreciated!
Sub deletetry2()
Dim R As Range
Set rng = Nothing
On Error Resume Next
Set R = Application.InputBox("Select cells To be deleted", Type:=8)
If TypeName(R) <> "Range" Then
Exit Sub
Else
R.Delete
End If
End Sub
Sub Check_ReferenceDeletecolumn()
Dim rng As Range
Dim rngError As Range
Set rng = Sheets("Sheet3").Range("A1:G100")
On Error Resume Next
Set rngError = rng.Cells.SpecialCells(xlCellTypeFormulas, xlErrors)
On Error GoTo 0
If Not rngError Is Nothing Then
rngError.EntireColumn.Delete
'delete means cells will move up after deleting that entire row
End If
End Sub
If the objective is to remove all rows containing errors, from a user defined range, this should work:
Option Explicit
Public Sub cleanUserDefinedRange()
Dim response As Range
On Error Resume Next
Set response = Application.InputBox("Select range to clean up errors", Type:=8)
If Not response Is Nothing Then cleanUpErrors response
On Error GoTo 0
End Sub
'------------------------------------------------------------------------------------------
Private Sub cleanUpErrors(ByRef rng As Range)
Application.ScreenUpdating = False
rng.SpecialCells(xlCellTypeFormulas, xlErrors).EntireRow.Delete
Application.ScreenUpdating = True
End Sub

Get the column of a user selected cell using vba excel

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