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

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
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
Use this:

=(LEFT($A$1,4)-LEFT($A2,4))*12+RIGHT($A$1,2)-RIGHT($A2,2)
 
Upvote 0

Forum statistics

Threads
1,216,082
Messages
6,128,700
Members
449,464
Latest member
againofsoul

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