VBA formula in first blank and filled down to end of data

Orfevre

New Member
Joined
Jul 11, 2022
Messages
25
Office Version
  1. 365
Platform
  1. Windows
Hi, I have the below code where in all other columns there is many populated rows, what I need this formula to do in column F is to find the first blank, then place the formula in it and fill it down to the last row.

What is currently happening is I have the range as F26 as this is usually first blank but this could change and I want the code to identify this and also have the formula dynamically know what row it is on, so for example if one month the first blank was in cell F30 the range would find it and the formula would start as E30*G30.

Any help would be greatly appreciated.

VBA Code:
Private Sub calc()
Dim lastrow As Long
Dim rng As Range
lastrow = ThisWorkbook.Worksheets("Indiv").Cells(Rows.Count, 1).End(xlUp).Row
Set rng = Range("F26:F" & lastrow)
rng.Formula = "=Round((E26*G26),2)"
End Sub
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
What row does your data start in? In other words, is row 1 a header row, with your data starting in row 2?
 
Upvote 0
Try this on a copy of your data:
EDITED

VBA Code:
Option Explicit
Sub Indiv_Formulas()
    Dim ws As Worksheet
    Set ws = Worksheets("Indiv")
    
    Dim LRow As Long
    LRow = ws.Cells(Rows.Count, 1).End(xlUp).Row
    
    Dim c As Range, i As Long
    For Each c In ws.Range(ws.Cells(3, 6), ws.Cells(LRow, 6))
        If c = "" Then
            i = c.Row
            Exit For
        End If
    Next c
    
    If i > 0 Then
        With ws.Range(ws.Cells(i, 6), ws.Cells(LRow, 6))
            .FormulaR1C1 = "=ROUND(RC5*RC7,2)"
            .Value = .Value
        End With
    End If
End Sub
 
Upvote 0
Solution
Try this on a copy of your data:
EDITED

VBA Code:
Option Explicit
Sub Indiv_Formulas()
    Dim ws As Worksheet
    Set ws = Worksheets("Indiv")
   
    Dim LRow As Long
    LRow = ws.Cells(Rows.Count, 1).End(xlUp).Row
   
    Dim c As Range, i As Long
    For Each c In ws.Range(ws.Cells(3, 6), ws.Cells(LRow, 6))
        If c = "" Then
            i = c.Row
            Exit For
        End If
    Next c
   
    If i > 0 Then
        With ws.Range(ws.Cells(i, 6), ws.Cells(LRow, 6))
            .FormulaR1C1 = "=ROUND(RC5*RC7,2)"
            .Value = .Value
        End With
    End If
End Sub
Absolutely brilliant thank you, I was playing around with the FormulaR1C1 but was struggling with it.
 
Upvote 0
Cross-posting (posting the same question in more than one forum) is not against our rules, but the method of doing so is covered by #13 of the Forum Rules.

Be sure to follow & read the link at the end of the rule too!

Cross posted at: VBA formula in first blank and filled down to end of data
If you have posted the question at more places, please provide links to those as well.

If you do cross-post in the future and also provide links, then there shouldn’t be a problem.
 
Upvote 0

Forum statistics

Threads
1,214,643
Messages
6,120,702
Members
448,980
Latest member
CarlosWin

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