Как импортировать датированный файл XLS из веб-VBA

Чтобы рассчитать налог, вы можете использовать round (в конце концов, это то, что делает ресторан):

def calc_tax(mealPrice):  
    tax = round(mealPrice*.06,2)
    return tax

Чтобы отобразить данные, вы можете использовать многострочную строку, а метод форматирования строки :

def display_data(mealPrice, tax):
    total=round(mealPrice+tax,2)
    print('''\
Your Meal Price is {m:=5.2f}
Tax                {x:=5.2f}
Total              {t:=5.2f}
'''.format(m=mealPrice,x=tax,t=total))

Обратите внимание, что метод формата был введен в Python 2.6, для более ранних версий вам потребуется использовать старую интерполяцию строк %:

    print('''\
Your Meal Price is %5.2f
Tax                %5.2f
Total              %5.2f
'''%(mealPrice,tax,total))

Тогда

mealPrice=input_meal()
tax=calc_tax(mealPrice)
display_data(mealPrice,tax)

дает:

# Enter the meal subtotal: $43.45
# Your Meal Price is 43.45
# Tax                 2.61
# Total              46.06

0
задан Jake 18 March 2019 в 23:53
поделиться

1 ответ

Независимо от использования QueryTable, вы можете открывать онлайн-файлы прямо из Excel. Ниже приведен пример того, как сгенерировать URL на основе ввода даты и открыть его из Excel.

Option Explicit

Private Const DATE_FMT As String = "yyyymmdd"
Private Const BASE_URL As String = "https://docs.misoenergy.org/marketreports/"
Private Const POSTFIX1 As String = "_sr_nd_is.xls"
Private Const POSTFIX2 As String = "_rt_lmp_final.csv"

Sub Main()
    Dim dDataDate As Date, dToday As Date, oWB As Workbook

    dToday = CDate(ThisWorkbook.Sheets(1).Cells(6, 1).Value) ' Not sure what to do with this
    dDataDate = CDate(ThisWorkbook.Sheets(1).Cells(1, 1).Value) - 1 ' 1 day before it

    Set oWB = GetOnlineFile(CreateURL1(dDataDate))

    If Not oWB Is Nothing Then
        ' Do whatever you need with the opened file

        oWB.Close
        Set oWB = Nothing
    End If
End Sub

Private Function GetOnlineFile(URL As String) As Workbook
    On Error Resume Next
    Set GetOnlineFile = Workbooks.Open(URL)
End Function

Private Function CreateURL1(DataDate As Date) As String
    CreateURL1 = BASE_URL & Format(DataDate, DATE_FMT) & POSTFIX1
End Function

Private Function CreateURL2(DataDate As Date) As String
    CreateURL2 = BASE_URL & Format(DataDate, DATE_FMT) & POSTFIX2
End Function
0
ответ дан PatricK 18 March 2019 в 23:53
поделиться
Другие вопросы по тегам:

Похожие вопросы: