Solved

A Macro to Copy Labels

Posted on 2002-04-10
9
211 Views
Last Modified: 2010-05-02
I have a spread sheet with various product reference numbers and stock levels.

For example.

REF     QUANTITY

100        3
101        4
102        2

This is in excel in two columns.

What I need is to set up some sort of macro that creates a reference number for each of the quantities.( i.e: 3 entries for REF 100, 4 entries for REF 101 etc)
So for example, would create another sheet that shows the following :

REF    QUANTITY

100      1
100      1
100      1
etc
I need to print a label for each reference number for each individual stock item.

Rob.

0
Comment
Question by:RobertWhite
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 3
9 Comments
 
LVL 44

Expert Comment

by:bruintje
ID: 6930920
Hi RobertWhite,

-open the VB Editor with ALT+F11
-then insert a new module from the menu
-paste the code

Option Explicit

Sub RecordToLabels()
Dim intRows As Integer
Dim strName As String
Dim intRecCount As Integer
Dim i As Integer, j As Integer
Dim intRecordLabelCounter As Integer

  'assuming your data start at A2 on sheet 1
  Sheets(1).Activate
  Range("A2").Select
  Selection.End(xlDown).Select
  intRows = Selection.Row
  For i = 2 To intRows
    intRecCount = Range("B" & i)
    strName = Range("A" & i)
    For j = 1 To intRecCount
      Sheets(2).Range("A" & j + 1 + intRecordLabelCounter) = strName
      Sheets(2).Range("B" & j + 1 + intRecordLabelCounter) = 1
    Next j
    intRecordLabelCounter = intRecordLabelCounter + j - 1
  Next i
End Sub

-then choose F8 to step
-or choose F5 to run

HTH:O)Bruintje
0
 

Author Comment

by:RobertWhite
ID: 6930928
Bruintje

Thanks for this.
I have tried this but nothing seems to happen.
When I press F5, I get prompted with the "GO TO" reference.

Am I doing something wrong ?

Rob
0
 
LVL 44

Expert Comment

by:bruintje
ID: 6930962
Hi Rob,

You can download a little sample here

http://www.bredlum.com/ee_temp/sample.xls

if you got any questions just post it here

HTH:O)Bruintje
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 6930969
Hi, try this macro:

Public Sub Duplicate()
  Dim lngLoop As Long
  Dim lngQuantity As Long
  Dim lngRowIndex As Long
 
  Sheet2.Range("A1:B10000").ClearContents
  Sheet1.Range("A1:B1").Copy Sheet2.Range("A1:B1")
   
  lngLoop = 2
  lngRowIndex = 2
 
  While Sheet1.Cells(lngLoop, 1) <> Empty
    lngQuantity = Val(Sheet1.Cells(lngLoop, 2).Value)
    While lngQuantity > 0
      Sheet2.Cells(lngRowIndex, 1).Value = Sheet1.Cells(lngLoop, 1).Value
      Sheet2.Cells(lngRowIndex, 2).Value = 1
     
      lngQuantity = lngQuantity - 1
      lngRowIndex = lngRowIndex + 1
    Wend
    lngLoop = lngLoop + 1
  Wend
 
End Sub


CHeers
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 75 total points
ID: 6930971
sorry bruintje, didn't see yours...
Robert, you can still try mine, eventually replacing

Sheet1 and Sheet2 as needed...

CHeers
0
 

Author Comment

by:RobertWhite
ID: 6930978
Sorry guys.

My excel and Vb skills are not the best.
How do I actually get these macros to run once copied into the MODULE ?

Regards

Rob
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 6930985
Either place the cursor inside the procedure, and hit <F5>, or hit <ALT>+<F8>, choose the macro (duplicate) from the list, and hit the RUN button.

CHeers
0
 

Author Comment

by:RobertWhite
ID: 6930994
This has worked a treat.
Brilliant.

Thanks a lot.
Is it ok to accept ANGELS answer ?

Rob
0
 
LVL 44

Expert Comment

by:bruintje
ID: 6931010
o yeah of course
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question