Question regarding us a for next loop for inputting data into a column

zoog25

Active Member
Joined
Nov 21, 2011
Messages
418
Hello all here is a question

Say for example i have 2 cells an a column. B2, C2 and column A. Now in B2 i have "p101 and C2 I have "p130". These two are the start and end of a range of numbers from p101 to p130. Each number to be put into column A so A1 = p101 and A30 = p130. Now I now by dragging down, excel does this, but i want to use a vba code because when i have to do it multiple times it gets a little confussing. Please help.
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Hi,

Try something like this:
Code:
Option Explicit


Sub Create_list()
    Dim C As Long
    Dim E As Integer
    Dim Prefix As String
    Dim R As Long
    Dim S As Integer
    Dim Splitpoint As Integer
    Dim Work As String
    
    'Get start
    Work = Range("B2").Value
    'Find splitpoint
    Splitpoint = Len(Work)
    While IsNumeric(Mid(Work, Splitpoint, 1))
        Splitpoint = Splitpoint - 1
    Wend
    Prefix = Mid(Work, 1, Splitpoint)
    S = CInt(Mid(Work, Splitpoint + 1))
    E = CInt(Mid(Range("C2").Value, Splitpoint + 1))
    R = 1
    For C = S To E
        Range("A" & R).Value = Prefix & C
        R = R + 1
    Next C
End Sub

Paul
 
Upvote 0

Forum statistics

Threads
1,215,470
Messages
6,124,992
Members
449,201
Latest member
Lunzwe73

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