用新值替换字符串

时间:2019-04-04 07:46:35

标签: excel vba

我的工作表名称为PMCC 1,而我的工作表中的内容为PMCC#01
我想使用替换功能将工作表名称转换为PMCC#01

我尝试了这段代码

temp = ws.Name
newtemp = Replace(temp,"","#0")

结果,我只得到1。

4 个答案:

答案 0 :(得分:1)

您是否只需要:

ws.Name = Replace(ws.Name, " ", "#0")

主要区别是" "而不是""

答案 1 :(得分:0)

尝试:

Option Explicit

Sub test()

    Dim strName As String
    Dim ws As Worksheet

    With ThisWorkbook

        For Each ws In .Worksheets '<- Loop all sheets
            With ws
                strName = .Range("A1") '<- Let us assume that the new name appears in all sheets at range A1
                .Name = strName '<- Change sheet name
            End With
        Next ws

    End With

End Sub

答案 2 :(得分:0)

主要问题是:您已经创建了一个变量,并在其中输入了一个来自图纸名称的值,并且您认为修改该变量的值会自动修改您的图纸名称。

这不是变量的工作方式:您特别需要将变量的值放入工作表名称中,例如:

temp = ws.Name
newtemp = Replacement_function(temp, ...) // not sure to use `Replace()` or `Substitute()`
ws.Name = newtemp

答案 3 :(得分:0)

如果工作表名称的数字元素可以为10或更高,则可能需要这样做:

temp = Split(ws_Name, " ")
newtemp = temp(0) & "#" & Format(temp(1), "00")

这样,PMCC 1变成PMCC#01,但是PMCC 13变成PMCC#13,而不是PMCC#013

相关问题