Beginner at VBA, getting a 424 error

pingwong9r9r

New Member
Joined
May 3, 2017
Messages
3
Hello,
I'm currently trying to write a macro which will accept user input for a range, where if only a single cell is selected, then the entire corresponding row to that cell is considered as the user input range, but I keep getting a 424 error with the following code:
Code:
Sub Calculations()


Dim UserRange As Range
Dim Result As Integer


Set UserRange = Application.InputBox("Select a range", "Record Selection", Type:=8)


Result = UserRange.EntireRow.Count


If Result = 1 Then
    Set UserRange = UserRange.EntireRow.Select
    End If
'... Perform calculations with user range


End Sub
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Do you want this

Code:
Result = UserRange.EntireRow.Count

to count the number of used cells in the row ?
If so, why ??
What calculation are you going to perform on that row ??
 
Upvote 0
Hello Micheal,
Thanks for your response. I used Result as a way to check that the user has selected only a single cell. If the user has selected a single cell, I wanted to reassign my UserRange value to the corresponding row that the user selected cell is in, rather than the single cell. I am not sure if I've reassigned UserRange correctly inside of my If statement, using...
Rich (BB code):
Set UserRange =  = UserRange.EntireRow.Select


After my If statement, I'm passing UserRange to various built in math operations, but am receiving either 424 or 1004 errors after fidgeting with UserRange... Here is my entire code.
Rich (BB code):
Rich (BB code):
Sub Calculations()


Dim UserRange As Range
Dim Result As Integer
Dim Avg As Double
Dim Sum As Double
Dim Lcm As Double
Dim Rms As Double


Set UserRange = Application.InputBox("Select a range", "Record Selection", Type:=8)
Result = UserRange.EntireRow.Count


If Result = 1 Then
    Set UserRange = UserRange.EntireRow.Select
End If


Avg = Application.WorksheetFunction.Average(UserRange)
Sum = Application.WorksheetFunction.Sum(UserRange)




Range("H1") = Avg
Range("H2") = Sum




MsgBox "Average: " & Application.WorksheetFunction.Average(UserRange) & vbNewLine _
     & "Sum: " & Application.WorksheetFunction.Sum(UserRange)
    


                


End Sub



 
Upvote 0
Lose the word Select. You don't use that when assigning ranges, i.e.
this
Code:
Set UserRange = UserRange.EntireRow.Select
should just be:
Code:
Set UserRange = UserRange.EntireRow
 
Upvote 0

Forum statistics

Threads
1,224,819
Messages
6,181,153
Members
453,021
Latest member
Justyna P

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