Home » excel » excel – How to provide a variable in the for loop to loop through a range

excel – How to provide a variable in the for loop to loop through a range

Posted by: admin May 14, 2020 Leave a comment

Questions:
Dim FromRang As Range, ToRang As Range
Dim UserCell As Range
'Ask the user to input the cell Range in which to search for the sub strings
    Set FromRang = Application.InputBox(Prompt:="Enter Search Range From:", Type:=8)
    Set ToRang = Application.InputBox(Prompt:="Enter Search Range To:", Type:=8)

For each UserCell in ("FromRang:ToRang")

giving type mismatch error

Please help..

How to&Answers:

Having looked at your previous question, you don’t need two separate variables to capture the range.

Set fullRange = Application.InputBox(Prompt:="Enter Search Range:", Type:=8)
i.e. you can select the range while the input box is active. Also, you can type in A1:A10

And then, you could do

For each UserCell in fullRange
  ...
Next