Using an inputbox for a range within a macro

cubiclemonkey

New Member
Joined
Mar 7, 2012
Messages
25
Good afternoon,

Is there a way to create a range based off of an inputbox that can be used within the following macro. I would like to replace the stipulate range of "K3:K2455" with an input for a starting cell and ending cell.

VBA Code:
Sub SorH()

   Dim Amount As Object
    
    For Each Amount In Range("K3:K2455")
                            
        If Amount > 0 Then
            Amount.Offset(0, -1).value = "S"
            
        ElseIf Amount < 0 Then
            Amount.Offset(0, -1).value = "H"
        
        Else
            Amount.Offset(0, -1).value = ""
        End If
    
    Next Amount
        
End Sub
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Try this:

VBA Code:
Sub SorH()
  Dim Amount As Range
  Dim sCell As Variant
  
  sCell = InputBox("input starting cell and ending cell, exmple: K3:K2455")
  
  For Each Amount In Range(sCell)
    If Amount > 0 Then
      Amount.Offset(0, -1).Value = "S"
    ElseIf Amount < 0 Then
      Amount.Offset(0, -1).Value = "H"
    Else
      Amount.Offset(0, -1).Value = ""
    End If
  Next Amount
End Sub
 
Upvote 0
Solution
I'm glad to help you. Thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,216,403
Messages
6,130,364
Members
449,576
Latest member
DrSKA

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