简体   繁体   English

当工作表名称为数字时,索引号/工作表名称与变量混淆

[英]Index number/worksheet name confusion with variables when worksheet names are numerical

I might have found a novel problem! 我可能发现了一个新问题! I'm trying to select worksheets based on the name of the worksheet, using a variable for the name since this is all part of a loop. 我正在尝试根据工作表的名称选择工作表,使用变量作为名称,因为这是循环的所有部分。 But I run into trouble when the worksheet's name is a number, since if I have, say, name = 5, then Worksheets(name) gives me the index number 5 worksheet and Worksheets("name") of course looks for a worksheet named "name". 但是当工作表的名称是一个数字时遇到麻烦,因为如果我有,比方说,name = 5,那么Worksheets(name)给我索引号5工作表和Worksheets(“name”)当然会找到一个名为的工作表“名称”。

I could do a workaround with adding a letter to each worksheet's name so it's not treated as a number, and then removing those letters afterwards, but is there a better way? 我可以为每个工作表的名称添加一个字母,以便它不被视为数字,然后删除这些字母,但是有更好的方法吗?

Sub RenameFiles()
Dim source, old_filename, old_tab, new_filename As String
Dim i As Integer
source = Range("path").Value
For i = 1 To Range("total_file_number").Value
    old_filename = Worksheets("Import and combine").Cells(3 + i, 2).Value
    old_tab = Worksheets("Import and combine").Cells(3 + i, 3).Value
    new_filename = Worksheets(old_tab).Cells(1, 1).Value 'If old_tab is a number, VBA treats Worksheets(old_tab) as looking up a worksheet by index number rather than name
    If Left(Worksheets(old_tab).Cells(1, 1).Value, 1) = "*" Then new_filename = Right(new_filename, Len(new_filename) - 2) 'removes asterisk from name
    new_filename = Replace(new_filename, ">", "") 'removes > from name, since > can't be used in file names
    Worksheets(old_tab).Name = new_filename
    Worksheets("Import and combine").Cells(3 + i, 3).Value = new_filename
    Name source & "\" & old_filename As source & "\" & new_filename
Next i
End Sub

Your problem is caused by this line: 您的问题是由此行引起的:

Dim source, old_filename, old_tab, new_filename As String

...though it does not manifest itself until you attempt to access the Worksheets collection. ...虽然在您尝试访问Worksheets集合之前它不会显示出来。

This is a very common mistake. 这是一个非常常见的错误。 The ONLY variable in the above line that is of type String is the last one. 上面一行中String类型的唯一变量是最后一个变量。 Each of the others is of type Variant. 其他每个都是Variant类型。

When assigning to the variant old_tab any numerical value will be stored as a number, not a string. 分配变量old_tab任何数值都将存储为数字,而不是字符串。 And the problem with that is that the Worksheets collection needs a string value as a key to return the correct worksheet. 而问题是,Worksheets集合需要一个字符串值作为返回正确工作表的键。

If each of the variables in the above line is supposed to be of type String then do this: 如果上面一行中的每个变量都应该是String类型,那么执行以下操作:

Dim source As String, old_filename As String, old_tab As String, new_filename As String

...and now that old-tab is a String, it's value will work properly as a key to the Worksheets collection. ...现在old-tab是一个String,它的值将作为Worksheets集合的一个键正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM