2017-05-26 397 views
0

我正在複製工作表並使用第三個工作表的單元格值對其重命名。我遇到的唯一問題是如何將新工作表聲明爲變量,因爲我將在該工作表上工作?在最後一行,我收到一個錯誤,說「期望:語句結束」。複製工作表並重命名它並聲明變量vBA

Dim wsNew As Worksheet 
Dim wsIntro As Worksheet 
Dim wsUp As Worksheet 
Set wsUp = Worksheets("Sheet1") 

Set wsIntro = Worksheets("Instructions") 

Worksheets("Sheet1").Copy after:=Sheets(Worksheets.Count) 
With ActiveSheet.UsedRange 
.Value = .Value 
End With 
ActiveSheet.name = wsIntro.Range("b6").Value & wsIntro.Range("b7").Value 
Dim wsAllo As Worksheet 
Set wsAllo = "wsIntro.Range("b6").Value & wsIntro.Range("b7").Value" 
+1

'Set wsAllo = Worksheets(wsIntro.Range(「b6」)。Value&wsIntro.Range(「b7」)。Value)' –

+0

This work too,thank you。 – Angel

回答

2

當您試圖設置引用的工作表是ActiveSheet,你可以簡單地改變

Set wsAllo = "wsIntro.Range("b6").Value & wsIntro.Range("b7").Value" 

Set wsAllo = ActiveSheet 

重構你的代碼稍微給:

Dim wsNew As Worksheet 
Dim wsIntro As Worksheet 
Dim wsUp As Worksheet 
Dim wsAllo As Worksheet 

Set wsUp = Worksheets("Sheet1") 
Set wsIntro = Worksheets("Instructions") 

'You shouldn't use "Sheets(Worksheets.Count)" - it will sometimes not do 
'what you expect (when you have Charts as well as Worksheets in the Workbook) 
'Use either "Sheets(Sheets.Count)" to place the new sheet as the last sheet 
'in the workbook or use "Worksheets(Worksheets.Count)" to place the new sheet 
'after the last worksheet in the workbook (but possibly with Charts after that)  
wsUp.Copy After:=Sheets(Sheets.Count) 
Set wsAllo = ActiveSheet 
With wsAllo 
    .Name = wsIntro.Range("b6").Value & wsIntro.Range("b7").Value 
    .UsedRange.Value = .UsedRange.Value 
End With 
+0

謝謝你的幫助,會做出這些改變。 – Angel

相關問題