通过 VBA 重命名工作表名称无法按预期工作

问题描述 投票:0回答:1

最根本的是 - 我在使用 if 子句循环中使用 VBA 重命名工作簿工作表时遇到问题。

复制工作表就像一个魅力,但工作表不会像预期的那样被重命名。

对于 i = 1...38,它们显示标题为“TEMPLATE (i)”,而不是使用递增的索引值进行重命名。

这里有什么魔法?

Sub dataExtraction()

Dim user_state As Integer
Dim index As Integer
Dim nums As Integer
Dim i As Integer
Dim x As Integer

Dim status As String
Dim billingMethod As String

status = "inaktiv"
billingMethod = "Projektverrechnung"
x = 1

For i = 2 To 500

    If Cells(i, 13).Value = status Then index = index + 1 Else
    If Cells(i, 13).Value = status Then Sheets("TEMPLATE").Copy After:=Sheets(Sheets.Count) Else
    
    Worksheets("TEMPLATE (2)").Activate
    
    If Cells(i, 13).Value = status Then Sheets("TEMPLATE (2)").Name = index Else
    
    Worksheets("dataset").Activate
            
Next i
    
MsgBox (index)
MsgBox "The name of the active sheet is " & ActiveSheet.Name

End Sub
excel vba
1个回答
0
投票

似乎

dataset
工作表包含值
inaktiv
。在这种情况下,激活
TEMPLATE (2)
工作表后,有必要限定单元格(i,13)。

For i = 2 To 500

    If Cells(i, 13).Value = status Then index = index + 1 Else
    If Cells(i, 13).Value = status Then Sheets("TEMPLATE").Copy After:=Sheets(Sheets.Count) Else
    
    Worksheets("TEMPLATE (2)").Activate
    
    If Worksheets("dataset").Cells(i, 13).Value = status Then Sheets("TEMPLATE (2)").Name = index Else    ' modification
    
    Worksheets("dataset").Activate
            
Next i
© www.soinside.com 2019 - 2024. All rights reserved.