2015-04-22 36 views
0

我想將VBA中特定工作簿中的工作表複製到我的活動工作簿中,並使用它進行大量計算。現在的問題是,目標工作表名稱總是不斷變化,我總是得到一個錯誤。要求用戶輸入以複製VBA中的工作表

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或類似的東西,我沒有得到一個錯誤。請幫忙。

+0

設置工作表複製到一個變量,現在你必須對它的引用,並且可以使用可變繼續你的代碼,以及在同一時間更改名稱。 [這裏是一個例子](http://stackoverflow.com/a/7692456/2521004)作者:Tim Williams –

+0

如果你想走在路上,你是建議,'mySheet = inputbox(...'應該做的技巧 –

回答

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 
+1

明白了,會嘗試使用它,它只是我使用的文件的數據沒有被我更新,並且看到了表格名稱的變化,所以只是想使它變得一般。 – Meesha

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 
+0

inb4方法,而不是規則 – user3819867

相關問題