VBA Code

ongengthai1973

Board Regular
Joined
May 6, 2019
Messages
55
I have 2 files.

File A
Master templates dates
1636810072651.png



File B
Scheduling file.
My question : I would like to use VBA code when I key in date (3/10/22) in columns M and VBA code auto fill up the dates start from L3 to B3 (Backward scheduling) based on File A.

1636810147577.png
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Will the Master Template Dates in File A change? Or is it a fixed table?
Will the template dates always be in Row 3?
 
Upvote 0
Coding to enter in File B (the two "File A" must be changed to pathname\filename of master file A):

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
 
  On Error Resume Next
  Workbooks("File A").close savechanges:=false 'close the Masterfile, if opened
  On Error goto 0

  Dim Master As Workbook
  Dim Schedule As Worksheet
  Dim R, i, Delta As Integer
 
  If Target.Column <> 13 Then Exit Sub 'Only react to changes in column M
  If IsDate(Target) = False Then
    MsgBox "No Date"
    Exit Sub
  End If
 
  R = Target.Row 'Set variable R to the correct row
  Set Schedule = ActiveSheet 'Set Schedule File B Worksheet as Schedule
  Set Master = Application.Workbooks.Open("File A") Open Master File A and set as "Master"
 
  For i = 12 To 2 Step -1
    Delta = DateDiff("d", Master.Worksheets(1).Cells(3, i + 1), Master.Worksheets(1).Cells(3, i))
    Schedule.Cells(R, i) = DateAdd("d", Delta, Schedule.Cells(R, i + 1))
  Next i
 
  Master.Close savechanges:=False 'Close the Master File A
 
  Set Master = Nothing
  Set Schedule = Nothing
 
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,066
Messages
6,122,947
Members
449,095
Latest member
nmaske

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