2016-06-24 68 views
-1

我正在嘗試獲取最新工作表並擦除舊工作表,但無法獲取保存變量的值。獲取最新工作表並擦除舊工作表

第一個循環用於存儲二維數組上的值。第二個循環是獲取最新的工作表。

Sub WorksheetEraseOld() 
    Dim ShtNames() As String 
    Dim SaveSheet As String 
    ReDim ShtNames(1 To ActiveWorkbook.Sheets.count) 
    For i = 1 To Sheets.count 
     ShtNames(i) = Sheets(i).Name 
    Next i 

    ReDim Preserve ShtNames(1 To ActiveWorkbook.Sheets.count) 
    For J = 1 To Sheets.count 
     If J = Sheets.count Then Exit For 
     Range("A1").Value = Val(Left(ShtNames(J), 4)) 
     Range("B1").Value = Val(Left(ShtNames(J + 1), 4)) 
     MsgBox ("Content of SaveSheet is " & SaveSheet) 
     SaveSheet = ShtNames(J) 
     If Val(Left(SaveSheet, 4)) < Val(Left(ShtNames(J + 1), 4)) Then SaveSheet = ShtNames(J + 1) 
    Next J 
End Sub 

回答

0
For J = 1 To Sheets.Count 
    If J = Sheets.Count Then Exit For 
Next J 
Range("A1").Value = Val(Left(ShtNames(J), 4)) 
Range("B1").Value = Val(Left(ShtNames(J + 1), 4)) 
MsgBox ("Content of SaveSheet is " & SaveSheet) 
SaveSheet = ShtNames(J) 
If Val(Left(SaveSheet, 4)) < Val(Left(ShtNames(J + 1), 4)) Then SaveSheet = ShtNames(J + 1) 
相關問題