Add formulas to specific columns

alialeola

New Member
Joined
Jul 6, 2022
Messages
21
Platform
  1. Windows
Hi guys,

Could you help me insert the below formulas into my VBA code?
These are all empty columns, with the header on row 2, so formulas should be added to row 3.

1705778870854.png
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
which columns are required to be populated ??
ANND
Only 1 row ??
 
Upvote 0
Try
Excel Formula:
Sub InsertFormulas()
Range("B3").Formula = "1-H3/K3"
Range("D3").Formula = "H3*1.2"
Range("E3").Formula = "Round(K3*2,1)"
Range("G3").Formula = "L3*M3"
End Sub
 
Upvote 0
Could you help me insert the below formulas into my VBA code?
It would also help with inserting something into your existing vba code if we could actually see that code. ;)
My signature block below has more information about posting vba code.
 
Upvote 0
Can you post the code you have so far??
 
Upvote 0
Try
Excel Formula:
Sub InsertFormulas()
Range("B3").Formula = "1-H3/K3"
Range("D3").Formula = "H3*1.2"
Range("E3").Formula = "Round(K3*2,1)"
Range("G3").Formula = "L3*M3"
End Sub
Thanks, but columns where I need the formulas to be generated are not always H, K or M.
It should be something like this:
cell3, last column-6 = "1 - lastcolumn-7 / lastcolumn-4 "
cell3, last column-4 = "lastcolumn-7 * 1.2"
cell3, last column-3 = round(lastcolumn-4 * 2,1)
cell3, last column-1 = lastcolumn-3 * lastcolumn-2
 
Upvote 0
Can you post the code you have so far??
I actually have bits of codes I gathered or recorded myself.
Here you go:

VBA Code:
Sub Create_Master_file()

    Dim FirstRow As Long
    
'   Check to see if data in cell A1 and if not, insert row
    If Range("A1") <> "" Then
        Rows(1).Insert
    Else
'       Find first row with data in column A
        FirstRow = Range("A1").End(xlDown).Row
'       Delete rows if FirstRow greater than 2
        If FirstRow > 2 Then
            Rows("2:" & FirstRow - 1).Delete
        End If
    End If

    Dim LastRow As Long
    Dim LastCol As Long
    Dim iRow As Long
    Dim ws As Worksheet
    
    Set ws = ActiveSheet
    
    With ws
        LastRow = .Cells(.Rows.Count, 2).End(xlUp).Row
        LastCol = .Cells(2, .Columns.Count).End(xlToLeft).Column ' needs to be a 2 due to headers start on row 2
        .Columns(LastCol + 1).EntireColumn.Insert
        With .Cells(2, LastCol + 1)
            .Value = "xxx"
            .Interior.Color = 255
            .Font.Color = vbWhite
        End With
        .Columns(LastCol + 2).EntireColumn.Insert
        With .Cells(2, LastCol + 2)
            .Value = "xxx"
            .Interior.Color = 255
            .Font.Color = vbWhite
        End With
        .Columns(LastCol + 3).EntireColumn.Insert
        With .Cells(2, LastCol + 3)
            .Value = "xxx"
            .Interior.Color = 255
            .Font.Color = vbWhite
        End With
        .Columns(LastCol + 4).EntireColumn.Insert
        With .Cells(2, LastCol + 4)
            .Value = "xxx"
            .Interior.Color = 255
            .Font.Color = vbWhite
        End With
        .Columns(LastCol + 5).EntireColumn.Insert
        With .Cells(2, LastCol + 5)
            .Value = "xxx"
            .Interior.Color = RGB(226, 239, 218)
            .Font.Color = vbBlack
        End With
        .Columns(LastCol + 6).EntireColumn.Insert
        With .Cells(2, LastCol + 6)
            .Value = "xxx"
            .Interior.Color = RGB(226, 239, 218)
            .Font.Color = vbBlack
        End With
        .Columns(LastCol + 7).EntireColumn.Insert
        With .Cells(2, LastCol + 7)
            .Value = "xxx"
            .Interior.Color = RGB(226, 239, 218)
            .Font.Color = vbBlack
        End With
        .Columns(LastCol + 8).EntireColumn.Insert
        With .Cells(2, LastCol + 8)
            .Value = "xxx"
            .Interior.Color = RGB(226, 239, 218)
            .Font.Color = vbBlack
        End With
    Cells.Select
    With Selection.Font
        .Name = "Calibri"
        .Size = 9
    End With
    With Selection
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlCenter
    End With
    
    Range("A2").Select
    Range(Selection, Selection.End(xlToRight)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Borders(xlDiagonalDown).LineStyle = xlNone
    Selection.Borders(xlDiagonalUp).LineStyle = xlNone
    With Selection
        .BorderAround xlContinuous, xlThin, 0
        With .Borders(xlInsideVertical)
            .LineStyle = xlContinuous
            .ColorIndex = 0
            .Weight = xlThin
        End With
        With .Borders(xlInsideHorizontal)
            .LineStyle = xlContinuous
            .ColorIndex = 0
            .Weight = xlThin
        End With
    End With
    
    Dim sh As Worksheet, LstRw As Long, LstCol As Long, jRng As Range, x, xRng As Range

    Set sh = ActiveSheet

    With sh
        LstRw = .Cells(.Rows.Count, "B").End(xlUp).Row
        LstCol = .Cells(2, .Columns.Count).End(xlToLeft).Column
        Set jRng = .Range(.Cells(3, LstCol - 1), .Cells(LstRw, LstCol - 1))
        For x = LstCol - 4 To LstCol - 3
            Set xRng = .Range(.Cells(3, x), .Cells(LstRw, x))
            .Cells(1, x) = "=SUMPRODUCT(" & xRng.Address & "," & jRng.Address & ")"
        Next x
       .Cells(1, LstCol - 1) = "=sum(" & jRng.Address & ")"
       .Cells(1, LstCol) = "=sum(" & jRng.Offset(, 1).Address & ")"
    End With
        
End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,102
Messages
6,123,101
Members
449,096
Latest member
provoking

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