将工作表名称从0开始插入单元格

时间:2016-07-18 15:07:41

标签: excel vba excel-vba

我想给单元格命名为我的工作表(064149107)。名称无法更改,工作簿中有许多工作表。

我的代码工作正常,除非我的工作表名称以0开头。

例如,代替064149107,我在单元格中获得64149107。

由于我在此之后正在进行vlookup,因此找不到匹配项并且错误为2042.

这是我的代码:

sht.Cells(LastRowsht + 1, 2) = sht.Name

我也尝试了这个:

            If Left(sht.Name, 1) = "0" Then 'règle le probleme d'un sheet name commencant par 0
            sht.Cells(LastRowsht + 1, 2) = "0" & sht.Name
            Else
            sht.Cells(LastRowsht + 1, 2) = sht.Name
            End If

但我没有工作。

3 个答案:

答案 0 :(得分:1)

此代码会在名称前加上一个'字符。

sht.Cells(LastRowsht + 1, 2).Value = chr(39) & sht.Name

或者,您可以先将单元格的格式设置为Text

sht.Cells(LastRowsht + 1, 2).NumberFormat = "@"
sht.Cells(LastRowsht + 1, 2).Value = sht.Name

答案 1 :(得分:0)

你能尝试类似的东西吗?

sht.Cells(LastRowsht + 1, 2).Value = "'" & sht.Name

我没有尝试过,但是通过明确地将其发布,我认为它应该有效。

答案 2 :(得分:0)

您只需在“数字”选项卡中通过功能区“主页”更改单元格的格式,将“常规”更改为“文本”。然后再次运行代码。

但请注意,您不需要VBA来填充单元格中的工作表名称。这可以使用单元格公式来完成:

=MID(CELL("filename",A1),FIND("]",CELL("filename",A1))+1,255)
相关问题