ana_c

New Member
Joined
May 26, 2021
Messages
17
Office Version
  1. 365
Platform
  1. Windows
Hi,
I'm new to VBA and would greatly appreciate your help with this query

I have a sheet that looks like this
1622192415154.png


I would like to add column titles in the highlighted cells based on the first left column that is not blank. For e.g. D1 will have the title -> C2+"_1" a.k.a "Column2_1" and cell E1 will be "Colum2_2".
Similarly in cell O1, it will be "Column5_1", P1="Column5_2" etc.

In total I have over 100 columns to go through and apply the same formula, however since these are header titles, we can restrict it to row 1.

Final desired output -
1622192729049.png


My data starts in B1

Thanks
 
Surely that would be column XFD?

Can I safely use row 2 to determine the last column that does have data and stop there?
Or might row 2 have last data in column BV but row 10 might go further, say to column CA?

yeah, row 2 might have last data in column BV but row 10 can have data further down
 
Upvote 0

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
yeah, row 2 might have last data in column BV but row 10 can have data further down
And so you would want the headings to go to whatever column that data further down is in? That is, the furthest right column that has data anywhere?
 
Upvote 0
OK, try

VBA Code:
Sub Add_Titles_v3()
  Dim c As Range
  Dim Bits As Variant, Suff As Variant
  Dim LastCol As Long
  
  LastCol = Cells.Find(What:="*", LookIn:=xlValues, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
  For Each c In Rows(1).Resize(, LastCol).SpecialCells(xlBlanks)
    Bits = Split(c.Offset(, -1).Value, "_")
    Suff = Bits(UBound(Bits))
    If UBound(Bits) = 0 Or Not IsNumeric(Suff) Then
      c.Value = c.Offset(, -1).Value & "_1"
    Else
      Bits(UBound(Bits)) = vbNullString
      c.Value = Join(Bits, "_") & Suff + 1
    End If
  Next c
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,543
Messages
6,120,123
Members
448,947
Latest member
test111

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