When asking for a Range input : object variable or with block variable not set in excel vba

I want user to input the cell Range

Dim FromRang, ToRang As Range

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

But this is giving me an error: "object variable or with block variable not set in excel vba"

Answers


Objects such as a Range can only be assigned in VBA by using Set. So as mehow mentioned you should use:

Set FromRang = Application.InputBox(Prompt:="Enter Search Range From:", Type:=8)

Furthermore dimensioning multiple variables requires you to state the type for every variable separately:

Dim FromRang As Range, ToRang As Range

Otherwise the first one will be unspecified and by default this results in it becoming of the type Variant which could lead to undesired behaviour.


Need Your Help

mathjax rendering stuck in Sublimetext Markdown Preview

markdown sublimetext preview mathjax

Using Sublime Text 3, I'm writing a Markdown document that includes math. The Markdown Preview package enables real-time rendering of the document in the browser (Firefox). So as I write, the cha...