2

I am having a problem with a particular line of code:

ActiveSheet.Range("A" & rowCount & ":" & Mid(alphabet, totHdrLngth, 1) & belowRowCount)

Where alphabet is a string containing uppercase letters A to Z.

I keep getting the following error:

Run-time error '5':
Invalid Procedure call or argument

I tried creating a String "inRange" and changing the code to this:

inRange = "A" & rowCount & ":" & Mid(alphabet, totHdrLngth, 1) & belowRowCount
curRange = ActiveSheet.Range(inRange)

But that did not help (as I thought it wouldn't). Any suggestions?

12
  • Could you use Debug.Print inRange to get the value of inRange into the debug window (ctrl+g)? Commented Aug 4, 2011 at 17:36
  • Hmm.. not seeing anything in the debug window. Running my cursor over "inRange" in debug mode and says the string is empty (inRange = "") Commented Aug 4, 2011 at 17:51
  • I just want to know how the range string looks when you call range, maybe you could MsgBox it or something. Commented Aug 4, 2011 at 17:53
  • inRange prints as empty (nothing shows up) Commented Aug 4, 2011 at 17:57
  • 1
    When you assign a value to a variable of type <any object>, you need to use the Set keyword. So that curRange assignment looks like it needs one. Without it, you'll get the error "Object variable or With Block" error you describe. Commented Aug 4, 2011 at 22:28

2 Answers 2

5

Although creating ranges like this is frowned upon in general, the way to do it is with the word SET (like @Gary McGill stated in the comments). Here is an example of how to do this:

Sub test()

Dim alphabet As String
Dim totHrdrLngth As Long
Dim belowRowCount As Long
Dim rowCount As Long
Dim inRange As Range

alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
totHrdrLngth = 5
belowRowCount = 10
rowCount = 5

' Gives us A5:E10
Set inRange = Range("A" & rowCount & ":" & range2 & _
                    Mid$(alphabet, totHrdrLngth, 1) & belowRowCount)

End Sub

You are running this macro in the current range, so there should be no need to specify ActiveSheet.Range. I hope this helps get you toward what you are trying to achieve.

Sign up to request clarification or add additional context in comments.

Comments

1

As far as I can tell, you're getting an error because your types don't match up. I imagine rowCount is an integer, as is belowRowCount. If you convert them to strings before concatenating them, you can fix it. str() will convert an integer to a string with a space before it, and LTrim() will remove the space. Try code as below:

Dim sRowCount As String
Dim sBelowRowCount As String

and later

sRowCount = LTrim(Str(RowCount))
sBelowRowCount = LTrim(Str(belowRowCount))

inRange = "A" & sRowCount & ":" & Mid(alphabet, totHdrLngth, 1) & sBelowRowCount
curRange = ActiveSheet.Range(inRange)

Hope this helps.

2 Comments

Excel VBA will cast them to the needed string.
If you use the string concatenator ampersand(&) any integer will be auto-cast to a string

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.