3

How to increment the values in one column depending upon the values of other column?

For ex

if it is

 A             B    

TS-1
TS-1
TS-1
TS-2
TS-2
TS-3
TS-3
TS-3
TS-3

it should come as

 A             B
TS-1        TC-1-1
TS-1        TC-1-2
TS-1        TC-1-3
TS-2        TC-2-1
TS-2        TC-2-2
TS-3        TC-3-1
TS-3        TC-3-2
TS-3        TC-3-3
TS-3        TC-3-4

It should be like this in column B depending on the number of values in columm A

4 Answers 4

3
  1. Copy this formula in cell B1: =A1 & "-" & COUNTIF(A$1:A1;A1)
  2. Drag the content from B1 to the end of the dataselection.
1
  • This doesn't work in Office 2010. Updated it to =A1 & "-" & COUNTIF(A$1:A1,A1)
    – TechnoCrat
    Jan 29, 2015 at 5:51
0

This looks fairly simple to accomplish.

Setup

  • Have a variable valA that stores the number in colA.
  • Have a variable valB that stores the rightmost number to be printed in colB.

Algorithm

  • Parse text in a row.
  • When valA is different from the previous row, you know you have to reset valB to 1.
  • Print valB and increment it by 1.
  • Keep parsing until you reach the end.
0

Given nice circumstances, you can compute a new value based on an old one by applying a function to the latter. In this case, you can't write a pure formula/function, because the new value depends not only on the old one, but also on the number of elements of the group (1, 2, 3, ...) it belongs to.

So you have to write a loop over the source elements that keeps track of the groups. When seeing "group", think "dictionary" - instead of keeping track of group identifier changes using If and a variable, let the dictionary work for you. Additional bonus: no dependency on order.

In code:

  Dim aSrc : aSrc = Split("TS-1 TS-1 TS-1 TS-2 TS-2 TS-3 TS-3 TS-3 TS-3")
  WScript.Echo "aSrc:", Join(aSrc)
  Dim aExp : aExp = Split("TC-1-1 TC-1-2 TC-1-3 TC-2-1 TC-2-2 TC-3-1 TC-3-2 TC-3-3 TC-3-4")
  ReDim aRes(UBound(aSrc))
  Dim dicN : Set dicN = CreateObject("Scripting.Dictionary")
  Dim nRow : nRow     = 0
  Dim sInp
  For Each sInp In aSrc
      Dim aParts : aParts = Split(sInp, "-")
      dicN(aParts(1)) = dicN(aParts(1)) + 1
      aRes(nRow) = "TC-" & aParts(1) & "-" & dicN(aParts(1))
      nRow = nRow + 1
  Next
  WScript.Echo "aExp:", Join(aExp)
  WScript.Echo "aRes:", Join(aRes)

output:

aSrc: TS-1 TS-1 TS-1 TS-2 TS-2 TS-3 TS-3 TS-3 TS-3
aExp: TC-1-1 TC-1-2 TC-1-3 TC-2-1 TC-2-2 TC-3-1 TC-3-2 TC-3-3 TC-3-4
aRes: TC-1-1 TC-1-2 TC-1-3 TC-2-1 TC-2-2 TC-3-1 TC-3-2 TC-3-3 TC-3-4
0

You can also do this without VBA by using an auxiliary column. Put the following formula in column C (starting in C2!):

=IF(A1A2,1,C1+1)

Note: this assumes your list in column a is sorted!

Then, use the following formula in column B:

=SUBSTITUTE(A2,"S","C")&"-"&C2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.