Hi, there are three columns(open date, close date and months).
I need to use VBA to automate date adding process.
If there is a value in column A, I want to add column A and C together and put the result in column B.
For example, in cell A2, I want to input date 6 months after 02/08/2021(cell A1), which will be 02/02/2022.
I have tried to code VBA, but it keeps getting type mismatch error.
If anyone knows how to fix this, please help.
Here is the mini sheet:
Here is the VBA code I wrote:
I am not quite sure if this is the right way to do, since I am new to VBA.
I'd appreciate a lot if anyone could help.
I need to use VBA to automate date adding process.
If there is a value in column A, I want to add column A and C together and put the result in column B.
For example, in cell A2, I want to input date 6 months after 02/08/2021(cell A1), which will be 02/02/2022.
I have tried to code VBA, but it keeps getting type mismatch error.
If anyone knows how to fix this, please help.
Here is the mini sheet:
Practice.xlsm | |||||
---|---|---|---|---|---|
A | B | C | |||
1 | Open Date | Close Date | Months | ||
2 | 2/8/2021 | 6 | |||
3 | 9/10/2022 | 3 | |||
4 | 3/3/2022 | 9 | |||
5 | |||||
6 | |||||
7 | |||||
8 | |||||
9 | |||||
10 | |||||
11 | |||||
12 | |||||
13 | |||||
14 | |||||
15 | |||||
16 | |||||
17 | |||||
18 | |||||
19 | |||||
Sheet1 |
Here is the VBA code I wrote:
VBA Code:
Sub AddDate()
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets("Sheet1")
Dim startcell As Range
Dim startcolumn As Range
Dim endcell As Range
Dim endcolumn As Range
Dim monthcell As Range
Dim monthcolumn As Range
Set monthcolumn = ws.Range("C1:C65565")
Set startcolumn = ws.Range("A1:A65565")
Set endcolumn = ws.Range("B1:B65565")
For Each startcell In startcolumn
For Each monthcell In monthcolumn
For Each endcell In endcolumn
If Not IsEmpty(startcell) Then
endcell = DateAdd("m", monthcell, startcell)
End If
Next
Next
Next
End Sub
I am not quite sure if this is the right way to do, since I am new to VBA.
I'd appreciate a lot if anyone could help.