GetValues来自已满足Excel VBA标准的已关闭工作簿

时间:2012-08-24 13:36:08

标签: excel excel-vba vba

![数据集] [1]我有一个宏来从封闭的工作簿中 GetValues 。宏工作并获取数据,但我错过了如何使它只获得帐户P 15178的任何数据。 源数据包含5个帐户,但我只需要一个帐户。帐号列在" A"列中。源数据。

这是我到目前为止所做的:

Sub test()


GetValuesFromAClosedWorkbook "H:\VBA", "DNAV.xlsx", "DNAV", "A1:F250"


End Sub

Sub GetValuesFromAClosedWorkbook(fPath As String, _
fName As String, sName, cellRange As String)

With ActiveSheet.Range(cellRange)
    .FormulaArray = "='" & fPath & "\[" & fName & "]" _
    & sName & "'!" & cellRange
    .Value = .Value

End With

End Sub

数据集:

2 个答案:

答案 0 :(得分:2)

此答案仅基于数据集的图像。如果有任何延迟,你需要调整代码。

Sub test()


    GetValuesFromAClosedWorkbook "H:\VBA", "DNAV.xlsx", "DNAV", "A1:F7" 'since your product only goes to row 7, this should be good. If your data is not sorted this way all the time, you will need a whole other solution.


End Sub

Sub GetValuesFromAClosedWorkbook(fPath As String, _
fName As String, sName, cellRange As String)

With ActiveSheet.Range(cellRange).Offset(6) 'offsetting by 6 rows should get the formula starting on cell A7
    .FormulaArray = "='" & fPath & "\[" & fName & "]" _
    & sName & "'!" & cellRange
    .Value = .Value

End With

End Sub

答案 1 :(得分:1)

以下是您可以尝试的内容:

Sub GetValuesFromClosedWorkbook(fpath as string, fname as String, _
    sname as String, cellRange as String, criteria as String)

Activesheet.AutoFilterMode = False

With  Activesheet.Range(cellRange)
    .FormulaArray = "='" & fpath & "\[" & fname & "]" & sname & "'!" & cellRange
    .Value = .Value
    .AutoFilter  Field:=1, Criteria1:= "<>" & criteria
    .Offset(1,0).SpecialCells(xlCellTypeVisible).EntireRow.Delete
End With

Activesheet.AutoFilterMode = False

End Sub

然后像这样打电话给你的潜水员:

GetValuesFromClosedWorkbook "H:\VBA", "DNAV.xlsx", "DNAV", "A1:F250", "P 15178"

未经测试,无法在我的手机中测试:)所以我留给你。
完全基于您的屏幕截图。
编辑它以满足您的需求。