You are here:

Excel/Excel VBA for generating labels

Advertisement


Question
QUESTION: Dear Aidan,

I have a data set in the form:

1
2
3
4
-1
-1
-1
3
6
7
-1
-1
-1
-1

I need to generate labels in another columns where the value of a designated column is not -1. Please note each sequence of values between the -1s represent a cluster, and the number of values in each cluster and the number of -1 separators varies.
I need something like:

C01  1
C01  2
C01  3
C01  4
    -1
    -1
    -1
C02  3
C02  6
C02  7
    -1
    -1
    -1
    -1

Thanks,

Benny

ANSWER: I'm not clear what you wanted me to do with the -1 values - the macro itself seems relatively simple
whilst the value of the cell is not -1, put the data in increasing columns
at -1, reset the column to the (first?) one
continue until no more data remains

BUT as I say, knowing how you want the output makes a difference (slightly) to the best method of programming it - if it helps, my direct email is aidan.heritage@virgin.net

---------- FOLLOW-UP ----------

QUESTION: I wrote -1 because that was the way I originally received the data. that -1 value can be replaced by any character as a separator(e.g. x). Main question here is labeling; with members of each cluster having the same label (in an adjacent column), and leave the separators cells in the this new column empty.

unlabeled data

Col
1
2
3
x
x
x
1
2
3

data after labeling

Col 1    Col2
C01      1
C01      2
C01      3
EMPTY    x
EMPTY    x
EMPTY    x
C02      1
C02      2
C02      3

C01= cluster 1, C02= cluster 2

Might use a loop to add 1 to the labels of the next cluster which start after an empty block.

Thanks again,

Benny

Answer
I think I've followed the requirements - this macro keeps the original data in column 1 and labels column 2 - that't the opposite way round to your explanation but it would be easy enough to insert a column first and then amend the logic here


Sub labeler()
Dim n As Long, OutVar As Long, endvar As Long
endvar = Cells.SpecialCells(xlCellTypeLastCell).Row
n = 2
OutVar = 1
While n <= endvar
   While Cells(n, 1).Value <> "x" And n <= endvar
       Cells(n, 2).Value = "GROUP " & OutVar
       n = n + 1
   Wend
   While Cells(n, 1).Value = "x"
       n = n + 1
   Wend
   OutVar = OutVar + 1
Wend
End Sub

About Excel
This topic answers questions related to Microsoft Excel spreadsheet (or workbook) stand-alone or Mircrosoft Office Excel including Excel 2003, Excel 2007, Office 2000, and Office XP. You can get Excel help on Excel formulas(or functions), Excell macros, charting in Excel, advanced features, and the general use of Excel. This does not provide a general Excel tutorial nor the basics of using a spreadsheet. It provides specific answers to using Microsoft Excel only. If you do not see your Excel question answered in this area then please ask an Excel question here

Excel

All Answers


Answers by Expert:


Ask Experts

Volunteer


Aidan Heritage

Expertise

I have provided first hand support since `95 for Microsoft Office majoring in Word and Excel - support for all versions from 2 onwards. I'm based in the UK, so please allow for time differences when asking me questions from other parts of the world!

Experience

My background is in the insurance industry and call centre areas, but have been called upon to provide many varied solutions.

Education/Credentials
I'm educated to UK A level standard, but as I left school some 30 years ago that is rather irrelevent - university of life has provided more of a background!

©2016 About.com. All rights reserved.