Macro To Insert Formula Based On Column Header

Feb 24, 2009

I have to use several dozen pivot tables a day. I already use a couple of macros to do a lot of the tedious formatting, but if I could automate this it would save me a lot more time. I have been trying to both write a code and modify code written by the recorder but have not even come close to anything that works. What I want is to have a macro that searches through the column header for predefined names and if it comes across one of these names it will insert a specific formula, based on its name, in the first cell below the header. Then it will autofill to the last row and move on to searching for the next predefined header name. If that name is not found, it will move to the next predefined header name. One major problem is that the formula to insert under any specific header name is based on other columns in the table that aren't always in the same range. So, not only do I need it to search for columns to place a formula in, that formula has to search for the appropriate column to find the correct value to use in the calculation.

For example, one predefined column name would be Avg Price. The formula for that column would be (Sales/Qty Sold), but these 2 columns could be located anwhere in the table. How can I get it, once it has found a column that needs a formula, to locate the correct column/cell to get the correct value from the sales and Qty Sold columns?

View 9 Replies


Macro To Insert Column / Formula?

Feb 18, 2012

I need a macro that will insert a column next to column B, and insert formulas into the new respective cells. forumula needs to be in the macro, it won't be located somewhere else on the sheet (for simplicity's sake, let's just say the formula is =A1+B1, then A2+B2 in the next row, etc). Also, it needs to stop inserting formulas when there's no more data in column B.

View 1 Replies View Related

Insert Column Macro Based Variable String Letter

Jun 11, 2008

I'm working on some code that's part of a userform. To illustrate what I need, I will give an example. A column letter, 'J' for example, is stored in colNum.Value taken from the userform. I need both a column inserted before column J, and data entered into that new column in row 2 (thus J2, which would now be blank).

View 4 Replies View Related

VBA Macro To Insert Horizontal Page Breaks Based On Criteria Of 1 Column

Jan 10, 2010

I want to achieve is a procedure that inserts horizontal page breaks at certain parts of the sheet where there is a cell equal to 2. Here is the code I have so far.

Sub insert_pagebreak()
Dim printbreak_cell As Range
Dim j As Long
Dim i As Long
Set printbreak_cell = Range("AD1")
j = 1
For i = 1 To 100
If printbreak_cell.Value = 2 Then
Set ActiveSheet.HPageBreaks(j).Location = printbreak_cell
j = j + 1
End If
Set printbreak_cell = printbreak_cell.Offset(1, 0)
Next i
End Sub

Everything works until the cell value reaches a 2, and then once it goes into the If statement I get a 'Application-defined or object-defined error' at the below line.

Set ActiveSheet.HPageBreaks(j).Location = printbreak_cell.............

View 3 Replies View Related

To Write A Macro That Will Insert A Formula Directly Into The Cells In Column M Of My Worksheet

Aug 9, 2007

I am trying to write a macro that will insert a formula directly into the cells in column M of my worksheet.

The worksheet has about 3400 rows. When I run the macro it does not adjust the cell reference in the formula so I am getting the same value in all 3400 rows.

I am using the following ....

View 9 Replies View Related

Return Column Header Based On Column Criteria And Number Value

Feb 7, 2014

I have the following sample data set and I'm trying to return the appropriate column header based on criteria (i.e. DDD) and a number value which will be somewhere within the range of the table. In example below, the value returned should be Header 2 because the value is greater than those in Header 1 column (range H9 to L26).

Here's data table:

CriteriaHeader 1Header 2Header 3Header 4

DDD10.00Header 1>>>=INDEX($I$9:$L$9,MATCH(I29,INDEX($I$10:$L$26,MATCH(H29,$H$10:$H$26),)))

View 3 Replies View Related

Formula To Display Column Header As Result When Any Value Exists In Cells Under Column

Jul 25, 2014

I am trying to write a formula where the column header of the row in which a value other than 0 exists, will display for each instance (row) where a value exists in an array spanning 3 columns. So the result cell could be any of the three column headers, or a combination thereof.

I started the formula in P2 of the GL Detail-2012 tab. File attached.

