使用ActiveWorkbook将变量引用保存到工作簿

时间:2019-06-03 09:56:27

标签: excel vba

我正在尝试永久引用两个特定的工作簿,但是在查看“本地”窗口时,它们不是

我也尝试引用ThisWorkbook.Name,但似乎无法解决问题。似乎总是回头参考Workbook / ThisWorkbook。

if((z&S) && ((!ab)&(K==INF)&(d>2)&(v>V)&(v<Beta))) {

运行下标时出现下标超出范围错误。

3 个答案:

答案 0 :(得分:0)

更改

Workbooks.Open Filename:=ActiveWorkbook.path & "\" & sFound
Set WB2 = ActiveWorkbook

Set WB2=Workbooks.Open(ActiveWorkbook.path & "\" & sFound)

答案 1 :(得分:0)

尝试一下:

Sub Import_data()

Dim wb As Workbook
Dim sFound As String, WB1 As Workbook, WB2 As Workbook

Set WB1 = ActiveWorkbook

sFound = Dir(ActiveWorkbook.Path & "\*Name.xlsx")    'the first one found

If sFound <> "" Then

    Set WB2 = Workbooks.Open(Filename:=ActiveWorkbook.Path & "\" & sFound)
    WB2.Worksheets("Sheet2").Range("A5").Copy
    WB1.Worksheets("Sheet2").Range("K18").PasteSpecial xlPasteValues

End If



End Sub

答案 2 :(得分:0)

如果您想使用干净的代码:请勿使用Sub Import_data() Dim wb As Workbook Dim sFound As String, WB1 As Workbook, WB2 As Workbook Set WB1 = ThisWorkbook sFound = Dir(WB1.path & "\*Name.xlsx") 'the first one found If sFound <> "" Then Set WB2 = Workbooks.Open(Filename:=WB1.path & "\" & sFound) WB2.Worksheets("Sheet2").Range("A5").Copy _ WB1.Worksheets("Sheet2").Range("K18") End If End Sub

尝试一下:

{{1}}