要求用户输入在VBA中复制工作表

时间:2015-04-22 20:15:26

标签: excel vba excel-vba

我想将表格从VBA中的特定工作簿复制到我的Active Workbook,并使用它进行大量计算。现在的问题是目标表名称总是在不断变化,我总是会收到错误。

Set targetWorkbook = Application.ActiveWorkbook
filter = "Text files (*.xls*),*.xls*"
Caption = "Please Select the Target file"
Ret = Application.GetOpenFilename(filter, , Caption)

If Ret = False Then Exit Sub
Application.AskToUpdateLinks = False
Set wb = Workbooks.Open(Ret)
Application.AskToUpdateLinks = True
wb.Worksheets("**This Keeeps on Changing**").Move After:=targetWorkbook.Sheets(targetWorkbook.Sheets.Count)

我可以选择或输入MsgBox中的名称或类似名称,以便我不会收到错误。请帮忙。

2 个答案:

答案 0 :(得分:1)

Set targetWorkbook = Application.ActiveWorkbook
Filter = "Text files (*.xls*),*.xls*"
Caption = "Please Select the Target file"
Ret = Application.GetOpenFilename(Filter, , Caption)

If Ret = False Then Exit Sub
Set wb = Workbooks.Open(Ret, False) 'why set it on application level when it's an optional argument?

For Each Worksheet In wb
    If LCase(Worksheet.Name) Like "*changing*" Then                              'it might be changing but it probably has a fixed part, right? 'note: you can use wildcards and string conversion rules
        Worksheet.Move After:=targetWorkbook.Sheets(targetWorkbook.Sheets.Count) 'do whatever it did before
    End If
Next

答案 1 :(得分:1)

Sub ertdfgcvb()
Set targetWorkbook = Application.ActiveWorkbook
Filter = "Text files (*.xls*),*.xls*"
Caption = "Please Select the Target file"
Ret = Application.GetOpenFilename(Filter, , Caption)

If Ret = False Then Exit Sub
Set wb = Workbooks.Open(Ret, False) 'why set it on application level when it's an optional argument?

shname = InputBox("What worksheet are you looking for?", "Changing sheet names are for losers")
For Each Worksheet In wb
    If LCase(Worksheet.Name) Like "*" & LCase(shname) & "*" Then                              'it might be changing but it probably has a fixed part, right? 'note: you can use wildcards and string conversion rules
        Worksheet.Move After:=targetWorkbook.Sheets(targetWorkbook.Sheets.Count) 'do whatever it did before
    End If
Next
End Sub