Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 使用类似名称后缀对工作表进行分组_Excel_Vba - Fatal编程技术网

Excel 使用类似名称后缀对工作表进行分组

Excel 使用类似名称后缀对工作表进行分组,excel,vba,Excel,Vba,我正在努力找出解决这个问题的最佳方法。我希望将工作表选项卡分组,并根据后缀对它们进行颜色编码 例如: 工作表名称: ToDo_XY Done_ZY ToDo_ZY Done_XY 应该是: ToDo_XY Done_XY ToDo_ZY Done_ZY 我知道工作表名称将在最后三位以“非字母数字字符”结尾,然后是两个字母,我需要按这两个字母分组 我不确定是否应该使用集合、字典或数组 以下是我到目前为止的情况: Public Sub GroupLabSheets() Call GetLab

我正在努力找出解决这个问题的最佳方法。我希望将工作表选项卡分组,并根据后缀对它们进行颜色编码

例如:

工作表名称:

ToDo_XY
Done_ZY
ToDo_ZY
Done_XY
应该是:

ToDo_XY
Done_XY
ToDo_ZY
Done_ZY
我知道工作表名称将在最后三位以“非字母数字字符”结尾,然后是两个字母,我需要按这两个字母分组

我不确定是否应该使用集合、字典或数组

以下是我到目前为止的情况:

Public Sub GroupLabSheets()

 Call GetLabListFromTextFile

 Dim ThirdLastCharStr As String, ThirdLastCharStrIsAlphaNumBool As Boolean, PossibleLabStr As String, PossibleLabStrExistsBool As Boolean
 
  For Each ws In ActiveWorkbook.Sheets
  ThirdLastCharStr = Mid(ws.Name, Len(ws.Name) - 3, 1)
  ThirdLastCharStrIsAlphaNumBool = IsAlphaNumeric(ThirdLastCharStr)
  PossibleLabStr = Right(ws.Name, 2)
  PossibleLabStrExistsBool = mylabs.Exists(PossibleLabStr)
     If ThirdLastCharStrIsAlphaNumBool = False And PossibleLabStrExistsBool = True Then
     
      Debug.Print "Worksheet Name = " & ws.Name & " - Index = " & ws.Index
           
     End If
  Next ws
  
   
  Dim WSArr As Variant
  WSArr = Array("ToDo_XY", "Done_XY")
  'WSArr.Move Before:=Sheets(1)

  Dim i As Long
  For i = LBound(WSArr) To UBound(WSArr)
   Debug.Print Worksheets(WSArr(i)).Name
   Worksheets(WSArr(i)).Tab.Color = WHLabTabColor
   Worksheets(WSArr(i)).Move Before:=Sheets(1)
  Next i
End Sub

公共函数是布尔型的alphanumeric(ByVal vInput作为变量)
关于错误转到错误处理程序
作为对象的Dim oRegEx
如果IsNull(vInput)=False,则
设置oRegEx=CreateObject(“VBScript.RegExp”)
oRegEx.Pattern=“^[a-zA-Z0-9]+$”
IsAlphaNumeric=oRegEx.测试(输入)
其他的
IsAlphaNumeric=True'空值返回True,请根据需要进行修改
如果结束
错误\u处理程序\u退出:
出错时继续下一步
如果Not oRegEx为Nothing,则设置oRegEx=Nothing
退出功能
错误\u处理程序:
调试。打印“发生以下错误”&vbCrLf&vbCrLf&_
“错误编号:”&错误编号&vbCrLf&_
“错误源:IsAlphaNumeric”&vbCrLf&_
“错误描述:”&错误描述&_
开关(Erl=0,”,Erl 0,vbCrLf和“线路号:”&Erl)_
,vbOKOnly+vbCritical,“发生错误!”
恢复错误\u处理程序\u退出
端函数
尝试以下代码:

Option Explicit

Sub RearrangeTabs()
    Dim a() As String, i As Integer, j As Integer, buf As String, ws As Worksheet
    Dim colour As Long
    
    With ActiveWorkbook
        ReDim a(1 To .Worksheets.Count, 1 To 2)
        i = 1
        For Each ws In .Worksheets
            buf = ws.Name
            ' make sort key
            a(i, 1) = Right(buf, 2) & IIf(Left(buf, 1) = "T", "A", "Z")
            a(i, 2) = buf
            i = i + 1
        Next
        
        ' primitive bubble sort
        For i = LBound(a, 1) To UBound(a, 1)
            For j = LBound(a, 1) To UBound(a, 1)
                If a(i, 1) < a(j, 1) Then
                    buf = a(i, 1): a(i, 1) = a(j, 1): a(j, 1) = buf
                    buf = a(i, 2): a(i, 2) = a(j, 2): a(j, 2) = buf
                End If
            Next j
        Next i
        
        colour = 3 'start ColorIndex (built-in set of colors [1..56])
        For i = UBound(a, 1) To LBound(a, 1) Step -1
            Set ws = .Worksheets(a(i, 2))
            ws.Tab.ColorIndex = colour
            ws.Move Before:=.Worksheets(1)
            ' increment ColorIndex for every odd i
            If i Mod 2 = 1 Then colour = colour Mod 56 + 1
        Next i
    End With
