Loop through string and write to range

stapuff

Well-known Member
Joined
Feb 19, 2004
Messages
1,126
I am trying to figure out how to determine the end of each line in my string.

My data is returned like

item = 2
item = 13
item = 9
item = 1742596

I want item1 returned to sheet1 range A1, item2 returned to sheet1 range A2, item3 returned to sheet1 range A3

Thanks,

Kurt
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
suppose your data is in B1, try this:

Code:
Sub test3()
Dim myString As String, myRange As Range, r As Byte

myString = Range("B1").Value
Set myRange = Range("A1")

r = Len(myString) - Len(Replace(myString, Chr(10), ""))

Do Until n = r
    myRange.Offset(n).Value = Left(myString, InStr(1, myString, Chr(10), vbTextCompare) - 1)
    myString = Replace(myString, myRange.Offset(n).Value & Chr(10), vbNullString)
    n = n + 1
Loop
myRange.Offset(n).Value = myString

End Sub
 
Upvote 0
Hey pool...appreciate the post back.

r = 25

N gets to 7 then errors out Invalid procedure call or arguement

your thoughts,

Kurt
 
Upvote 0
It looks like where it errors out there are 2 chr(10) in a row and the code only gets rid of the first one.

Kurt
 
Upvote 0
let's then just replace two consecutive chr(10) with one like this:

Code:
Sub test3()
Dim myString As String, myRange As Range, r As Byte

Range("B1").Value = Replace(Range("B1").Value, Chr(10) & Chr(10), Chr(10))
myString = Range("B1").Value
Set myRange = Range("A1")

r = Len(myString) - Len(Replace(myString, Chr(10), ""))

Do Until n = r
    myRange.Offset(n).Value = Left(myString, InStr(1, myString, Chr(10), vbTextCompare) - 1)
'    myString = Replace(myString, myRange.Offset(n).Value & Chr(10), vbNullString)
    myString = Right(myString, Len(myString) - Len(myRange.Offset(n).Value) - 1)
    n = n + 1
Loop
myRange.Offset(n).Value = myString

End Sub

I also changed one line to take care of the situation where there are matching items.
 
Upvote 0
sorry...missed you line edit.

works!

the first and last 3 cells return the empty square box still.

your thoughts,

kurt
 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,285
Members
452,902
Latest member
Knuddeluff

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top