Appending specific text at the end of every row depending on occurrence of a number

manvit

New Member
Joined
Oct 31, 2018
Messages
19
Hi,
I m new to VBA and my problem is explained below with example
i need to go from this :
rankname
1jon
2alex
3william
4josh
5jamies
1sarah
2angela
3haley
4julie
5sofia

<tbody>
</tbody>
In this table , i need to append a text which has no column name. 'A' has to be appended the end of every row until the next occurrence of 1 in column rank . from the second occurrence of 1 in rank column i need 'B' to be append at the end of every row.
after applying the code, it should look like this.
rankname
1jon A
2alexA
3williamA
4joshA
5jamiesA
1sarahB
2angelaB
3haleyB
4julieB
5sofia

B

<tbody>
</tbody>

Any help on writing this code will be very much appreciated.

Thanks,
Manvit
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Try this for results in column "C"
Code:
[COLOR="Navy"]Sub[/COLOR] MG01Nov12
[COLOR="Navy"]Dim[/COLOR] Rng [COLOR="Navy"]As[/COLOR] Range, Dn [COLOR="Navy"]As[/COLOR] Range, c [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] Temp [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]String[/COLOR]
[COLOR="Navy"]Set[/COLOR] Rng = Range("A2", Range("A" & Rows.Count).End(xlUp))
c = 64
[COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] Dn [COLOR="Navy"]In[/COLOR] Rng
    [COLOR="Navy"]If[/COLOR] Dn.Value = 1 [COLOR="Navy"]Then[/COLOR]
        c = c + 1
        Temp = Chr(c)
    [COLOR="Navy"]End[/COLOR] If
    Dn.Offset(, 2).Value = Temp
[COLOR="Navy"]Next[/COLOR] Dn
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0
Thanks :) It works !!! . i need to know how did you manage to print 'B' ? In reality i need to append text like "Physics", "Chem". how can i append these?
 
Upvote 0
No problem , Have a look at the below:-
Code:
[COLOR="Navy"]Sub[/COLOR] MG01Nov40
'[COLOR="Green"][B]Variables Declared[/B][/COLOR]
[COLOR="Navy"]Dim[/COLOR] Rng [COLOR="Navy"]As[/COLOR] Range, Dn [COLOR="Navy"]As[/COLOR] Range, c [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] Temp [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]String[/COLOR]

'[COLOR="Green"][B]"Rng", Sets the range of data in column "A" to be looped through[/B][/COLOR]
[COLOR="Navy"]Set[/COLOR] Rng = Range("A2", Range("A" & Rows.Count).End(xlUp))

'[COLOR="Green"][B]NB:- The Ascii codes from "65 to 90", give[/B][/COLOR]
'[COLOR="Green"][B]you a list of 'Characters "A to Z", so Chr(65") = "A"[/B][/COLOR]
c = 64

'[COLOR="Green"][B]Loop through column "A"[/B][/COLOR]
[COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] Dn [COLOR="Navy"]In[/COLOR] Rng
  
  '[COLOR="Green"][B]When dn.value = 1 then c = 65 and Chr(65)= "A"[/B][/COLOR]
    [COLOR="Navy"]If[/COLOR] Dn.Value = 1 [COLOR="Navy"]Then[/COLOR]
        c = c + 1
        
        '[COLOR="Green"][B]So on first loop when "1" is found temp (string Variable) = "A", then[/B][/COLOR]
        '[COLOR="Green"][B]Next time Dn.value = "1" , c = 66 and Temp = "B" ect[/B][/COLOR]
        Temp = Chr(c)
    [COLOR="Navy"]End[/COLOR] If
    
    '[COLOR="Green"][B]Column "C" value = Temp[/B][/COLOR]
    Dn.Offset(, 2).Value = Temp
[COLOR="Navy"]Next[/COLOR] Dn
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0
Try this:-
Change array "Subjects" as shown in Code !!
Code:
[COLOR="Navy"]Sub[/COLOR] MG01Nov42
[COLOR="Navy"]Dim[/COLOR] Rng [COLOR="Navy"]As[/COLOR] Range, Dn [COLOR="Navy"]As[/COLOR] Range, c [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] Temp [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]String,[/COLOR] ray [COLOR="Navy"]As[/COLOR] Variant
[COLOR="Navy"]Set[/COLOR] Rng = Range("A2", Range("A" & Rows.Count).End(xlUp))

'[COLOR="Green"][B]Change the array below to Your specific subject[/B][/COLOR]
ray = Array("Physics", "Chem", "Maths", "English")


[COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] Dn [COLOR="Navy"]In[/COLOR] Rng
    [COLOR="Navy"]If[/COLOR] Dn.Value = 1 [COLOR="Navy"]Then[/COLOR]
        c = c + 1
        Temp = ray(c - 1)
    [COLOR="Navy"]End[/COLOR] If
    Dn.Offset(, 2).Value = Temp
[COLOR="Navy"]Next[/COLOR] Dn

[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick

http://www.dec.org.uk/
 
Upvote 0
No problem , Have a look at the below:-
Code:
[COLOR=Navy]Sub[/COLOR] MG01Nov40
'[COLOR=Green][B]Variables Declared[/B][/COLOR]
[COLOR=Navy]Dim[/COLOR] Rng [COLOR=Navy]As[/COLOR] Range, Dn [COLOR=Navy]As[/COLOR] Range, c [COLOR=Navy]As[/COLOR] [COLOR=Navy]Long,[/COLOR] Temp [COLOR=Navy]As[/COLOR] [COLOR=Navy]String[/COLOR]

'[COLOR=Green][B]"Rng", Sets the range of data in column "A" to be looped through[/B][/COLOR]
[COLOR=Navy]Set[/COLOR] Rng = Range("A2", Range("A" & Rows.Count).End(xlUp))

'[COLOR=Green][B]NB:- The Ascii codes from "65 to 90", give[/B][/COLOR]
'[COLOR=Green][B]you a list of 'Characters "A to Z", so Chr(65") = "A"[/B][/COLOR]
c = 64

'[COLOR=Green][B]Loop through column "A"[/B][/COLOR]
[COLOR=Navy]For[/COLOR] [COLOR=Navy]Each[/COLOR] Dn [COLOR=Navy]In[/COLOR] Rng
  
  '[COLOR=Green][B]When dn.value = 1 then c = 65 and Chr(65)= "A"[/B][/COLOR]
    [COLOR=Navy]If[/COLOR] Dn.Value = 1 [COLOR=Navy]Then[/COLOR]
        c = c + 1
        
        '[COLOR=Green][B]So on first loop when "1" is found temp (string Variable) = "A", then[/B][/COLOR]
        '[COLOR=Green][B]Next time Dn.value = "1" , c = 66 and Temp = "B" ect[/B][/COLOR]
        Temp = Chr(c)
    [COLOR=Navy]End[/COLOR] If
    
    '[COLOR=Green][B]Column "C" value = Temp[/B][/COLOR]
    Dn.Offset(, 2).Value = Temp
[COLOR=Navy]Next[/COLOR] Dn
[COLOR=Navy]End[/COLOR] [COLOR=Navy]Sub[/COLOR]
Regards Mick


Thanks Mick :) ,
But it wouldn't work if i need to append words like 'Physics' and 'chemistry'. So do you have any idea about this ?
 
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,120
Members
451,399
Latest member
alchavar

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