如何在for循环中提供变量以循环遍历范围

时间:2013-05-01 15:36:50

标签: excel vba

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")

给出类型不匹配错误

请帮助..

1 个答案:

答案 0 :(得分:3)

查看了上一个问题后,您不需要两个单独的变量来捕获范围。

Set fullRange = Application.InputBox(Prompt:="Enter Search Range:", Type:=8)
即,您可以在输入框处于活动状态时选择范围。此外,您还可以输入A1:A10

然后,你可以做

For each UserCell in fullRange
  ...
Next