End Sub
选项显式
子选项卡()
Dim a()作为字符串,i作为整数,j作为整数,buf作为字符串,ws作为工作表
暗淡的颜色和长的一样
使用ActiveWorkbook
重拨a(1到.Worksheets.Count,1到2)
i=1
对于每个ws-In.工作表
buf=ws.Name
'设置排序键
a(i,1)=右(buf,2)和IIf(左(buf,1)=“T”、“a”、“Z”)
a(i,2)=buf
i=i+1
下一个
“原始气泡排序”
对于i=LBound(a,1)到UBound(a,1)
对于j=LBound(a,1)到UBound(a,1)
如果a(i,1)
之前

之后
请尝试以下代码:

Option Explicit

Sub RearrangeTabs()
    Dim a() As String, i As Integer, j As Integer, buf As String, ws As Worksheet
    Dim colour As Long
    
    With ActiveWorkbook
        ReDim a(1 To .Worksheets.Count, 1 To 2)
        i = 1
        For Each ws In .Worksheets
            buf = ws.Name
            ' make sort key
            a(i, 1) = Right(buf, 2) & IIf(Left(buf, 1) = "T", "A", "Z")
            a(i, 2) = buf
            i = i + 1
        Next
        
        ' primitive bubble sort
        For i = LBound(a, 1) To UBound(a, 1)
            For j = LBound(a, 1) To UBound(a, 1)
                If a(i, 1) < a(j, 1) Then
                    buf = a(i, 1): a(i, 1) = a(j, 1): a(j, 1) = buf
                    buf = a(i, 2): a(i, 2) = a(j, 2): a(j, 2) = buf
                End If
            Next j
        Next i
        
        colour = 3 'start ColorIndex (built-in set of colors [1..56])
        For i = UBound(a, 1) To LBound(a, 1) Step -1
            Set ws = .Worksheets(a(i, 2))
            ws.Tab.ColorIndex = colour
            ws.Move Before:=.Worksheets(1)
            ' increment ColorIndex for every odd i
            If i Mod 2 = 1 Then colour = colour Mod 56 + 1
        Next i
    End With
End Sub
选项显式
子选项卡()
Dim a()作为字符串,i作为整数,j作为整数,buf作为字符串,ws作为工作表
暗淡的颜色和长的一样
使用ActiveWorkbook
重拨a(1到.Worksheets.Count,1到2)
i=1
对于每个ws-In.工作表
buf=ws.Name
'设置排序键
a(i,1)=右(buf,2)和IIf(左(buf,1)=“T”、“a”、“Z”)
a(i,2)=buf
i=i+1
下一个
“原始气泡排序”
对于i=LBound(a,1)到UBound(a,1)
对于j=LBound(a,1)到UBound(a,1)
如果a(i,1)
之前

之后
试试这个:

子排列表()
Dim i等长,wb作为工作簿,ws作为工作表
Dim dict作为对象,后缀,颜色,col作为集合,n作为长度
颜色=要应用的阵列(vbRed、vbYellow、vbGreen、vbBlue)颜色(可能需要添加更多…)
Set dict=CreateObject(“scripting.dictionary”)
设置wb=ThisWorkbook
'根据后缀收集并分组所有匹配的工作表
对于wb.工作表中的每个ws
如果SortIt(ws)那么
后缀=右(ws.Name,2)
如果不存在dict.exists(后缀),则dict.Add后缀,新集合
dict(后缀)。添加ws
如果结束
下一个ws
'现在在组上循环并移动组中的所有图纸
'在该组中的第一张之后
当i=0时,计数为-1
设置列=目录项()(i)
对于n=1到列计数
设置ws=col(n)
ws.Tab.Color=颜色(i)
如果n>1,那么ws.Move after:=col(n-1)
下一个
接下来我
端接头
'此工作表是否可用于排序?
函数SortIt(ws作为工作表)作为布尔值
将nm变暗为字符串
nm=UCase(ws.Name)
如果Len(nm)>=4,则
SortIt=(不是像“[A-Z0-9]”一样的中间(nm,Len(nm)-2,1)和_
右(nm,2)像“[A-Z][A-Z]”
如果结束
端函数
试试这个:

子排列表()
Dim i等长,wb作为工作簿,ws作为工作表
Dim dict作为对象,后缀,颜色,col作为集合,n作为长度
颜色=要应用的阵列(vbRed、vbYellow、vbGreen、vbBlue)颜色(可能需要添加更多…)
Set dict=CreateObject(“scripting.dictionary”)
设置wb=ThisWorkbook
'根据后缀收集并分组所有匹配的工作表
对于wb.Worksh中的每个ws