VBA Name New column & Use Column Header as Range

th259

New Member
Joined
Oct 24, 2023
Messages
35
Office Version
  1. 365
Platform
  1. Windows
VBA Code:
'Insert Column to the Right of Encumbered Column
Rows(1).Find("Encumbered").Offset(, 1).EntireColumn.Insert
Columns("V:V").Select
    Range("V1").Select
    ActiveCell.FormulaR1C1 = "Total"
    Dim LR As Long 'LR = Last row
    LR = Cells(Rows.Count, "A").End(xlUp).Row 'Finds last row in column A
    Range("V2").FormulaR1C1 = "=RC[1]+RC[2]-RC[74]"
    Range("V2:V" & LR).FillDown 'Copy the formula to the last row
    Range(Selection, Selection.End(xlDown)).Select
    Columns("V:V").EntireColumn.AutoFit
I use the above to insert a new column to the left of column Encumbered, which happens to be column V, how do I immediate insert a header titled "Total" to the first row and how do I use that header in a formula.
 
Last edited by a moderator:

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
When posting vba code in the forum, please use the available code tags, not quote tags. My signature block below has more details. I have swapped the tags for you this time. 😊


I use the above to insert a new column to the left of column Encumbered
I think you mean to the right?

how do I immediate insert a header titled "Total" to the first row
You can insert the column & heading like this
VBA Code:
With Rows(1).Find("Encumbered")
  .Offset(, 1).EntireColumn.Insert
  .Offset(, 1).Value = "Total"
End With

how do I use that header in a formula.
You will need to explain that further.
Are you talking about the formula(s) being inserted into that new column?
What exactly is the formula supposed to do?
 
Upvote 0
I changed the order a little bit, but the newly inserted Total Column is column X, the 3 columns to the right are titled, Actual, DR, and CR, respectively. I wanted to insert a simple formula, adding the numbers in the Actual and DR column then subtracting the number in the CR column. Below is what I currently have but I want to change it so that the range reference the column header instead of X2.
With Rows(1).Find("Encumbered")
.Offset(, 1).EntireColumn.Insert
.Offset(, 1).Value = "Total"
End With
Dim LR As Long 'LR = Last row
LR = Cells(Rows.Count, "A").End(xlUp).Row 'Finds last row in column A
Range("X2").FormulaR1C1 = "=RC[1]+RC[2]-RC[3]"
Range("X2:X" & LR).FillDown 'Copy the formula to the last row
Range(Selection, Selection.End(xlDown)).Select
Columns("X:X").EntireColumn.AutoFit
 
Upvote 0
I'm sorry, I missed the first sentence.

VBA Code:
With Rows(1).Find("Encumbered")
.Offset(, 1).EntireColumn.Insert
.Offset(, 1).Value = "Total"
End With
Dim LR As Long 'LR = Last row
LR = Cells(Rows.Count, "A").End(xlUp).Row 'Finds last row in column A
Range("X2").FormulaR1C1 = "=RC[1]+RC[2]-RC[3]"
Range("X2:X" & LR).FillDown 'Copy the formula to the last row
Range(Selection, Selection.End(xlDown)).Select
Columns("X:X").EntireColumn.AutoFit
 
Upvote 0
Thanks, but next time copy the code from your vba window, not the post in the forum. From the vba window the various indentations would have been preserved whereas they were lost in the quote tags & I think that you have copied from there. :)

Give this a try with a copy of your workbook.

VBA Code:
Sub Test()
  Dim LR As Long 'LR = Last row
  
  LR = Cells(Rows.Count, "A").End(xlUp).Row 'Finds last row in column A
  
  With Rows(1).Find("Encumbered")     'Find 'Encumbered' in row 1
    .Offset(, 1).EntireColumn.Insert  'Insert Column to the Right of Encumbered
    With .Offset(, 1)
      .Resize(LR).FormulaR1C1 = "=RC[1]+RC[2]-RC[3]"  'Insert formulas in new column
      .Value = "Total"                                'Add heading
      .EntireColumn.AutoFit                           'Autofit column width
    End With
  End With
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,223,888
Messages
6,175,212
Members
452,618
Latest member
Tam84

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