VBA - date minus X days from today

耗尽温柔 提交于 2019-12-13 07:15:29

问题


Here's a snippet of my invoice generator, particularly the crawler SUB that grabs the exchange rate from a webpage who's URL contains the exchange rate date. I'm trying is force it to go to a URL from Saturday if the referring date (datIzd, pulled from datIzdCtrl content control) is Sunday or Monday, because the page isn't yet generated on those days. Currently Sunday and Monday send out "out of range" error.

Perhaps a much better way would be for the date to keep going 1 day back until it hits a page that exists (and notifying in MsgBox how far back it went), because the same thing applies to holidays - bank doesn't release the new exchange rate, so the one from last working day is relevant.

Can someone show me how this is done? I tried using

If Weekday(Now(), vbMonday)

and playing around with it, but it didn't get far.

Also I'm aware that in the code I'm seemingly unnecessarily reformating dates multiple times, but this is a must since US and Croatian date formats aren't the same, and they have to be properly presented on respective invoices, and recalculated for URL name between conversions.

Here's what I have.

Dim splData As Variant

Enum READYSTATE
    READYSTATE_UNINITIALIZED = 0
    READYSTATE_LOADING = 1
    READYSTATE_LOADED = 2
    READYSTATE_INTERACTIVE = 3
    READYSTATE_COMPLETE = 4
End Enum

Sub Crawler()

    Dim url As String, datIzd As Date, xmlHTTP As MSXML2.ServerXMLHTTP60
    Dim getData As String

    Set xmlHTTP = New MSXML2.ServerXMLHTTP60

    ActiveDocument.SelectContentControlsByTitle("datIzCtrl")(1).Range.ParentContentControl.DateDisplayFormat = "MM-DD-YYYY"

    datIzd = ActiveDocument.SelectContentControlsByTitle("datIzCtrl")(1).Range.Text

    With xmlHTTP
        url = "http://www.hnb.hr/tecajn/f" & Format(datIzd, "ddmmyy") & ".dat"
        .Open "GET", url, False
        .setRequestHeader "Content-Type", "text/xml"
        .send
        getData = .responseText
    End With

    repData = Replace(getData, "       ", vbCrLf)
    repData = Replace(getData, "      ", vbCrLf)
    splData = Split(repData, vbCrLf)

    If OptionPredracun.Value = True Or OptionRacunPredujam.Value = True Or OptionRacunUkupniIznosHR.Value = True Then
        ActiveDocument.SelectContentControlsByTitle("datIzCtrl")(1).Range.ParentContentControl.DateDisplayFormat = "DD. MMMM YYYY."
    Else
        ActiveDocument.SelectContentControlsByTitle("datIzCtrl")(1).Range.ParentContentControl.DateDisplayFormat = "MMMM DD, YYYY"
    End If


End Sub

Note: if this sounds confusing, you can have a look at my previous post regarding this same project that goes into more detail on what I'm trying to do:

VBA extract and parse data from website to Word


回答1:


It is a bit unclear on what type of variable datIzd is. The assignment to the .Text property of the form seems that it is a String but its later use in the .Format function makes it look like a Variant or Date type. In the following, I'll declare it as a Date type and use DateValue (another option is CDate) to convert the content control's text to an actual date.

dim datIzd as date
datIzd = DateValue(ActiveDocument.SelectContentControlsByTitle("datIzCtrl")(1).Range.Text)

Once you have an actual date in datIzd, you should be able to determine its Weekday and subtract the weekday from the date if it is Sunday or Monday. The default vbSunday is probably best for this as that makes Sunday a 1 and Monday a 2.

if Weekday(datIzd) < 3 then
  datIzd = datIzd - Weekday(datIzd)
end if

A more simplified version of that uses VBA's perceived values of -1/0 for True/False like this.

datIzd = datIzd + ((Weekday(datIzd) < 3) * Weekday(datIzd))

This should all be done before datIzd is used to create the URL string in,

url = "http://www.hnb.hr/tecajn/f" & Format(datIzd, "ddmmyy") & ".dat"

Either of those should push Sunday and Monday back to the previous Saturday. Tuesday and later will be left alone.



来源:https://stackoverflow.com/questions/28551983/vba-date-minus-x-days-from-today

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!