Assign title and subtitle number VBA

mdelandro

New Member
Joined
Mar 9, 2020
Messages
5
Office Version
  1. 2019
Platform
  1. Windows
  2. MacOS
Hi,

I would like to run a code that runs through all cells in column C (Title/Subtitle), Looks for the word "Title" and assigns the correct number in column A (Title), the number being integers (1, 2, 3 , 4.....).
Then I would like to do the same thing but look for subtitle in cell C and assign decimals in cell A (1.1, 2.1, 2.2, 2, 3.1 ecc) as appropriate.

I have created an example below of what my final table should look like. Basically i want it to auto populate column A(Title)

I was wondering if anyone could help me with some pointers, lines of code or what to think about. I'm just starting with VBA so any help would be much appreciated

Thank you

Mario

A
B
C​
Item
Description
Title/Subtitle
1
Main title​
Title​
1.1
subtitle​
Subtitle​
Text​
2
Main Title​
Title​
2.1
Subtitle​
Subtitle​
2.2
Subtitle​
Subtitle​
3
Title​
Title​
Text​
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Why not just a formula

+Fluff.xlsm
ABC
1ItemDescriptionTitle/Subtitle
21Main titleTitle
31.1subtitleSubtitle
4 Text
52Main TitleTitle
62.1SubtitleSubtitle
72.2SubtitleSubtitle
83TitleTitle
9 Text
10
Data
Cell Formulas
RangeFormula
A2:A9A2=IF(C2="","",IF(C2="Title",COUNTIF(C$2:C2,C2),A1+0.1))
 
Upvote 0
Why not just a formula

+Fluff.xlsm
ABC
1ItemDescriptionTitle/Subtitle
21Main titleTitle
31.1subtitleSubtitle
4 Text
52Main TitleTitle
62.1SubtitleSubtitle
72.2SubtitleSubtitle
83TitleTitle
9 Text
10
Data
Cell Formulas
RangeFormula
A2:A9A2=IF(C2="","",IF(C2="Title",COUNTIF(C$2:C2,C2),A1+0.1))

Brilliant, thanks.
To be honest this is more of an exercise i was trying to solve for myself to learn VBA to get started with some simple examples, rather than a real life situation, and i hadn’t even considered a formula. However now that you mention it and show me how it can be done with a formula, i guess i can try and “translate” that to VBA code as an exercise.
Thank you
 
Upvote 0
You're welcome & thanks for the feedback.
To do it in VBA you would need the same logic, possibly using counters to keep track of what numbers you want
 
Upvote 0
To do it with a macro, you could use something like
VBA Code:
Sub mdelandro()
   Dim Cl As Range
   Dim TitleCount As Long
   Dim SubtitleCount As Double
   
   For Each Cl In Range("C2", Range("C" & Rows.Count).End(xlUp))
      If Cl.Value = "Title" Then
         TitleCount = TitleCount + 1
         SubtitleCount = TitleCount
         Cl.Offset(, -2).Value = TitleCount
      ElseIf Cl.Value = "Subtitle" Then
         SubtitleCount = SubtitleCount + 0.1
         Cl.Offset(, -2).Value = SubtitleCount
      End If
   Next Cl
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,287
Members
452,631
Latest member
a_potato

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