Change VBA Code

Av8tordude

Well-known Member
Joined
Oct 13, 2007
Messages
1,074
Office Version
  1. 2019
Platform
  1. Windows
This code creates a table to be able to chart once created. I would to do two things to allow flexibility....

1. Since is table for months, I would like to create the a table for weeks.
2. Select between two dates.

Can anyone assist.. Thank you kindly

Code:
Sub ProfitLoss()    Dim dt As Date, c As Range, f As Range
    Dim lr1 As Long, lr2 As Long, y As String, m As String
    
    Range("AA2:AE" & Rows.Count).ClearContents
    lr1 = Range("D" & Rows.Count).End(xlUp).Row
    For Each c In Range("D18", Range("D" & Rows.Count).End(xlUp))
        y = Year(c)
        m = Month(c)
        dt = DateSerial(y, m, 1)
        Set f = Range("AA:AA").Find(dt, LookIn:=xlValues, lookat:=xlWhole)
        If f Is Nothing Then
            lr2 = Range("AA" & Rows.Count).End(xlUp).Row + 1
            Range("AA" & lr2).Value = dt
            Range("AB" & lr2).Value = Cells(c.Row, "P")
            Range("AC" & lr2).Value = Evaluate("=MAX(IF(YEAR(D18:D" & lr1 & ")=" & y & _
                ",IF(MONTH(D18:D" & lr1 & ")=" & m & ",P18:P" & lr1 & ")))")
            Range("AD" & lr2).Value = Evaluate("=MIN(IF(YEAR(D18:D" & lr1 & ")=" & y & _
                ",IF(MONTH(D18:D" & lr1 & ")=" & m & ",P18:P" & lr1 & ")))")
            Range("AE" & lr2).Value = Cells(Evaluate("=MAX(IF(YEAR(D18:D" & lr1 & ")=" & y & _
                ",IF(MONTH(D18:D" & lr1 & ")=" & m & ",ROW(P18:P" & lr1 & "))))"), "P")
        End If
    Next
End Sub
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Hi,

1. A table for weeks can be created by modifying your "dt" variable. Simply take date that is visible in cell D18 and then, with each loop, add 7 days to it.
Code:
dt = DateSerial(Year(Range("D18")), Month(Range("D18")), Day(Range("D18")) + (c.Row() - 18) * 7)

2. Select between two dates - do you want to end your loop before some specific date? If yes, I think this one can be achieved by modifying your "For Each" statement to end with a specific row, e.g.:
Code:
For Each c In Range("D18:D30")

I hope it is a good starting point.
 
Upvote 0

Forum statistics

Threads
1,214,784
Messages
6,121,535
Members
449,037
Latest member
tmmotairi

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