使用输入框分配工作簿名称,创建暗处

时间:2019-10-11 19:54:14

标签: excel vba

我正在尝试创建一个宏,该宏最初将通过输入框接收2个工作簿名称(文件名),将为每个工作簿创建一个dim,然后将特定的单元格从workbook1复制到workbook2。

我可以在两张纸之间进行复印,但是我需要有关暗处的输入框的帮助。

根据我的需要,我从其他地方提取了一些代码并对其进行了编辑,但是我似乎无法使其正常工作。 Assigning variable workbook name with Inputbox VBA

我对昏暗的人来说还很陌生,所以我确定那里只是缺少什么。

Sub copytoworking()

Dim wb As Workbook
Dim X As String
X = InputBox("What is this Working workbook's name?")
Set wb = Workbooks(X)
With Workbooks(X & ".xlsx")

Dim wb2 As Workbook
Dim Y As String
Y = InputBox("What is the source data workbook's name?")
Set wb2 = Workbooks(Y)
With Workbooks(Y & ".xlsx")

'copying between sheets code is here

End With
End With
End Sub

现在我在Set wb = Workbooks(X)上收到错误“ 9”。

1 个答案:

答案 0 :(得分:0)

在InputBox中输入诸如-的值

 "C:\Users\HP\Documents\EbayPrice"

以下内容并非十全十美,但它应该引导您朝正确的方向发展。您可能需要进行一些验证以避免打开非Excel文件,并且如果您单击“取消”而不是选择文件。

 Dim wb, wb2, wbname, wb2name As String
    wb = Application.GetOpenFilename()
    wb2 = Application.GetOpenFilename()
    Workbooks.Open Filename:=wb
    wbname = ActiveWorkbook.Name
    Workbooks.Open Filename:=wb2
    wb2name = ActiveWorkbook.Name
    Windows(wbname).Activate
    Windows(wb2name).Activate