Relative instead of Absolute paths in Excel VBA

前端 未结 8 953
眼角桃花
眼角桃花 2020-12-05 04:10

I have written an Excel VBA macro which imports data from a HTML file (stored locally) before performing calculations on the data.

At the moment the HTML file is ref

8条回答
  •  天涯浪人
    2020-12-05 04:33

    You can provide more flexibility to your users by provide Browser Button to them

    Private Sub btn_browser_file_Click()
    Dim xRow As Long
    Dim sh1 As Worksheet
    Dim xl_app As Excel.Application
    Dim xl_wk As Excel.Workbook
    Dim WS As Workbook
    Dim xDirect$, xFname$, InitialFoldr$
    InitialFoldr$ = "C:\"
    With Application.FileDialog(msoFileDialogFolderPicker)
        .InitialFileName = Application.DefaultFilePath & "\"
        .Title = "Please select a folder to list Files from"
        .InitialFileName = InitialFoldr$
        .Show
        Range("H13").Activate
        If .SelectedItems.Count <> 0 Then
            xDirect$ = .SelectedItems(1) & "\"
             Range("h12").Value = xDirect$
            xFname$ = Dir(xDirect$, 7)
            Do While xFname$ <> ""
             If (Format(FileDateTime(xDirect$ & "\" & xFname$), "MM/DD/YYYY") > Format(Range("H10").Value, "MM/DD/YYYY")) Then
                ActiveCell.Offset(xRow) = xFname$
                xRow = xRow + 1
                xFname$ = Dir
                Else
                xFname$ = Dir
                xRow = xRow
            End If
            Loop
        End If
    End With
    

    with this piece of code you can achieve this, easily. Tested code

提交回复
热议问题