PDA

View Full Version : Runtime error 5 - manipulating strings & Ascii



dev00790
11-03-2012, 02:27 PM
Hi I'm getting a runtime error in the code below.
Please advise.


Option Explicit

Dim i As Integer
Dim sAscii
Dim j As Integer
Dim k As Integer
Dim str As String
Dim AscEncode_result As String

Sub get_ascii_codes2()
For k = 2 To 1037
str = Cells(k, 1).Value
'str here is: "J-_c+w.+zomT6Y!2bsC-H8qlv_L+:brxrr!h,U3I+3Z.-Gs_dTZ5L&1bsspo,ffb9Q"
i = 1
For j = 2 To 350

Debug.Print str
Debug.Print i
Debug.Print j
Debug.Print k

'get run time error 5 in below line
'k=2, j=35, i=34
'str = "r!h,U3I+3Z.-Gs_dTZ5L&1bsspo,ffb9Q"
Cells(k, j).Value = CStr(Asc(Mid(str, i, 1)))
If Len(str) > 1 Then
str = Right(str, Len(str) - 1)
Else
MsgBox ("Value of str is now " & str & " for k of " & k)
End If
i = i + 1
Next j
Next k
End Sub

Bob Phillips
11-03-2012, 03:31 PM
Str is an invalid variable name, that aside it works fine for me.

dev00790
11-03-2012, 03:43 PM
I have edited my first post. - Added in variable declarations.

I don't see how str is an invalid variable name?

Teeroy
11-03-2012, 04:04 PM
I'm not sure Str is an invalid (i.e reserved) variable name (you'd definitely have to declare it) but it is the name of a VBA function so a VERY bad idea to use it.

Bob Phillips
11-04-2012, 12:21 PM
I have edited my first post. - Added in variable declarations.

I don't see how str is an invalid variable name?

It is invalid because you didn't declare it, and as such the code thought it was referring to the builtin string function.

But, you shouldn't use it because you wipe the builtin if you do.