VBA Code to AutoFill a column based on date

sergo_wasp

New Member
Joined
Aug 27, 2022
Messages
10
Office Version
  1. 365
Platform
  1. Windows
Hi everyone,

I would really appreciate if you could help me with one issue.
I need to write VBA code that is going to drag the formula down in column B depending on what date is today in column A.
If the date is today or 27.08.2022 then column B will be filled out until including B6 cell and so on.

Thank you so much!

Wish you a nice day!

sergo_wasp
 

Attachments

  • Capture.PNG
    Capture.PNG
    34 KB · Views: 18

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
I really do not think you need a formula in the cell if you're using Vba
With this script any time you enter todays date in column A
The sum of columns H and I same row will be entered in column B same row.

This is an auto sheet event script
Your Workbook must be Macro enabled
To install this code:
Right-click on the sheet tab
Select View Code from the pop-up context menu
Paste the code in the VBA edit window

The script will run when you enter Todays Date in column A

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
'Modified  8/27/2022  11:33:19 PM  EDT
If Target.Column = 1 Then
    If Target.Value = Date Then Target.Offset(, 1).Value = Target.Offset(, 7).Value + Target.Offset(, 8).Value
    
End If
End Sub
 
Upvote 0
I really do not think you need a formula in the cell if you're using Vba
With this script any time you enter todays date in column A
The sum of columns H and I same row will be entered in column B same row.

This is an auto sheet event script
Your Workbook must be Macro enabled
To install this code:
Right-click on the sheet tab
Select View Code from the pop-up context menu
Paste the code in the VBA edit window

The script will run when you enter Todays Date in column A

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
'Modified  8/27/2022  11:33:19 PM  EDT
If Target.Column = 1 Then
    If Target.Value = Date Then Target.Offset(, 1).Value = Target.Offset(, 7).Value + Target.Offset(, 8).Value
   
End If
End Sub
Thank you for your answer.

However, I showed a dummy version of my data. The real one is far more complex.

There are a lot of formulas and columns.
So the real issue for me is to write a code that is going to drag the formulas down in columns (B:Z) depending on what date is today in column A. By date I mean function TODAY()-1 in excel. So it should be totally automated.

Something like
Case when Date is today()-1
Then formula in column B dragged down to B6 based on column A

But today()-1 is static so the second line shall be dynamic.

Hope I clarified better my issue. Thanks again!
 
Upvote 0
Why not build the date check into the formula, then you never have to drag it anywhere after initially filling the whole column?

22 08 28.xlsm
ABHI
1Date
225/08/2022312
326/08/2022624
427/08/2022936
528/08/2022 48
629/08/2022 510
730/08/2022 612
Fill to date
Cell Formulas
RangeFormula
B2:B7B2=IF(A2<TODAY(),SUM(H2:I2),"")
 
Upvote 0
Why not build the date check into the formula, then you never have to drag it anywhere after initially filling the whole column?

22 08 28.xlsm
ABHI
1Date
225/08/2022312
326/08/2022624
427/08/2022936
528/08/2022 48
629/08/2022 510
730/08/2022 612
Fill to date
Cell Formulas
RangeFormula
B2:B7B2=IF(A2<TODAY(),SUM(H2:I2),"")
That would be a good solution but my simplification was too bad. Apologies.
There is actually a link in each cell in B not a formula.
Such as in B2 =‘\\dummy001\DUM\[db_dummy.xls]db_random’!B27
In B3
=‘\\dummy001\DUM\[db_dummy.xls]db_random’!B28
 
Upvote 0
Why not build the date check into the formula, then you never have to drag it anywhere after initially filling the whole column?

22 08 28.xlsm
ABHI
1Date
225/08/2022312
326/08/2022624
427/08/2022936
528/08/2022 48
629/08/2022 510
730/08/2022 612
Fill to date
Cell Formulas
RangeFormula
B2:B7B2=IF(A2<TODAY(),SUM(H2:I2),"")
Each month that link is different so I cannot fill down the whole column with that formula you pasted.. unfortunately. Is there any other solution to this you think?
 
Upvote 0
Good idea to give a fairly true snapshot of your actual requirement at the start. ;)

Does this do what you want?

VBA Code:
Sub Fill_Down()
  Dim rDateFound As Range
  
  Set rDateFound = Columns("A").Find(What:=Date)
  If Not rDateFound Is Nothing Then
    With Range("B2")
      .Replace What:="=", Replacement:="|=", LookAt:=xlPart
      .AutoFill Destination:=.Resize(rDateFound.Row - .Row + 1)
      .Resize(rDateFound.Row - .Row + 1).Replace What:="|=", Replacement:="=", LookAt:=xlPart
    End With
  End If
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,214,958
Messages
6,122,475
Members
449,087
Latest member
RExcelSearch

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