check the header for duplicates

Shawn23

New Member
Joined
Mar 6, 2013
Messages
23
Hi,

Is there a way i can check the header if there are any duplicate? If so, I would like to use VBA to change it. For example, both Column D and Column F has the header named "WEEK". I would like Column D now to be "Week 1" and Column F would be "Week 2". This table will be populated, so every time it sees the word "week" in the header it will add one to it. WEEK #

Thank you for your help.

ITEMS Price item soldweekitem soldweektotal item soldtotal
Books $2.002 $4.007 $28.009 $32.00
Pens $1.504 $6.0010 $60.0014 $66.00
Papers $0.507 $3.5015 $52.5022 $56.00

<tbody>
</tbody>
 

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.
Shawn23,

Sample raw data:


Excel 2007
ABCDEFGH
1ITEMSPriceitem soldweekitem soldweektotal item soldtotal
2Books$2.002$4.007$28.009$32.00
3Pens$1.504$6.0010$60.0014$66.00
4Papers$0.507$3.5015$52.5022$56.00
5
Sheet1


After the macro:


Excel 2007
ABCDEFGH
1ITEMSPriceitem soldweek 1item soldweek 2total item soldtotal
2Books$2.002$4.007$28.009$32.00
3Pens$1.504$6.0010$60.0014$66.00
4Papers$0.507$3.5015$52.5022$56.00
5
Sheet1


Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

Code:
Option Explicit
Sub NumberWeek()
' hiker95, 04/13/2013
' http://www.mrexcel.com/forum/excel-questions/697026-check-header-duplicates.html
Dim n As Long, i As Long, fc As Long
n = Application.CountIf(Rows(1), "week")
If n > 1 Then
  For i = 1 To n
    fc = 0
    On Error Resume Next
    fc = Application.Match("week", Rows(1), 0)
    On Error GoTo 0
    Cells(1, fc) = Cells(1, fc) & " " & i
  Next i
End If
End Sub

Before you use the macro with Excel 2007 or newer, save your workbook, Save As, a macro enabled workbook with the file extension .xlsm

Then run the NumberWeek macro.
 
Upvote 0
Shawn23,

Thanks for the feedback.

You are very welcome. Glad I could help you again.

And, come back anytime.
 
Upvote 0
Hey Hiker95,

Excuse me for asking so much question. Still learning this language.

n = Application.CountIf(Rows(1), "week")

I know this starts from the beginning of row 1 but how do i specify to start at row one, Column F. Meaning the code will skip column D and start from Column F and so on. Ive tried changing countif but no luck.

Thank you
 
Upvote 0
Shawn23,

Something like?

Rich (BB code):
'from this:
n = Application.CountIf(Rows(1), "week")


'to this:
n = Application.Countif(Range("F1:Z1"),"week")
 
Upvote 0
Hey Hiker95,

Thats what I had in mind but its not working. Instead of the first "WEEK" column being skipped. its actually the last one being skipped.
 
Upvote 0
Shawn23,

In order to continue:

Can you post a screenshot of the raw data worksheet, and, post a screenshot of the worksheet results (manually formatted by you) that you are looking for?

To post your data, you can download and install one of the following two programs:
Excel Jeanie
MrExcel HTML Maker


Even better, so that we can get it right the next time:

You can upload your workbook to Box Net,
sensitive data scrubbed/removed/changed
mark the workbook for sharing
and provide us with a link to your workbook.
 
Upvote 0
before macro



Excel 2007
ABCDEFGHIJK
5ITEMSPriceitem soldweekitem soldweektotal item soldweekweekweektotal
6Books$ 2.002$ 4.007$ 28.009$ 32.00
7Pens$ 1.504$ 6.0010$ 60.0014$ 66.00
8Papers$ 0.507$ 3.5015$ 52.5022$ 56.00
Sheet3


after macro

Excel 2007
ABCDEFGHIJK
5ITEMSPriceitem soldweek 1item soldweek 2total item soldweek 3week 4weektotal
6Books$ 2.002$ 4.007$ 28.009$ 32.00
7Pens$ 1.504$ 6.0010$ 60.0014$ 66.00
8Papers$ 0.507$ 3.5015$ 52.5022$ 56.00
Sheet3
 
Upvote 0

Forum statistics

Threads
1,203,094
Messages
6,053,504
Members
444,667
Latest member
KWR21

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