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./.
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
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
 
Upvote 0
Solution
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")
 
Upvote 0
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./.
When you say that Column A is "number format as: YYYYMM", it is unclear if 202211 is actually a number, or if it's just text, or if it's any date in November 2022 such as 11/1/2022. They are three COMPLETELY different things, and to get the month number from each would be completely different:
Book1 (version 2).xlsb
AB
120221111
220221111
320221111
Sheet3
Cell Formulas
RangeFormula
B1B1=A1-202200
B2B2=VALUE(RIGHT(A2,LEN(A2)-4))
B3B3=MONTH(A3)

Note that XL2BB is not preserving the Right/Left justification of Column A. Rows 1 and 3 are numbers and therefore Right justified automatically without any formatting applied manually, and row 2 is Text and therefore Left justified automatically without any formatting applied manually. With the exception of final reports, it's always best to NOT change any horizontal formatting since it's an easily spotted clue to possible problems. As long as I'm talking about formatting, NEVER use Merge & Center - it TOTALLY screws up a Worksheet's structure. Instead use Center Across Selection in the Horizontal Alignment format section.
p.s. I have no idea why VBA is even considered for this problem, nor do I comprehend what "not taking in consideration the variations from years" means or has to do with it.
 
Upvote 0
When you say that Column A is "number format as: YYYYMM", it is unclear if 202211 is actually a number, or if it's just text, or if it's any date in November 2022 such as 11/1/2022. They are three COMPLETELY different things, and to get the month number from each would be completely different:
Book1 (version 2).xlsb
AB
120221111
220221111
320221111
Sheet3
Cell Formulas
RangeFormula
B1B1=A1-202200
B2B2=VALUE(RIGHT(A2,LEN(A2)-4))
B3B3=MONTH(A3)

Note that XL2BB is not preserving the Right/Left justification of Column A. Rows 1 and 3 are numbers and therefore Right justified automatically without any formatting applied manually, and row 2 is Text and therefore Left justified automatically without any formatting applied manually. With the exception of final reports, it's always best to NOT change any horizontal formatting since it's an easily spotted clue to possible problems. As long as I'm talking about formatting, NEVER use Merge & Center - it TOTALLY screws up a Worksheet's structure. Instead use Center Across Selection in the Horizontal Alignment format section.
p.s. I have no idea why VBA is even considered for this problem, nor do I comprehend what "not taking in consideration the variations from years" means or has to do with it.
@jdellasala Sorry if it wasn't clear but English is not my main language.
The code I provided was in VBA per request from @sbv1986.
What I mentioned about "not taking in consideration the variations from years" was that he has data from multiple years on this table as the formula provided wouldn't work for that since it was looking only for the months and not years. If that was also unclear please let me know so I can better explain!
 
Upvote 0
Another way

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=12*(LEFT($A$1,4) -LEFT($A2,4))+RIGHT($A$1,2)-RIGHT($A2,2)
 
Upvote 0
@jdellasala Sorry if it wasn't clear but English is not my main language.
The code I provided was in VBA per request from @sbv1986.
What I mentioned about "not taking in consideration the variations from years" was that he has data from multiple years on this table as the formula provided wouldn't work for that since it was looking only for the months and not years. If that was also unclear please let me know so I can better explain!
What I said about VBA was in general. The problem doesn't require VBA, and there are MUCH easier ways to solve the problem using simple formulas.
As for the years thing, I was wrong. I now see that he's looking for total months. My apologies.

That said, and in hopes that the data is actually a date, the little known DATEDIF comes to the rescue!
Book1 (version 2).xlsb
ABC
1202211Month number
2202209Month number 2 = A1 - A22
3202206Month number 5 = A1 - A35
4202203Month number 8 = A1 - A48
5202112Month number 11 = A1 - A511
6202111Month number 12 =A1 - A612
7202109Month number 15 = A1 - A714
8202011Month number 24 =A1 - A824
Sheet3
Cell Formulas
RangeFormula
C2:C8C2=IF(A2>$A$1,DATEDIF($A$1,A2,"M"),DATEDIF(A2,$A$1,"M"))

It's not well supported in Excel, but it is an official function and does EXACTLY what you're looking for - as long as you use actual dates!
DATEDIF
The only reason for the IF clause above is because DATEDIF's parameters are start_date and end_date and throws an error if the first parameter is larger than the second. If you know that the top date (A1) will always be larger than the rest of the dates, the formula
Excel Formula:
=DATEDIF(A2,$A$1,"M")
will work fine.
 
Upvote 0
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 VBA code but code wrong if A8 = 202011, B8 must be = 24. Your code run B8 = 11
 
Upvote 0

Forum statistics

Threads
1,214,918
Messages
6,122,249
Members
449,075
Latest member
staticfluids

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