You are here:

Excel/change column headers if repeated


Hello Tom,

i have worksheet which contains due dates, due amounts for products, balance amount remaining for products. But in this case the headers are repeated and i want to add 'bal' word if they repeat.
Like col A is due date
ColB Prod1, ColC Prod2, ColD Prod3, ColE Prod4, ColF Total Due
ColG Prod1, ColH Prod2, ColI Prod3, ColJ Prod4, ColK Balance Due
I want ColG to ColJ to add word 'Bal' like Prod1_Bal,Prod2_Bal etc then copy those rows where Products contains amount above 0 and due date(col A) less than today to seperate sheet.
Products is dynamic and varies
Thanks a lot


this does what I understand you to describe.  It assumes that you headers are in row 1.  

When you say "Products contains amount above 0" I assume in your example that would mean Column B:E in a single row sums up to a value greater than zero.

Also it assumes there will be duplicate product names in row 1 as you describe.

Sub abc()
Dim r As Range, cell As Range, sh As Worksheet, r1 As Range
Dim fcol As Long
Set sh = Worksheets("Another")  '<== name of sheet to copy data to
Set r = Range("B1", Cells(1, Columns.Count).End(xlToLeft))
For Each cell In r
 Set r1 = Range("A1", cell.Offset(0, -1))
 If Application.CountIf(r1, cell) > 0 Then
   If fcol = 0 Then fcol = cell.Column
   cell.Value = cell.Value & "_bal"
 End If
Set r = Range("A2", Cells(Rows.Count, "A").End(xlUp))
For Each cell In r
If cell.Value > 1 And cell.Value < Date Then
  If Application.Sum(cell.Offset(0, 1).Resize(1, fcol - 3)) > 0 Then
    cell.EntireRow.Copy sh.Cells(sh.Rows.Count, 1).End(xlUp).Offset(1, 0)
  End If
End If

End Sub

Tom Ogilvy

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


All Answers

Answers by Expert:

Ask Experts


Tom Ogilvy


Selected as an Excel MVP by Microsoft since 1999. Answering Excel questions in Allexperts since its inception in 2001. Able to answer questions on almost all aspects of Excel's internal capabilities. If seeking a VBA solution, please specify that in your question itself so I give you the answer you want. [Excel has weak protection - if you are distributing an application, I don't answer questions on how to protect your project from your users.]


Extensive experience.

Master of Science (MS) degree Operations Research (ORSA)

Awards and Honors
Microsoft MVP in Excel.

©2017 All rights reserved.

[an error occurred while processing this directive]