Copy some columns if date is not blank

Rex44

New Member
Joined
Feb 28, 2023
Messages
1
Office Version
  1. 365
Platform
  1. Windows
From sheet1 (roster) I want to copy to sheet2 (Birthday) all the members who's birthday
is in the month. (month is entered from input box) this works great for all months except December.

When December is selected it firsts copies the members who has no birthday (birthday cell is blank)
listed. Followed by the birthdays for the month of December.

The same story goes for Anniversaries.

VBA Code:
inputnumber = InputBox("Enter Birthday By month number")
myrbdate = inputnumber      'Birthday month
Myaadate = inputnumber      'Anniversary Month
LastRow = Sheet1.Cells(Rows.Count, 1).End(xlUp).Row

Sheet1.Range("A1").Select
For i = 2 To LastRow

    mybdate = Month(Cells(i, 9))  '<gets the month of cell (variable Row number i ,column 9)
    If myrbdate = mybdate Then
        erow = Bdays.Cells(Rows.Count, 2).End(xlUp).Offset(1, 0).Row
        Worksheets("Sheet1").Cells(i, 2).Copy _
                        Destination:=Sheets("bdays").Cells(erow, 2)
        Worksheets("Sheet1").Cells(i, 1).Copy _
                        Destination:=Sheets("bdays").Cells(erow, 3)
        Worksheets("Sheet1").Cells(i, 9).Copy _
                    Destination:=Sheets("bdays").Cells(erow, 4)
    End If
Next i


Results =
DayFirstLastBDay
0​
CharlieAgnew
0​
JohnAnderson
0​
GeorgeAsbury
0​
BobAustin
0​
RussBarker
0​
RayRomano
1​
MarkHeitz
12/1/1941​
3​
DavidMurphy
12/3/1946​
12​
EricKisshauer
12/12/1945​
16​
JackDennis
12/16/1950​
16​
TomMcFarland
12/16/1942​
24​
DannyGrant
12/24/1942​
25​
MikeBell
12/25/1950​
25​
NoelHayes
12/25/1937​
26​
JimSoule Jr.
12/26/1936​
 
Last edited by a moderator:

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Month() function returns 12 when the argument has zero or null value. That's why the code thinks the empty cells are December. You can try like this:
VBA Code:
Sub test()
With Worksheets("Sheet1")
myrbdate = 12
LastRow = .Cells(Rows.Count, 1).End(xlUp).Row
.Select
  For i = 2 To LastRow
    mybdate = IIf(.Cells(i, 9) > 0, Month(.Cells(i, 9)), 0) '<gets the month of cell (variable Row number i ,column 9)
    If myrbdate = mybdate Then
        erow = Sheets("bdays").Cells(Rows.Count, 2).End(xlUp).Offset(1, 0).Row
        .Cells(i, 2).Copy _
                        Destination:=Sheets("bdays").Cells(erow, 2)
        .Cells(i, 1).Copy _
                        Destination:=Sheets("bdays").Cells(erow, 3)
        .Cells(i, 9).Copy _
                    Destination:=Sheets("bdays").Cells(erow, 4)
    End If
  Next i
End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,151
Messages
6,123,316
Members
449,094
Latest member
Chestertim

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