|
- Sub Test()
- Dim arrData As Variant
-
- If GetDataByFilePath(ThisWorkbook.Path & Application.PathSeparator & "数据.xlsx", "Format", arrData) Then
- MsgBox "提取数据" & UBound(arrData) & "行" & UBound(arrData, 2) & "列"
- Else
- MsgBox "无数据!"
- End If
- End Sub
- '根据指定的工作簿名称、表名,返回二维数组
- ''' --strFilePathAndName 要提取数据的工作簿 全路径名称
- ''' --strSheetName 要提取数据的工作表名
- ''' --ArrResult 返回结果
- Function GetDataByFilePath(strFilePathAndName As String, strSheetName As String, ByRef ArrResult As Variant) As Boolean
- Dim wb As Workbook, varTmp As Variant
- On Error GoTo ExitFun
- Set wb = GetObject(strFilePathAndName)
- ArrResult = wb.Sheets(strSheetName).UsedRange
- wb.Close (False)
- Set wb = Nothing
- If IsEmpty(ArrResult) Then GoTo ExitFun
- If Not IsArray(ArrResult) Then
- varTmp = ArrResult
- ReDim ArrResult(1 To 1, 1 To 1) As Variant
- ArrResult(1, 1) = varTmp
- End If
-
- GetDataByFilePath = True
- Exit Function
- ExitFun:
- If Not (wb Is Nothing) Then wb.Close (False)
- Set wb = Nothing
- GetDataByFilePath = False
- End Function
复制代码
|
评分
-
2
查看全部评分
-
|