【发布时间】:2018-05-08 16:38:56
【问题描述】:
我的工作簿中有 10 张工作表 - 这些工作表是从各个工作簿中导入的 - 这些工作簿是从不同的监控工具中提取的
我需要对所有 10 个工作表应用过滤器,但是,并非所有工作表都采用相同的格式/结构。
对于 6 个工作表,列标题相同且顺序相同。
剩下的 4 张有不同的标题。例如:过滤器需要查找标题名称 Status - 这适用于具有相同结构的 6 个工作表,但是,其他 4 个工作表具有以下内容:
wsheet1:
用户状态 而不是 Status - 我需要将标题更改为 Status
wsheet2:
Current_Status 而不是 Status - 我需要将标题更改为 Status
下面是示例代码,它应该操纵 指定的工作表,以使其“看起来”与其他工作表相同,但是,我遇到了一些非常烦人的问题'不应用于指定的工作表,而是在执行宏时应用于“Activesheet”。
这是我的代码:
Sub arrangeSheets()
Dim lastCol As Long, idCount As Long, nameCount As Long, headerRow As Long
Dim worksh As Integer, WS_Count As Integer, i As Integer, count As Integer
Dim rng As Range, cel As Range, rngData As Range
Dim worksheetexists As Boolean
worksh = Application.Sheets.count
worksheetexists = False
headerRow = 1 'row number with headers
lastCol = Cells(headerRow, Columns.count).End(xlToLeft).Column 'last column in header row
idCount = 1
nameCount = 1
' Set WS_Count equal to the number of worksheets in the active
' workbook.
WS_Count = ActiveWorkbook.Worksheets.count
'If Application.Match finds no match it will throw an error so we need to skip them
On Error Resume Next
For x = 1 To worksh
If Worksheets(x).Name = "wsheet1" Then
worksheetexists = True
Set rng = Sheets("wsheet1").Range(Cells(headerRow, 1), Cells(headerRow, lastCol)) 'header range
With Worksheets("wsheet1").Name
Rows(2).Delete
Rows(1).Delete
count = Application.Match("*USER STATUS*", Worksheets("wsheet1").Range("A1:AZ1"), 0)
If Not IsError(count) Then
For Each cel In rng 'loop through each cell in header
If cel = "*USER STATUS*" Then 'check if header is "Unit ID"
cel = "STATUS" & idCount 'rename "Unit ID" using idCount
idCount = idCount + 1 'increment idCount
End If
Next cel
End If
End With
Exit For
End If
Next x
End Sub
【问题讨论】:
-
您是说您只需将
User Status更改为Status onwsheet1` 并在wsheet2上将Current_Status更改为Status? -
这一切都在同一个工作簿中运行吗?请注意,首先要排序的是不要只使用单元格或范围,而是使用它们附带的工作表名称,否则它将默认为 Activesheet
-
我需要对 4 个工作表进行更多更改,但我需要有关如何“关注”正确工作表的帮助,然后我将处理我需要操作工作表的代码。@ ashleedawg
-
感谢@QHarr 的回复,我还在学习,所以我会在这些方面对自己进行更多的教育。