How to count the number of wednesdays in the next month given the date of the last wednesday of the previous month

aletheasim96

New Member
Joined
Feb 2, 2018
Messages
3
Hello everyone! Okay im super new to excel vba so I apologise if my question is really basic:P I've been asked to automate the sheet below but have no idea how to go about it. So the data values in the table are linked to a PHD system that generates the data using the dates at the top of each column. So what i am supposed to do is to come up with a button that can generate the dates of the next 4 or 5 wednesdays in the next month, depending on exactly how many wednesdays fall in the next month, given the date of the last wednessday of the previous month, in this case 28 Feb. So the desired end result of running the code would be to add the next 4 or 5 wednesdays to the columns next to the last entry(28 Feb in this case) and to drag the formulas from the previous data columns to generate the results for these new columns. Could anyone give me advice on how to do this? Any help is much appreciated!!!:)



03-Jan-2018 8:48:00PM10-Jan-2018 8:48:00PM17-Jan-2018 8:48:00PM24-Jan-2018 8:48:00PM31-Jan-2018 8:48:00PM07-Feb-2018 8:48:00PM14-Feb-2018 8:48:00PM21-Feb-2018 8:48:00PM28-Feb-2018 8:48:00PM
26542542542452687766767
4234534542452525258678
24524525442442875276
4224525445422524552257886
45245242425252552589
785
4545244548676754

<tbody>
</tbody>


Thank you!!!!
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Try this
I assume the dates are in row 1 and they are excel dates not text. I find the last row used based on column A. Change to a column that will always have data. I assume the data starts in row 2

Code:
Sub popwed()

Dim lc As Long
Dim lr As Long
Dim lastwed As Date
Dim lmonth As Long
Dim nmonth As Long


lc = Cells(1, Columns.Count).End(xlToLeft).Column 'find last column used
lr = Cells(Rows.Count, "A").End(xlUp).Row 'find last row of date
lastwed = Cells(1, lc) 'the last date
lmonth = Month(lastwed) 'month of the last date


If lmonth = 12 Then 'set the next month
    nmonth = 1
Else
    nmonth = lmonth + 1
End If


For x = 1 To 5 'loop to populate the next 4 or 5 wed
    If Month(Cells(1, lc + x - 1) + 7) = nmonth Then
        Cells(1, lc + x) = Cells(1, lc + x - 1) + 7
    Else
        Exit For
    End If
    
Next x


'copy formulas
nlc = Cells(1, Columns.Count).End(xlToLeft).Column 'find new last column used
Range(Cells(2, lc), Cells(lr, lc)).Copy Range(Cells(2, lc + 1), Cells(lr, nlc))


End Sub
 
Upvote 0
Hey Scott, I've tried it and it does exactly what i wanted it to! However, i have other data on the sheet above and below the rows im currently working on and when i run the code it works on all the rows. Is there a way to limit it to just rows 51 to 63? Sorry for not stating this in my original post and thanks so much for your time!:)
 
Upvote 0
This will only copy from rows 51 to 63 in the last column

Code:
Sub popwed()

Dim lc As Long
Dim lastwed As Date
Dim lmonth As Long
Dim nmonth As Long




lc = Cells(1, Columns.Count).End(xlToLeft).Column 'find last column used
lastwed = Cells(1, lc) 'the last date
lmonth = Month(lastwed) 'month of the last date


If lmonth = 12 Then 'set the next month
    nmonth = 1
Else
    nmonth = lmonth + 1
End If




For x = 1 To 5 'loop to populate the next 4 or 5 wed
    If Month(Cells(1, lc + x - 1) + 7) = nmonth Then
        Cells(1, lc + x) = Cells(1, lc + x - 1) + 7
    Else
        Exit For
    End If
    
Next x


'copy formulas
nlc = Cells(1, Columns.Count).End(xlToLeft).Column 'find new last column used
Range(Cells(51, lc), Cells(63, lc)).Copy Range(Cells(51, lc + 1), Cells(63, nlc))




End Sub
 
Upvote 0

Forum statistics

Threads
1,216,075
Messages
6,128,657
Members
449,462
Latest member
Chislobog

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