DRRK/docs/vba/Module4.bas

50 lines
1.8 KiB
QBasic

Attribute VB_Name = "Module4"
Sub UpdFxRates()
Set cwb = ActiveWorkbook
Set ws = Worksheets("FX_RATES")
Set tmpSheet = cwb.Worksheets("moex_tmp")
Set summarySheet = cwb.Worksheets("SUMMARY")
rep_date = CDate(summarySheet.Range("REP_DT"))
last_row = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row
last_date = CDate(ws.Cells(last_row, 1)) + 1
vLink = "https://www.cbr.ru/currency_base/dynamics/?UniDbQuery.Posted=True&UniDbQuery.so=0&UniDbQuery.mode=1&UniDbQuery.date_req1=&UniDbQuery.date_req2=&UniDbQuery.VAL_NM_RQ=R01375&UniDbQuery.From=" & Format(last_date, "dd.mm.yyyy") & "&UniDbQuery.To=" & Format(rep_date, "dd.mm.yyyy")
tmpSheet.Cells.ClearContents
With tmpSheet.QueryTables.Add(Connection:="URL;" & vLink, Destination:=tmpSheet.Range("$A$1"))
.FieldNames = False
.RowNumbers = False
.FillAdjacentFormulas = False
.RefreshOnFileOpen = False
.BackgroundQuery = False
.SaveData = True
.WebFormatting = xlWebFormattingNone
.WebPreFormattedTextToColumns = True
.WebConsecutiveDelimitersAsOne = True
.WebSingleBlockTextImport = False
.Refresh
End With
For Each xConnect In cwb.Connections
xConnect.Delete
Next xConnect
For Each xQueryTable In tmpSheet.QueryTables
xQueryTable.Delete
Next xQueryTable
d_lr = tmpSheet.Cells(ws.Rows.Count, 1).End(xlUp).Row
For c_row = 4 To d_lr
date_c = DateSerial(CInt(Right(CStr(tmpSheet.Cells(c_row, 1)), 4)), CInt(Mid(CStr(tmpSheet.Cells(c_row, 1)), 4, 2)), Left(CStr(tmpSheet.Cells(c_row, 1)), 2))
fx_rate = tmpSheet.Cells(c_row, 3) / tmpSheet.Cells(c_row, 2)
ws.Cells(last_row + 1, 1) = date_c
ws.Cells(last_row + 1, 2) = fx_rate
last_row = last_row + 1
Next
tmpSheet.Cells.ClearContents
End Sub