我正在尝试浏览文件并获取工作表名称以导入现有文件中的数据。
当我为要导入的文件指定固定范围时,我的代码运行良好。(突出显示) 我想获取要导入的文件的使用范围并提取确切的日期。
请参阅下面的代码
Sub BrowseSourceData()
Dim wsht_Temp As Workbook
Dim varFile As Variant
Dim fileLoc As String
Dim filePath As String
Dim fileName As String
Dim fileExtn As Long
Dim mydata As String
Dim shtName As String
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets("Source Data")
ws.Activate
ws.Cells.Clear
fileLoc = Application.GetOpenFilename("Excel files (*.xls*;,*.xls*," & " Comma Seperated Files (*.csv),*.csv,", , "Select a file", , False)
If fileLoc = "False" Then
Exit Sub
Else
'---Get the filename and file loaction
'fileExtn = InStr(fileLoc, "csv")
fileName = Mid$(fileLoc, 1 + InStrRev(fileLoc, "\"), Len(fileLoc))
filePath = Mid$(fileLoc, 1, InStrRev(fileLoc, "\"))
'If fileExtn > 0 Then
If InStr(fileLoc, "csv") Or InStr(fileLoc, "CSV") > 0 Then
Call loadDataFromCSV(fileLoc)
Else
'dynamic sheet naming
Set wsht_Temp = Workbooks.Open(filePath & "\" & fileName, False)
wsht_Temp.Close False
mydata = "='" & filePath & "[" & fileName & "]" & shtName & ***"'!$A$1:$AC$10000"***
***With ws.Range("A1:L10000")***
.Formula = mydata
'convert formula to text
.Value = .Value
End With
End If
End If
Set wsht_Temp = Nothing
Sheets("HomePage").Activate
End Sub
答案 0 :(得分:0)
如果您准确定义了源范围,则可以将 external:= true 参数应用于.Address属性。您只需要左上角单元格的相对地址。
Set wsht_Temp = Workbooks.Open(filePath & "\" & fileName, False)
mydata = "='" & wsht_Temp.worksheets(shtName).range("A1").address(0, 0, external:=true)
wsht_Temp.Close False
尝试列A:L的交叉和工作表的.UsedRange属性。
With intersect(ws.Range("A:L"), ws.usedrange)
.formula = myData
.value = .value
end with