Long variable "j" won't increase above zero on 2d iteration of do while

M

marc hankin

In my macro (below) named "RemovSpcsAftInitCapng", I
created a long variable "j", and I included the following
statements:

Do While i <= lStrMain
i = i + 1
If i = 1 Then j = i
If i = 2 Then j = j - 1
If i > 2 Then j = j - 1
For some reason which eludes me, after the first
iteration, j = 0 despite the coding above. What am I
doing wrong?

Thank you in advance for any help,

Marc


Function MsgboxFn()
Dim strMain As String, strNuMain As String, strChoice
As String
Dim strLeft As String, strTrim As String, strRt As
String
Dim strMsg As String, StrMsg1 As String,
strBlankSpace As String
Dim blnEmptySpc As Boolean
Dim lStrMain As Long, lstrNuMain As Long, lstrLeft As
Long, lstrRt As Long
Dim i As Long, j As Long
Dim lStrRtInverter As Long, lStrMainCntr As Long
StrMsg1 = "||strMain =" & strMain & "||strNuMain
=" & strNuMain & "||strChoice =" & strChoice & "||strLeft
=" & strLeft & vbCr
StrMsg1 = StrMsg1 & "||strTrim =" & strTrim
& "||strRt As String =" & strRt & "||strMsg =" & strMsg
& "||StrMsg1 =" & StrMsg1 & vbCr
StrMsg1 = StrMsg1 & "||strBlankSpace =" &
strBlankSpace & "||blnEmptySpc =" & blnEmptySpc
& "||lStrMain =" & lStrMain & "||lstrNuMain =" &
lstrNuMain & vbCr
StrMsg1 = StrMsg1 & "||lstrLeft =" & lstrLeft
& "||lstrRt =" & lstrRt & "||i =" & i & "||j =" & j
& "||lStrRtInverter =" & lStrRtInverter & "||lStrMainCntr
=" & lStrMainCntr
MsgBox "StrMsg1 =" & StrMsg1
MsgBox "strMsg =" & strMsg
End Function

Private Sub RemovSpcsAftInitCapng()
'
' Macro recorded 8/29/2004 by Marc B. Hankin
'

Dim strMain As String, strNuMain As String, strChoice
As String
Dim strLeft As String, strTrim As String, strRt As
String
Dim strMsg As String, StrMsg1 As String,
strBlankSpace As String
Dim blnEmptySpc As Boolean
Dim lStrMain As Long, lstrNuMain As Long, lstrLeft As
Long, lstrRt As Long
Dim i As Long, j As Long
Dim lStrRtInverter As Long, lStrMainCntr As Long
Beegin:
Selection.Range.Case = wdTitleWord
strMain = Selection.Text
strNuMain = strMain
'MsgBox strMain
'MsgBox strNuMain
lStrMain = Len(strMain) '# of chars in
strMain
lstrNuMain = Len(strNuMain) '# of
chars in strMain
'MsgBox lStrMain
'
'
MsgboxFn
Do While i <= lStrMain
i = i + 1
If i = 1 Then j = i
If i = 2 Then j = j - 1
If i > 2 Then j = j - 1
MsgBox "Iteration No." & i & " AND j=" & j
& " AND strNuMain=" & strNuMain
strBlankSpace = ""
strBlankSpace = Mid(strNuMain, j, 1)
blnEmptySpc = False
If strBlankSpace = Chr(32) Then blnEmptySpc = True
If blnEmptySpc = False Then If strBlankSpace = Chr
(160) Then blnEmptySpc = True
If blnEmptySpc = True Then
MsgBox ("There is a space located in Space
No. " & j)
If j > 1 Then
strLeft = Left(strNuMain, i - 1)
Else
strLeft = ""
End If
'
MsgBox "Now leaving ..strLeft = null.. End
If ..."
'
'MsgBox strLeft
'
lStrRtInverter = lstrNuMain - i
j = i - 1
strRt = Right(strNuMain, lStrRtInverter)
'MsgBox strRt
strNuMain = strLeft & strRt
'Length of each string
lstrNuMain = Len(strNuMain)
lstrLeft = Len(strLeft)
lstrRt = Len(strRt)
End If
MsgBox "Now leaving ..Bottom of loop..."
MsgboxFn
Loop
End Sub
 
K

Klaus Linke

Do While i <= lStrMain
i = i + 1
If i = 1 Then j = i
If i = 2 Then j = j - 1
If i > 2 Then j = j - 1
For some reason which eludes me, after the first
iteration, j = 0 despite the coding above.
What am I doing wrong?



Hi Marc,

Assuming you start with i=1...
Then j=i=1 after the first iteration.

After the second iteration, i=2, so j=j-1=1-1=0.

I haven't looked at the rest of your code to see what you want to do.
What value did you want/expect?

A "For ... Next" would probably be easier to read (and I wouldn't have had
to guess which value of i you start with):

For i=1 to lStrMain-1
'...
Next i

Perhaps you also should use "Select Case" instead of the Ifs. It's easier
to read (well, at least it is for me), and you don't run the risk to change
j more than once.

Select Case i
Case 1
j=i
Case 2
j=j-1
Case >2
j=j-1
Case Else
MsgBox "I didn't expect i=" & i
End Select

Regards,
Klaus
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top