Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba 仅从工作簿导入已用范围_Vba_Excel_Csv - Fatal编程技术网

Vba 仅从工作簿导入已用范围

Vba 仅从工作簿导入已用范围,vba,excel,csv,Vba,Excel,Csv,我试图浏览一个文件并获取图纸名称,以便在现有文件中导入数据 当我为要导入的文件指定固定范围时,我的代码运行良好。(突出显示) 我想获取要导入的文件的使用范围,并提取准确的日期 请看下面我的代码 Sub BrowseSourceData() Dim wsht_Temp As Workbook Dim varFile As Variant Dim fileLoc As String Dim filePath As String Dim fileName As

我试图浏览一个文件并获取图纸名称,以便在现有文件中导入数据

当我为要导入的文件指定固定范围时,我的代码运行良好。(突出显示) 我想获取要导入的文件的使用范围,并提取准确的日期

请看下面我的代码

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

如果准确定义源范围,则可以将外部:=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

嗨,吉佩德,谢谢你的回复。行mydata=“=”&wsht_Temp.worksheets(shtName).range(“A1”).address(0,0,external:=true)出现错误,因为此时“shtName”为空,您可能应该为其指定工作表名称。