Here is what I started: =INDEX($M$1:$O$1,SUMPRODUCT(COUNTIF($M$2:$O$67756,))). Not working.

View 7 Replies View Related

Return Value From Column Based On Header Value?

Jul 16, 2013

Cells A3:A500 contain product numbers, e.g. 1001, 1002, 1003 etc.. etc...
Cells G2:N2 are column headers, e.g. North2011, South2011, North2012, South2012 etc... etc...
Cell F2 is a lookup that will return a year, e.g. 2011, 2012, 2013 etc... etc...

Against each product is a "yes" if it was sold in the north or south region for each year. This could be both north and south, one or the other or none at all.

What I would like is a formula in cells E3:E500 that looks at the year in cell F2 and if there is a "yes" in either the North or South for that year to return "yes" otherwise blank.

I am unable to upload a sample document due to firewalls.

View 5 Replies View Related

Return Column Header Based On Row Value

Mar 11, 2014

Evaluation Results

Vendor Lowest Price
Vendor 2nd Lowest Price

[Code] .......

I have above table and wants to get the formula for getting the 1) Vendor Name of the Lowest Price per item 2) Vendor Name of the 2nd Lowest Price per item 3) and be able to copy down the formula for other items.

View 5 Replies View Related

Formula To Return Column Header If There Is Data In The Column

Sep 18, 2012

I need a formula to return column header if there is data in the column, any data.But I also need to return the second, third, fourth header with data too

In column A row 2, I have =IF(M8"",$M$1,IF(N8"",$N$1,IF(O8"",$O$1,IF(P8"",$P$1,IF(Q8"",$Q$1,IF(R8"",$R$1,IF(S8"",$S$1,IF(T8"",$T$1,""))))))))

This returns the row 1 header for the first instance of data in row 2 columns M thru T But in B2, I need the second header with data in the column.

I need the results in yellow (1st, 2nd, 3rd, 4th) from the array to it's left

1St match2nd 3rd 4thColumn MColumn NColumn OColumn PColumn QColumn R
Column MColumn PColumn QData data2Data3
Column NColumn OColumn PColumn R Data Data2 data3 data4

Would use match/index but can't increment it to the second or 3rd match

View 2 Replies View Related

Insert Group Header Based On Consistent Value Within The Group?

Feb 10, 2014

I have several groups of data in the same sheet. Each group has two blank rows above the first row of data. Each group has column "B" in common (e.g., "Phone" in the example attached). I'd like to add a title/label of "Phone" to the first column in the row immediately preceding the first row of data for that group.

View 1 Replies View Related

Delete Column Based On Column Header

Apr 29, 2009

I have a spreadsheet with several columns (all column names are in row 1).

I have 2 of column names. Note that their actual column# might change so the macro would have to look for the column name and NOT the column position ...

View 11 Replies View Related

Dynamic Lookup Based On Column Header

Jun 21, 2013

I'm trying to calculate a figure in a report tab that reads off the data tab and looks for a specific column based on the header of that column. My explanation is admittedly poor but I've attached a sample file with an outline of what I'm trying to do which I hope makes things clearer.

Sample File.xlsx

View 2 Replies View Related

Lookup Column Header Based On Cell Value

Aug 11, 2009

I have a spreadsheet an excerpt of which is as follows:

1 Doors Security Vehicle Key

Want I want to happen is to return the column header where there for a cell which contains "TRUE" in a blank column. If there isn't a cell with the value "TRUE" then to return "N/A". In the above example in blank column P for Row 2 would be "Doors", Row 3 would be "Security,Vehicle" and Row 4 would be "N/A".

In my actual spreadsheet I have 12 columns (L to W) which have the TRUE or FALSE values.

I know it has something to do with either HLOOKUP, INDEX and MATCH but can't seem to get it right. Any ideas?

View 4 Replies View Related

Looking Up Column Header Value Based On Color And Position In Row

Dec 12, 2009

I am trying to create a schedule using colors. The colors will indicate the start time, break time, lunch time, break time (second break) and end time. see attached document.

Thank you for attempting to solve this puzzle using formulas and or VBA code. I am trying to automate the schedule so the numerical times automatically appear when I "paint" the schedule.

