Excel 为1中的3个不同操作将宏变灰

Excel 为1中的3个不同操作将宏变灰,excel,macros,vba,Excel,Macros,Vba,对不起,我的语言不是母语 所以这里有个问题 有一本工作簿,里面有大约30张工作表和数据。我需要根据工作表的名称将数据转换为特定格式。我为每种格式创建了一个marco,该格式运行良好,适用于所有选定的工作表。 我的想法是创建一个宏,在检查工作表名称后调用三种格式marcos之一,并格式化数据。差不多 If sheet.name = "111, 112, 113..." then 'if it fit the name do the format-1 call "Module_name_1" E

对不起,我的语言不是母语

所以这里有个问题

有一本工作簿,里面有大约30张工作表和数据。我需要根据工作表的名称将数据转换为特定格式。我为每种格式创建了一个marco,该格式运行良好,适用于所有选定的工作表。 我的想法是创建一个宏,在检查工作表名称后调用三种格式marcos之一,并格式化数据。差不多

If sheet.name = "111, 112, 113..." then 'if it fit the name do the format-1

call "Module_name_1"

Else if sheet.name = "222, 223, 224..." then 'if it fit the name do the format-2

    call "Module_name_2"

Else sheet.name = "333, 334, 335..." then 'if it fit the name do the format-3

    call "Module_name_3"

感谢您抽出时间=)

检查所有工作表名称的前两个字符:

Sub SID()
    Dim shn As String, sh As Worksheet
    Dim shn2 As String

    For Each sh In Sheets
        shn = sh.Name
        shn2 = Left(shn, 2)
        If shn2 = "11" Then Call Module_name_1
        If shn2 = "22" Then Call Module_name_2
        If shn2 = "33" Then Call Module_name_3
    Next sh
End Sub

您可以使用select case语句检查名称。 比较时,此处的数字应转换为字符串。这样,您就可以根据自己的价值轻松地进行分支。 如果需要更改限制,请更改119、229和339

Select Case sheet.name
    Case 111 To 119
'if it falls into the region then do the format-1

        call "Module_name_1"

    Case 222 To 229
    'if it fit the name do the format-2

        call "Module_name_2"

    Case 333 To 339
    'if it fit the name do the format-3

         call "Module_name_3"

End Select

如果图纸名称的第一个字符是决定因素,可能

Application.Run "Module_name_" & Left(shn, 1)

你甚至不需要“呼叫”,但它看起来很漂亮,而且可能更直观。如果你真的想简化事情,试试这样的方法

选择Case sheet.name 案件111至119 模块名称1 案件222至229 模块名称2 案件333至339 模块名称3 结束选择