Alter this code to make it loop through rows and columns

oliviar

Board Regular
Joined
Sep 12, 2010
Messages
184
Hello my friends!
I have created this code which turns a range expressed between col A and B - into a list in column F.
So is the start of the range is 10 in A1, and the end of the range is 15 in B1, then f1 is 10, f2 is 11, f3 is 12, f4 is 13, etc.

However my code is static, and will only make the column in F and will only look at the numbers in A1 and B1. But I have more ranges to split up, in A2&B2 and A3&B3, and so on.

How can my code loop through all the ranges expressed between Col A and B on rows 2 and below, and put them into columns after F until exhausted? :confused:

Code:
Sub dotest()
Range("f1").Select

Range("f1") = Range("b1")
  ActiveCell.Offset(1, 0).Select
If Range("b1") > Range("a1") Then
Do
        ActiveCell.Value = ActiveCell.Offset(-1, 0).Value - 1
        ActiveCell.Offset(1, 0).Select
    Loop Until ActiveCell.Offset(-1, 0) - Range("a1").Value = 0
    End If
End Sub
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Code:
Sub MakeList()
    currentrow = 1
    For Row = 1 To Cells(Rows.Count, 1).End(xlUp).Row
        For row2 = Cells(Row, 1) To Cells(Row, 2)
            Cells(currentrow, 6) = row2
            currentrow = currentrow + 1
        Next row2
    Next Row
End Sub
 
Upvote 0
Hey Diddi,
That was cool! (and fast) except I need each time it goes to a new row, for it to also go to the next column. Can this code do that?
 
Upvote 0
Haha!
I altered it like this:
Code:
Sub MakeList()
    currentrow = 1
    i = 6
    For Row = 1 To Cells(Rows.Count, 1).End(xlUp).Row
        For row2 = Cells(Row, 1) To Cells(Row, 2)
            Cells(currentrow, i) = row2
            currentrow = currentrow + 1
        Next row2
        i = i + 1
    Next Row
End Sub

And you should see what it does... its creates cascading bundles of values, all the way down the sheet. Not quite right either - though I can bash it into my will by telling it to delete all empty cells. Though I'm sure there is a correct way.
 
Upvote 0
Its putting all the ranges into one column, and I want each range to be in a new column.
The one I mangled does what I want, except it starts each new column in the row the last one ended off in. So its cascading down like a waterfall.
Ideally each column would resume at row 1.
:)
 
Upvote 0
Code:
Sub MakeList()
    Dim CurrentRow As Long, Row As Long, Row2 As Long
    CurrentRow = 1
    For Row = 1 To Cells(Rows.Count, 1).End(xlUp).Row
        For Row2 = Cells(Row, 1) To Cells(Row, 2)
            Cells(CurrentRow, Row + 5) = Row2
            CurrentRow = CurrentRow + 1
        Next Row2
        CurrentRow = 1
    Next Row
End Sub
 
Upvote 0
no probs. is it still raining in your area
 
Upvote 0

Forum statistics

Threads
1,223,229
Messages
6,170,881
Members
452,364
Latest member
springate

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