Loop function!

Lbarron

New Member
Joined
Nov 3, 2017
Messages
7
Hi all,

I'm trying to write some code in excel vba that will use the loop command to print numbers to cells, say from 1 to 100. I can get the numbers to print to cells however what I also need is each number to be wrapped in symbols like this.

"X" ,

Including the comma.

I've tried a few things but loose the effect due to the compiler seeing the comma as a next expression.

My last code was basically ......value = i ± 1
Next i

Any idea please?
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Can you post your current code and some examples of expected results?
 
Upvote 0
Is this what you want?

Here are two versions:
Code:
Sub Count_Me()
Dim i As Long
    For i = 1 To 20
        Cells(i, 1).Value = i & "X"
    Next
End Sub

Code:
Sub Count_Two()
Dim i As Long
    For i = 1 To 20
        Cells(i, 2).Value = "*" & i & "X"
    Next
End Sub
 
Upvote 0
The bottom one looks very promising and very similar to my current code

In each cell I need it to read "1" , then "2" , and so on till "100" ,

I need the cells to display the characters so the quotation symbol and the comma.
 
Upvote 0
Is this what you want:
Code:
Sub Count_Me()
Dim i As Long
Dim ans As String
ans = """"
    For i = 1 To 20
        Cells(i, 1).Value = ans & i & ans
    Next
End Sub
 
Upvote 0
I need the cells to display the characters so the quotation symbol and the comma.

Hi, welcome to the forum, here is a different non-looping option you can also try:

Rich (BB code):
Sub m()
With Range("A1").Resize(100)
    .Value = Evaluate("CHAR(34)&ROW(1:" & .Rows.Count & ")&CHAR(34)& "" ,""")
End With
End Sub
 
Last edited:
Upvote 0
I've tried something similar. Not sure it will show the comma but thank you. I will try this!
 
Upvote 0
Thanks formR never seen anything like this but will try it! Thanks again!
Then you might not have seen anything like this either :LOL: Since your range and output to it is fixed (that is, not dependent on variables), there is this compact one-liner available...
Code:
Sub Counter()
  [A1:A100] = [""""&ROW(1:100)&""","]
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,219
Messages
6,123,692
Members
449,117
Latest member
Aaagu

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