Next cell after repeat is met

joferder

Board Regular
Joined
Dec 18, 2005
Messages
59
I have a workbook that I use to create labels. It is pretty simple. There is one tab that the user would type in the last name of the person in column A and the number of labels needed in Column B.

Table Name: <mark>Teacher Names and Numbers</mark>
NameQty
Smith2
Doe3


On the other tab in the workbook 'Labels", there are a couple of macros designed to repeat a teacher's name the number of times needed to for the number of labels they need and then create the labels so that I can import them into our label maker software.

Smith 01
Smith 02
Doe 01
Doe 02
Doe 03

The macro I am using is:
Code:
Sub RepeatData()
' hiker95, 10/29/2013
' http://www.mrexcel.com/forum/excel-questions/735863-repeat-cell-multiple-times-based-variable.html
Dim a As Variant, c As Variant
Dim i As Long, ii As Long, n As Long
a = Range("A2:B" & Range("A" & Rows.Count).End(xlUp).Row)
n = Application.Sum(Range("B2:B" & Range("B" & Rows.Count).End(xlUp).Row))
ReDim c(1 To n, 1 To 1)
For i = LBound(a, 1) To UBound(a, 1)
  If a(i, 2) <> "" Or a(i, 2) <> 0 Then
    For n = 1 To a(i, 2)
      ii = ii + 1
      c(ii, 1) = a(i, 1)
    Next n
  End If
Next i
Columns(3).ClearContents
Range("C2").Resize(UBound(c, 1), UBound(c, 2)) = c
Columns(3).AutoFit
End Sub

The problem I recently came upon was that some of our teachers already have computers in their rooms and I don't need to print out a whole new set of labels, so I would like to add a column to the first spreadsheet that would simply list the number of computers already in the room and the formula would automatically start with that number. The problem I am having is that I can get the first name to work correctly, but when it goes to the next name it starts counting at 1 because the formula automatically goes to the next person as it goes down the page. The best I can think of would be to have the macro repeat the number of computers that each teacher already has in column D until the next person's name starts and then their number of computers would repeat next to theirs. My labels page would look something like this:

Sheet Name: <mark>Labels</mark>
Name# of LblsPCs AlreadyName (rpt)PC already (rpt)Lbl #Label
Smith26Smith67Smith 07
Doe33Smith68Smith 08
Doe34Doe 04
Doe35Doe 05
Doe36Doe 06

I hope that all of this makes sense. I would really appreciate any help that anyone can offer to get my macro to repeat the number of computers already in each room on my 'Labels' sheet from 'Teacher Names and Numbers' sheet. If any of this doesn't make sense, please let me know and I will see if I can clarify it.

Thank you in advance for any and all help.

John
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
Code:
Sub RepeatData()
    Range("D2:G" & Rows.Count).ClearContents
    lr = Range("A" & Rows.Count).End(xlUp).Row
    counter = 2
    For i = 2 To lr
        lblCnt = Range("B" & i).Value
        increment = 1
        For j = 1 To Range("B" & i).Value
            Range("D" & counter).Value = Range("A" & i).Value
            Range("E" & counter).Value = Range("C" & i).Value
            Range("F" & counter).Value = Range("C" & i).Value + increment
            Range("G" & counter).Value = Range("A" & i).Value & " " & Format(Range("C" & i).Value + increment, "00")
            counter = counter + 1
            increment = increment + 1
            If increment > lblCnt Then Exit For
        Next j
    Next i
End Sub
 
Upvote 0

Forum statistics

Threads
1,216,074
Messages
6,128,652
Members
449,462
Latest member
Chislobog

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