View 5 Replies View Related

Formula To Return Column Header?

Jul 7, 2014

I am looking at formula to return the column header, see example attached.

View 2 Replies View Related

Formula To Retrieve Column Header?

Feb 27, 2013

I have a table, lets say from C4:J15. Basically all the column headers are dates(row 4). I want to input a formula into column D(D5:D15) that basically retrieves the column header of the first instance in which that row has data in a cell. So say Row8 has the word 'Medicare' in C8, and there is a dollar amount in cell H8, in cell D8 I would like to retrieve the column header of 2/3/2013 that is in cell H4(the header). If there is a dollar amount in the row, that means there is a variance, and I'd like to know the first date in which there is a variance so I don't have to search the whole row.

View 2 Replies View Related

Lookup Column Header Value Based On Matching Table Value

May 16, 2013

I'd like a formula that'll return the column header by matching a lookup value with a table in the second sheet.

eg: sheet 1



[Code] .......

The formula should match the name in A2, John, with value from the table in sheet 2 and return the correct region, this case North.

View 1 Replies View Related

Selectively Copying Cells Based On Column Header?

Jul 9, 2013

I'm trying to copy cells from a column in one workbook to cells in a column in another workbook based on what's in a header column.

Such as

Original workbook
Mat Qty
A 1
C 3
D 2
E 4
G 5
Total 15

Target workbook becomes

Mat Qty -> Mat Qty
A -> A 1
B -> B
C -> C 3
D -> D 2
E -> E 4
F -> F
G -> G 5

My code so far (which might not be worth reading) is:

Sub CopyColumnToWorkbook()
Dim sourceColumn As Range, targetColumn As Range, sourceColumnValue As Range, targetColumnValue As Range
Dim mySourceCell As Range, myTargetCell As Range


I'm getting a runtime error ('1004'. Application-defined or object-defined error) on the Copy statement within the If sttement.

View 3 Replies View Related

Delete Rows Based On Criteria While Referencing Column Header

Jun 6, 2007

I'm trying to use VBA code to delete certain rows based on a couple of criteria as follows:

1) If column header is "TERMINAL NAME" and the cell value in that column is "BONDDESK", I need the entire row deleted.

2) Similiarly, if the column header is "PRODUCT TYPE" and the cell value in that column is "CORP", that entire row needs deleted as well.

The information is coming from another source, so the columns may change from day to day, i.e, "TERMINAL NAME" may be in Column L one day but in Column O another day.....

View 6 Replies View Related

Convert Numbers To Postitive/Negative Based On Column Header

Mar 7, 2008

I have this data source file that comes from a back office program, and basically what i want it to do is to turn the number negative in column C when the header of that column in column A is WITHDR and leave it positive when it is CONTR. What i was thinking was that it could go to column A from column C and go up each cell until the cell is not a date and multiply the column C number by -1 depending the column A header equals WITHDR and do that for every cell in column C, but I dont know how to proceed, im new at VBA.

View 8 Replies View Related

Macro To Create Named Ranges Based On A Header Row.

Oct 14, 2009

I need a macro that can set up some named ranges using the text in the header row and the sheet name. The header row will always be in row one, but the number of columns will be dynamic. The amount of rows in the range will need to be dynamic also. To clarify, every used cell stating in row 2 in a column will be the named range with the sheetname+text in row one of the column the actual name.

View 4 Replies View Related

Macro To Select Range With Column Header?

Mar 12, 2003

How would a code look like to select a range with a column header on top. in the code you provided, it selected the entire W column. How would you select the range given that the W column was called Address?

Range(Selection, Selection.End(xlDown)).Name = "Problem_Area"

View 6 Replies View Related

Finding Particular Column By Its Field Header Through Macro

Mar 12, 2012

I am trying to write a Macro that will identify a particular column by its field header (i.e. first row) and will run a specific set of code on that particular column.

My current code looks like this, coverting New York to NY:-

Cells.Replace What:="New York", Replacement:="NY", LookAt:=xlPart, SearchOrder:=xlByRows

