[英]How to get path and file name for import to Excel using VBA
I have a demo.xlsm file with macros running automatically when I open the workbook but it needs to read a .csv file for data and a Lamda_Logo.png file.我有一个 demo.xlsm 文件,当我打开工作簿时会自动运行宏,但它需要读取 .csv 文件以获取数据和 Lamda_Logo.png 文件。 The Lamda_Logo.png has always the same name.
Lamda_Logo.png 始终具有相同的名称。 The name of the .csv file is unknown but it will have the same path with my demo.xlsm
.csv 文件的名称未知,但它将与我的 demo.xlsm 具有相同的路径
How can I use variables in VBA code to reference the path and name of my csv file?如何在 VBA 代码中使用变量来引用我的 csv 文件的路径和名称?
I have tried我试过了
csvPath = Dir(Thisworkbook.Path & "\*.csv")
but it doesnt work.但它不起作用。 The only command that works is
唯一有效的命令是
ThisWorkbook.Path & "\Lamda_Logo.png"
to load the picture but I cant do the same for the .csv file since I won't know the name..加载图片,但我不能对 .csv 文件做同样的事情,因为我不知道名字..
Here is my code:这是我的代码:
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
I have tried我试过了
csvPath = Dir(Thisworkbook.Path & "\*.csv")
but it doesnt work.但它不起作用。 The only command that works is
唯一有效的命令是
ThisWorkbook.Path & "\Lamda_Logo.png"
to load the picture but I cant do the same for the .csv file since I won't know the name..加载图片,但我不能对 .csv 文件做同样的事情,因为我不知道名字..
The Dir
function will return the File Name , but without the Path . Dir
函数将返回 File Name ,但没有Path 。 So, if your workbook is "D:\\Files\\Testing\\Workbook1.xlsx", and your CSV is "D:\\Files\\Testing\\Data_20211020.csv", then:因此,如果您的工作簿是“D:\\Files\\Testing\\Workbook1.xlsx”,而您的 CSV 是“D:\\Files\\Testing\\Data_20211020.csv”,则:
ThisWorkbook.Path
will be "D:\\Files\\Testing" ThisWorkbook.Path
将是“D:\\Files\\Testing”Dir(ThisWorkbook.Path & "\\*.csv")
will be "Data_20211020.csv" Dir(ThisWorkbook.Path & "\\*.csv")
将是 "Data_20211020.csv"csvPath
is "D:\\Files\\Testing\\Data_20211020.csv"csvPath
结果是“D:\\Files\\Testing\\Data_20211020.csv” As such, the full path will be因此,完整路径将是
csvPath = ThisWorkbook.Path & "\" & Dir(ThisWorkbook.Path & "\*.csv")
(Remember that Dir
will return a pseudo-random file that matches the mask/filter, and calling Dir()
again will return another one, until they have all been returned. If you have multiple CSV files, you might need a more specific filter, such as Dir(ThisWorkbook.Path & "\\Data_*_Processed.csv")
or something) (请记住,
Dir
将返回一个与掩码/过滤器匹配的伪随机文件,再次调用Dir()
将返回另一个文件,直到它们全部返回。如果您有多个 CSV 文件,您可能需要一个更具体的过滤器,比如Dir(ThisWorkbook.Path & "\\Data_*_Processed.csv")
什么的)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.