Using two counters to create data

tholder

New Member
Joined
Feb 16, 2011
Messages
11
I have a list of about 40 rows, and each row has an ID, year, and total value. I need to create a row with the ID and Year and a number starting at one up and going the total value.

For example, if row one of my data was
12 2011 3

I would need to create
12 2011 1
12 2011 2
12 2011 3


This is what I have, but when I run it nothing happens.

Sub barCodeSheet()
Dim maxVal As Long, id As Long, year As Long

For Counter = 1 To maxVal
Location.Value = Counter
Location.Offset(0, -1).Value = year
Location.Offset(0, -2).Value = id
Set Location = Location.Offset(1, 0)
Next Counter

End Sub

Sub retrieve()
Dim maxVal As Long, id As Long, year As Long, Row_Count As Long

Row_Count = Application.CountA(Range("A:A"))

Set Location = Cells(6, 2)

For Counter = 2 To Row_Count

id = Cells(Counter, 1).Value
maxVal = Cells(Counter, 4).Value
year = Cells(Counter, 3).Value

Call barCodeSheet

Next Counter

End Sub
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Not sure if I've understood your query but I'm going to make the following assumptions (these can all be changed to accomodate but to get something working this will do):

-You have no header rows
-Your example of 12 2011 3 are in cells A1, B1 and C1 respectively
- The output will start in cell E1 and be on the same sheet

Given this, try:
Code:
Sub TurtlesCanSwim()
With Application
    .ScreenUpdating = False
    .Calculation = xlCalculationManual
End With
Dim i As Long, j As Long, k As Long, L As Long
k = 1
For i = 1 To Range("A" & Rows.Count).End(xlUp).Row
    If Range("C" & i) > 0 Then
    L = 1
        For j = Range("C" & i) To 1 Step -1
            Range("A" & i & ":B" & i).Copy
            Range("E" & k).PasteSpecial xlPasteAll
            Range("G" & k) = L
            k = k + 1
            L = L + 1
        Next j
    End If
Next i
With Application
    .ScreenUpdating = True
    .Calculation = xlCalculationAutomatic
End With
End Sub
 
Upvote 0
Oh that worked perfectly. Very different approach, clever! Thanks a lot. And I love the sub name.
 
Upvote 0
Thank you and thank you! I was just being creative with the sub name, seem to be doing that alot lately.

I don't know why I made the 'L' variable capital and i, j and k all lower case, typo - no esoteric reason anyway.

Generally speaking as your data size increases, nested loops aren't the best way to work through data but I'm not sure (in this instance) what other solutions there are, so curious if anyone else can post something faster/more efficient.

Anyway, glad it works,
Jack
 
Upvote 0
After I used your code for what I needed done immediately I went back and got my code to work. Works pretty slick actually.

Code:
Sub barCodeSheet(maxval As Long, id As Long, year As Long, Location As Range)

    For Counter = 1 To maxval
        Location.Value = Counter
        Location.Offset(0, -1).Value = year
        Location.Offset(0, -2).Value = id
        Set Location = Location.Offset(1, 0)
    Next Counter
    
End Sub

Sub retrieve()
Dim maxval As Long, id As Long, year As Long, Row_Count As Long, Location As Range

    Row_Count = Application.CountA(Range("A:A"))
    
    Set Location = Range("f2")
    
    For Counter = 2 To Row_Count
    
        id = Cells(Counter, 1).Value
        maxval = Cells(Counter, 3).Value
        year = Cells(Counter, 2).Value

        barCodeSheet maxval, id, year, Location
    
    Next Counter

End Sub
 
Upvote 0

Forum statistics

Threads
1,224,560
Messages
6,179,520
Members
452,922
Latest member
nstaab07

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