【问题标题】:Greating a macro for 3 different actions in 1为 1 中的 3 种不同操作创建宏
【发布时间】:2016-12-16 14:29:55
【问题描述】:

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

所以这里可能有问题。

有一个工作簿,里面有大约 30 张工作表和数据。我需要根据工作表的名称将数据转换为某种格式。我为每种工作良好的格式创建了一个 marco,并应用于所有选定的工作表。 我的想法是在检查工作表名称并格式化数据时创建一个调用三个格式宏之一的宏。类似的东西

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"

无论如何感谢您的宝贵时间 =)

【问题讨论】:

    标签: excel macros vba


    【解决方案1】:

    您可以使用 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
    

    【讨论】:

      【解决方案2】:

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

      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
      

      【讨论】:

        【解决方案3】:

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

        选择案例表.名称 案例 111 至 119 模块名称_1 案例 222 至 229 模块名称_2 案例 333 至 339 模块名称_3 结束选择

        【讨论】:

          【解决方案4】:

          如果工作表名称的第一个字符是决定因素,也许这个

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

          【讨论】:

            猜你喜欢
            • 2012-10-12
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2018-12-20
            • 1970-01-01
            • 1970-01-01
            • 2012-04-01
            • 2015-08-09
            相关资源
            最近更新 更多