This code applies to 'New York' found in the entire sheet. I need to identify a column that is named 'State' and then make the code run ONLY on that column.

View 6 Replies View Related

Rearrange (sort) Columns Based On Number In Column Header String

Apr 3, 2014

I want to rearrange(sort asscending) columns based on numerical value in column header string through VBA macro. Please check attachment.

i.e. (Present Data)
# A B C D
1 col.1 col.4 col.3 col.2

(Output Data )
# A B C D
1 col.1 col.2 col.3 col.4


View 2 Replies View Related

VBA Loop Through Worksheet And Format Each Column As Text, Or Date Based On Header

Jun 30, 2006

I am trying to figure out a way to condense my code (improve my code) Right now I am using a Macro in Excel to go through a workbook by selecting each column individually and if there is something in the header then formatting that column as text. The problem with the way I am currently doing it, is that due to size limitations on the VBA code, I can only select columns A to Z, and if there is more data in the worksheet it is not formatted. Also I would like the code to check the header and if the word date is in there, format it as a date instead. Also if there is a way to delete any invalid ranges in the workbook

Sub Format()
Dim VarFileName As String
Dim VarPath As String
Dim VarSavein As String
Dim wsheet As Worksheet
VarSavein = Sheets("sheet1").Range("C2").Value
VarFileName = Sheets("sheet1").Range("A2").Value
VarPath = Sheets("sheet1").Range("B2").Value
Workbooks.Open VarPath & VarFileName
For Each wsheet In ActiveWorkbook.Worksheets
Selection.TextToColumns Destination:=Range("A1"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
Semicolon:=False, Comma:=False, Space:=False, Other:=False, FieldInfo _
:= Array(1, 2), TrailingMinusNumbers:=True..........................................

View 3 Replies View Related

VBA Code To Copy And Paste To Specific Rows Based On Matching Column Header?

Dec 7, 2011

I have a spreadsheet and I was wondering if there is a way to copy from a specific range in say A1:A5 in Sheet1 and paste into Sheet2 where the column matches cell A1 in Sheet1.

I am trying to do this in VBA and I was wondering if there is a way to do this.

View 9 Replies View Related

Copy Column Data From Multiple Worksheets To Single Worksheet Based On Header Criteria

Oct 18, 2013

I have a workbook with many sheets of similar but not identical data. I need to extract columns from each sheet based on 5 header criteria and paste them to a single sheet. Each worksheet contains these 5 criteria.

I've been working with the VBA script I've pasted below. It's grabbing the 5 column criteria that I have in sheet 12, and comparing them to sheet 1 in the workbook, then copying them to sheet 12. This much is good, but I need the script to also return the data from the other worksheets as well. I've tried modifying the script based on other loop functions in other scripts I've found, but I'm not having any luck.

View 14 Replies View Related

Macro To Copy Data From All Columns Containing Header To A Single Column In Workbook

Apr 11, 2014

I am a macro newbie and I think this is beyond me.

I've been trying all morning with no success to make a macro that will copy data from all columns of one workbook containing specified header text to a single column in a different workbook.

So for example, I've got a workbook called coupon barcodes that has multiple tabs for each person redeeming coupons on sheets 2-88 (sheet one can be skipped) and some of the columns are labeled "voucher 1" in cell A4 or B4 or C4 etc. with a list of all the coupon barcodes that customer redeemed below that. I want all of the data from all of the columns in this workbook that have the header "voucher 1" to be copied and pasted into one column (order doesn't matter) also labeled "voucher 1" in another workbook I have open called vouchering database.

There must be a way to do this that is easier than searching the internet all afternoon again

View 3 Replies View Related

Insert Header And Footer

May 14, 2014

Ive created a simple VBA code thats suppose to insert a header and footer. The header works but the footer doesn't. Does it matter that my footer is in "Quotes"? I would like for it to say Page of Page so the footer will say 1 of 4! I feel like its something simple just not sure...

With ActiveSheet.PageSetup
.CenterHeader = "HSIS Phone Bill"
.CenterFooter = "Page &[Page] of &[Pages]"

End With

View 3 Replies View Related

Copyrights 2005-15, All rights reserved