繁体   English   中英

如何使用 VBA 获取导入 Excel 的路径和文件名

[英]How to get path and file name for import to Excel using VBA

我有一个 demo.xlsm 文件,当我打开工作簿时会自动运行宏,但它需要读取 .csv 文件以获取数据和 Lamda_Logo.png 文件。 Lamda_Logo.png 始终具有相同的名称。 .csv 文件的名称未知,但它将与我的 demo.xlsm 具有相同的路径

如何在 VBA 代码中使用变量来引用我的 csv 文件的路径和名称?

我试过了

csvPath = Dir(Thisworkbook.Path & "\*.csv") 

但它不起作用。 唯一有效的命令是

ThisWorkbook.Path & "\Lamda_Logo.png"

加载图片,但我不能对 .csv 文件做同样的事情,因为我不知道名字..

这是我的代码:

Private Sub Workbook_Open()


Dim ws As Worksheet
Dim imagePath As String
Dim imgLeft As Double
Dim imgTop As Double

Set ws = ActiveSheet
imagePath = ThisWorkbook.Path & "\Lamda_Logo.png"
imgLeft = ActiveCell.Left
imgTop = ActiveCell.Top

'Width & Height = -1 means keep original size
ws.Shapes.AddPicture _
    fileName:=imagePath, _
    LinkToFile:=msoFalse, _
    SaveWithDocument:=msoTrue, _
    Left:=imgLeft, _
    Top:=imgTop, _
    Width:=-1, _
    Height:=-1
    
     Dim fileName As String, folder As String
    
    folder = ThisWorkbook.Path & "\"
    fileName = "#Lamda_Dev_Projects_Risk_Register_191020211650.csv"
    
    ActiveCell.Offset(1, 0).Range("A12").Select
    
With Worksheets("Sheet1").Range("A13:T13")
 .Font.Size = 12
End With

Worksheets("Sheet1").Range("A13:T13").Font.Bold = True

Range("A13:T13").Interior.Color = RGB(147, 175, 186)
        
    With ActiveSheet.QueryTables _
        .Add(Connection:="TEXT;" & folder & fileName, Destination:=ActiveCell)
        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .TextFilePromptOnRefresh = False
        .TextFilePlatform = 850
        .TextFileStartRow = 1
        .TextFileParseType = xlDelimited
        .TextFileTextQualifier = xlTextQualifierDoubleQuote
        .TextFileConsecutiveDelimiter = False
        .TextFileTabDelimiter = False
        .TextFileSemicolonDelimiter = False
        .TextFileCommaDelimiter = True
        .TextFileSpaceDelimiter = False
        .TextFileColumnDataTypes = Array(1, 1, 1, 1)
        .TextFileTrailingMinusNumbers = True
        .Refresh BackgroundQuery:=True
    End With
    
    'check for filter, turn on if none exists
  If Not ActiveSheet.AutoFilterMode Then
    ActiveSheet.Range("A13").AutoFilter
  End If
    

End Sub

我试过了

csvPath = Dir(Thisworkbook.Path & "\*.csv") 

但它不起作用。 唯一有效的命令是

ThisWorkbook.Path & "\Lamda_Logo.png"

加载图片,但我不能对 .csv 文件做同样的事情,因为我不知道名字..

Dir函数将返回 File Name ,但没有Path 因此,如果您的工作簿是“D:\\Files\\Testing\\Workbook1.xlsx”,而您的 CSV 是“D:\\Files\\Testing\\Data_20211020.csv”,则:

  • ThisWorkbook.Path将是“D:\\Files\\Testing”
  • Dir(ThisWorkbook.Path & "\\*.csv")将是 "Data_20211020.csv"
  • 你想要的csvPath结果是“D:\\Files\\Testing\\Data_20211020.csv”

因此,完整路径将是

csvPath = ThisWorkbook.Path & "\" & Dir(ThisWorkbook.Path & "\*.csv") 

(请记住, Dir将返回一个与掩码/过滤器匹配的伪随机文件,再次调用Dir()将返回另一个文件,直到它们全部返回。如果您有多个 CSV 文件,您可能需要一个更具体的过滤器,比如Dir(ThisWorkbook.Path & "\\Data_*_Processed.csv")什么的)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM