Populate ComboBox1 with nummber 0f years from column A and column B

haseft

Active Member
Joined
Jun 10, 2014
Messages
321
Hi,
In Sheet1 there are some columns.
Column A (Date_Begin) and column B (Date_end) has date format.
I want to populate ComboBox1 with nummber 0f years from column A and column B.
I am thinking to find the lowest year (“yyyy”) from column A and the highest year from column B.
Here is some data.
Date_Begin
Date_end
2015-01-01​
2016-12-31​
2015-01-01​
2017-12-31​
2018-10-01​
2018-12-30​
2018-01-01​
2019-12-31​
2019-01-01​
2021-05-30​


The lowest year in column A is = 2015
The heist year in column B is =2021
The number of years between the lowest and the heist is 6 year.
So lowest year + 1, lowest year + 2, , , , , , lowest year +6.
The result of ComboBox1 should be like this (show only "yyyy"):
2015
2016
2017
2018
2019
2020
2021

Help with this please.
Code:
Private Sub UserForm_Initialize()
'
Dim i As Integer
Dim LowestYear As Long
Dim HeistYear As Long
Dim TotYears As Long

LowestYear = Min(Range("A2", .Range("A" & Rows.Count).End(xlUp)))
HeistYear = Max(Range("B2", .Range("B" & Rows.Count).End(xlUp)))
TotYears = HeistYear  - LowestYear

For i = 1 To TotYears
'
'
'  add to ComboBox
'
Next

End Sub
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Try this

VBA Code:
Private Sub UserForm_Initialize()
  Dim i As Long
  For i = Year(WorksheetFunction.Min(Range("A:A"))) To Year(WorksheetFunction.Max(Range("B:B")))
    ComboBox1.AddItem i
  Next
End Sub
 
Upvote 0
Solution
I'm glad to help you. Thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,911
Messages
6,122,198
Members
449,072
Latest member
DW Draft

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