我试图找到一个仅根据某个单元格重命名某些工作表的宏,并在其他工作表中将该同一单元格用于其他用途。我看过两个宏函数。告诉它从工作簿的第n张表开始。另一个告诉它仅重命名某些工作表。两种选择都有问题。如果要添加工作表怎么办?这样,您的第一个热表将与以前的第n个表不同。现在可能是第11张,而不是第5张。您的宏将需要更新,这违背了整个目的。至于第二个选项,只需列出要重命名的所有工作表:第一次运行宏后该怎么办?宏中的工作表名称将需要更新以反映新的工作表名称,这也违反了整个目的。
我尝试通过vba表名称而不是布局表名称运行宏。而且仍然无法正常工作。
Sub h100_monthyear()
Dim rs As Worksheet
For Each rs In Sheets
If x01st.select <> And x02nd.select <> And x03rd.select <> And x04th.select <> And x05th.select <> And x06th.select <> And x07th.select <> And x08th.select <> And x09th.select <> And x10th.select <> And x11th.select <> And x12th.select <> And x13th.select <> And x14th.select <> And x15th.select <> And x16th.select <> And x17th.select <> And x18th.select <> And x19th.select <> And x20th.select <> And x21st.select <> And x22nd.select <> And x23rd.select <> And x24th.select <> And x25th.select <> And x26th.select <> And x27th.select <> And x28th.select <> And x29th.select <> And x30th.select <> And x31st.select <> Then
rs.Name = rs.Range("h100")
Next rs
End Sub
我无法从该线程Renaming Sheets in Macro without renaming first four sheets中获得任何其他解决方案,而David(站点管理员)大喊大叫,试图将其发布到该位置。用户要当心,显然,您在发布某些内容时必须小心。
为快速参考,这是上面基于的最基本的宏:
Sub h100_monthyear()
Dim rs As Worksheet
For Each rs In Sheets
rs.Name = rs.Range("h100")
Next rs
End Sub
我还以这种方式对VBA表名称进行了尝试:
x01st <> And x02nd <> And x03rd <> And x04th <> And x05th <> And x06th <> And x07th <> And x08th <> And x09th <> And x10th <> And x11th <> And x12th <> And x13th <> And x14th <> And x15th <> And x16th <> And x17th <> And x18th <> And x19th <> And x20th <> And x21st <> And x22nd <> And x23rd <> And x24th <> And x25th <> And x26th <> And x27th <> And x28th <> And x29th <> And x30th <> And x31st <> Then
有人可以帮忙吗?
答案 0 :(得分:0)
您的问题是此If x01st.select <> And x02nd.select <> And x03rd.select <> And x04th.select …
无效的语法。
我建议将所有允许重命名的工作表的名称放入数组WsNameList
中,并在循环中检查当前工作表rs.Name
的名称是否在{{1 }}。因此,我使用了函数WsNameList
。
IsInArray
或者(没有数组列表)
Sub h100_monthyear()
Dim WsNameList As Variant
WsNameList = Array("x01st", "x02nd", "x03rd", "x04th", "x05th") 'add more sheets here
Dim rs As Worksheet
For Each rs In Worksheets
If IsInArray(rs.Name, WsNameList) Then
rs.Name = rs.Range("h100")
End If
Next rs
End Sub
Public Function IsInArray(ByVal FindString As String, ByVal InArray As Variant) As Boolean
IsInArray = (UBound(Filter(InArray, FindString)) > -1)
End Function