Consulting

Results 1 to 20 of 20

Thread: Solved: Last business day in formula

  1. #1
    VBAX Regular
    Joined
    Oct 2012
    Posts
    85
    Location

    Solved: Last business day in formula

    See the below VBA code. In the formula, is there a way to reference the last business day instead of having to manually change the date everyday? So instead of ...,"11/27/2012","11/27/2012" is there a way to reference the last business day in this formula? See the attached sheet, the date is also in cell A1 we could also reference the date in that cell too in the formula if there is a way to do that?

    [VBA]Sub Macro15()
    '
    ActiveCell.FormulaR1C1 = _
    "=BDH(RC[-5]&"" equity"",""px last"",""11/27/2012"",""11/27/2012"")"
    Range("G3").Select
    End Sub
    [/VBA]
    Attached Files Attached Files

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Use

    =BDH(B3&" equity","px last",WORKDAY(DATE(YEAR($A$1),MONTH($A$1)+1,0),-1),WORKDAY(DATE(YEAR($A$1),MONTH($A$1)+1,0),-1))
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    VBAX Regular
    Joined
    Oct 2012
    Posts
    85
    Location
    I am getting an application defined or object defined error for the below formula

    .Cells(i, "G").FormulaR1C1 = _
    "=BDH(B3&"" equity"",""px last"",WORKDAY(DATE(YEAR($A$1),MONTH($A$1)+1,0),-1),WORKDAY(DATE(YEAR($A$1),MONTH($A$1)+1,0),-1))"

  4. #4
    VBAX Mentor Teeroy's Avatar
    Joined
    Apr 2012
    Location
    Sydney, Australia
    Posts
    414
    Location
    Change .FormulaR1C1 to .Formula as you are not using R1C1 format in the formula anymore.
    _________________________________________________________________________
    "In theory there is no difference between theory and practice. In practice there is." - Chuck Reid

    Any day you learn something new is a day not wasted.

  5. #5
    VBAX Regular
    Joined
    Oct 2012
    Posts
    85
    Location
    .Cells(i, "G").Formula = _
    "=BDH(B3&"" equity"",""px last"",WORKDAY(DATE(YEAR($A$1),MONTH($A$1)+1,0),-1),WORKDAY(DATE(YEAR($A$1),MONTH($A$1)+1,0),-1))"

    For some reaosn this formula does not work. If I have the date in cell A1 that needs to be used, can I somehow reference the date in cell A1 in place of "11/27/2012" in the below code?

    Sub Macro15()
    '
    ActiveCell.FormulaR1C1 = _
    "=BDH(RC[-5]&"" equity"",""px last"",""11/27/2012"",""11/27/2012"")"
    Range("G3").Select
    End Sub

  6. #6
    VBAX Regular
    Joined
    Oct 2012
    Posts
    85
    Location
    The date in cell A1 already has the date we need, so I am trying a fromula like this but it doesnt seem to work..

    =BDH(B2&" equity","px_last","WORKDAY($A$1,0,0)","WORKDAY($A$1,0,0)")

    I think is may be because the result of "WORKDAY($A$1,0,0)" does not return in date form like 11/27/2012 so the forumla cant read what date it is...

  7. #7
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    "R1C1" = "$A$1"
    "RC" = ThisCell
    "R[-1]C" = ThisCell.Offset(-1, 0)
    "R3[+1]C1" =Cells($A$3).Offset(1, 0)

  8. #8
    VBAX Regular
    Joined
    Oct 2012
    Posts
    85
    Location
    What will that do? Should I add this in the code somewhere?

  9. #9
    VBAX Regular
    Joined
    Oct 2012
    Posts
    85
    Location
    And right now I am not working with R1C1, I am working with the below code:

    [VBA]Sub Test()
    Dim NumBlocks As Long
    Dim Lastrow As Long
    Dim i As Long

    With ActiveSheet

    Lastrow = .Cells(.Rows.Count, "F").End(xlUp).Row
    For i = 3 To Lastrow

    .Cells(i, "G").Formula = _
    "=BDH(RC[-5]&"" equity"",""DATE(MONTH($A$1)YEAR($A$1)"",""DATE(MONTH($A$1)YEAR($A$1)"")"
    Range("G3").Select

    Next i

    End With
    End Sub
    [/VBA]

  10. #10
    VBAX Regular
    Joined
    Oct 2012
    Posts
    85
    Location
    Right now, the below code works perfectly. I just need to get the dates (3/12/2013) to reference the date in cell A1 so I dont have to manually go in everyday and enter the date.

    [VBA]Sub Test()
    Dim NumBlocks As Long
    Dim Lastrow As Long
    Dim i As Long

    With ActiveSheet

    Lastrow = .Cells(.Rows.Count, "F").End(xlUp).Row
    For i = 3 To Lastrow

    .Cells(i, "G").Formula = _
    "=BDH(RC[-5]&"" equity"",""px_last"",""3/12/2013"",""3/12/2013"")"
    Range("G3").Select

    Next i

    End With
    End Sub
    [/VBA]

  11. #11
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    For some reaosn this formula does not work. If I have the date in cell A1 that needs to be used, can I somehow reference the date in cell A1 in place of "11/27/2012" in the below code?

    Sub Macro15()
    '
    ActiveCell.FormulaR1C1 = _
    "=BDH(RC[-5]&"" equity"",""px last"",""11/27/2012"",""11/27/2012"")"
    Range("G3").Select
    End Sub
    looks like R1C1 to me.

  12. #12
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    [vba]Sub Test()
    Dim NumBlocks As Long
    Dim Lastrow As Long
    Dim i As Long
    Dim RefersToA
    RefersToA = CStr(Range"A1"))

    With ActiveSheet

    Lastrow = .Cells(.Rows.Count, "F").End(xlUp).Row
    For i = 3 To Lastrow

    .Cells(i, "G").Formula = _
    "=BDH(RC[-5]&"" equity"",""px_last""," & REfersToA & "," & RefersToA & ")"
    Range("G3").Select

    Next i

    End With
    End Sub [/vba]
    Last edited by SamT; 03-13-2013 at 04:25 PM.

  13. #13
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Never mind I see the answer in one of your other posts.
    Last edited by SamT; 03-13-2013 at 04:17 PM. Reason: Original message doesn't apply

  14. #14
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Never mind again. Got in a hurry and edited the wrong post before, so did some more editing to make things right.
    Last edited by SamT; 03-13-2013 at 04:24 PM.

  15. #15
    VBAX Mentor Teeroy's Avatar
    Joined
    Apr 2012
    Location
    Sydney, Australia
    Posts
    414
    Location
    It appears your BDH function is expecting strings for the 3rd and fourth terms so you need to enclose the value of A1 with "". Try changing the formula line in SamT's code to:
    [vba].Cells(i, "G").Formula = _
    "=BDH(RC[-5]&"" equity"",""px_last"",""" & REfersToA & """,""" & RefersToA & """)" [/vba]
    _________________________________________________________________________
    "In theory there is no difference between theory and practice. In practice there is." - Chuck Reid

    Any day you learn something new is a day not wasted.

  16. #16
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    You're right, I left the "As String" off the Variable declaration.

    However, I have never had to enclose string variables in quotes when passing them as arguments. His UDF may require them, so it's worth a try.

  17. #17
    VBAX Mentor Teeroy's Avatar
    Joined
    Apr 2012
    Location
    Sydney, Australia
    Posts
    414
    Location
    @SamT, if you were calling the function directly from VBA you are right you wouldn't need the quotes since the variable is of data type string. Because it is not a direct function call the formula string will evaluate (and concatenate) first then call the UDF. That formula string may just show those arguments as dates to the UDF which is why I suggested the quotes.
    _________________________________________________________________________
    "In theory there is no difference between theory and practice. In practice there is." - Chuck Reid

    Any day you learn something new is a day not wasted.

  18. #18
    VBAX Regular
    Joined
    Oct 2012
    Posts
    85
    Location
    I tried this code but there is a syntax error with
    RefersToA = CStr(Range"A1"))


    [VBA]Sub Test()
    Dim NumBlocks As Long
    Dim Lastrow As Long
    Dim i As Long
    Dim RefersToA
    RefersToA = CStr(Range"A1"))

    With ActiveSheet

    Lastrow = .Cells(.Rows.Count, "F").End(xlUp).Row
    For i = 3 To Lastrow

    .Cells(i, "G").Formula = _
    "=BDH(RC[-5]&"" equity"",""px_last"",""" & RefersToA & """,""" & RefersToA & """)"
    Range("G3").Select

    Next i

    End With
    End Sub
    [/VBA]

  19. #19
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Since we don't have your workbook and UDFs, I sometimes write simple code directly into the Post Reply box, expecting the reader to catch simple errors. If I had written it in the VBE, it would have caught it.

    If you have the VBE set up to automatically place "Option Explicit" at the top of Code Pages, VBE would have told you it expected a Parenthesis in that line.
    [vba]RefersToA = CStr(Range"A1"))[/vba]
    is missing a parenthesis.

    It should read
    [vba]RefersToA = CStr(Range("A1"))[/vba]
    You could also use
    [vba]RefersToA = CStr(Range("A1").Value)[/vba]
    but .Value is the default return for a Range, so I usually leave it off.

  20. #20
    VBAX Regular
    Joined
    Oct 2012
    Posts
    85
    Location
    That works great, thank you!!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •