Deduct month to number

sbv1986

Board Regular
Joined
Nov 2, 2017
Messages
87
AB
1202211Month number
2202209Month number 2 = A1 - A2
3202206Month number 5 = A1 - A3
4202203Month number 8 = A1 - A4
5202112Month number 11 = A1 - A5
6202111Month number 12 =A1 - A6
7202109Month number 15 = A1 - A7
8202011Month number 24 =A1 - A8

Hi all

I have data like table above, Column (A) is number format as: YYYYMM

Now I want A1 deduct each row belove (A2:A8) then fill Result in Column (B)
For example:
A1 - A2, then B2 must be 2 months
A1 - A5, B5 = 11
A1 - A8, B2 = 24

Please help me to dot his by VBA exel, many thanks./.
 
Or try

Book1
ABC
1202211Month number
22022092Month number 2 = A1 - A2
32022065Month number 5 = A1 - A3
42022038Month number 8 = A1 - A4
520211211Month number 11 = A1 - A5
620211112Month number 12 =A1 - A6
720210914Month number 15 = A1 - A7
820201124Month number 24 =A1 - A8
Sheet1
Cell Formulas
RangeFormula
B2:B8B2=DATEDIF(TEXT(A2,"0000-00-1"),TEXT($A$1,"0000-00-1"),"M")
YOur formula work well, can you convert this to VBA code in excel?
 
Upvote 0

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Hey!

If you wanna do it with VBA:

VBA Code:
Sub DeductMonth()

Dim ws As Worksheet
Set ws = ThisWorkbook.ActiveSheet

With ws

Row = .Range("A1048576").End(xlUp).Row

date1 = CDate(Left(.Range("A1").Value, 4) & "-" & Right(.Range("A1").Value, 2))
.Range("B1").Value = Right(.Range("A1").Value, 2)

For i = 2 To Row

date2 = CDate(Left(.Range("A" & i).Value, 4) & "-" & Right(.Range("A" & i).Value, 2))

    .Range("B" & i).Value = DateDiff("m", date2, date1)

Next i

End With

End Sub
Thanks for your code.

I Put small code then run your code work well

"Dim Row, i As Long, date1 As String, date2 As String"
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,323
Members
452,635
Latest member
laura12345

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