我在工作簿中有一个参考工作表,其中包含工作表名称以及此特定工作簿中包含的所有文件的所有标头。我想使用此参考工作表从每个相应的工作表中删除不需要的列。该过程是清理已合并到单个工作簿中的60到100个文件作为单独的工作表。
参考工作表包含A列中的所有工作表名称,以及B到AH中的标题。有没有办法可以突出显示我想要删除的每个记录红色,然后运行VBA来检查参考工作表,以便从每个相应的工作表中删除哪些列。我希望有人让我开始或者指出我可以参考的过程方向来完成这项工作?我试过谷歌但无济于事。老实说,我甚至不知道如何开始。
答案 0 :(得分:0)
如果我正确阅读你的Q,工作流程是:
开始使用的示例代码(这里有一些可能需要调整的假设)
Sub DeleteHeaders()
Dim wsRef As Worksheet
Dim ws As Worksheet
Dim rRef As Range, rRow As Range
Dim cl As Range
Dim rHeader As Range
Dim RED As Long
RED = RGB(255, 0, 0)
Set wsRef = Worksheets("NameOfReferenceWorksheet")
' Get reference to all data on reference sheet
With wsRef
Set rRef = .Range(.Cells(2, 34), .Cells(.Rows.Count).End(xlUp))
End With
For Each rRow In rRef
' Get reference to listed worksheet
On Error Resume Next
Set ws = Worksheets(rRow.Cells(1, 1))
If Err.Number <> 0 Then
' Worksheet not present in workbook. What now?
On Error GoTo 0
Else
' loop over the row
On Error GoTo 0
For Each cl In rRow.Offset(0, 1).Resize(1, rRow.Columns.Count - 1)
If cl.Interior.Color = RED Then
Set rHeader = Nothing
' search for highlighted header text
With ws.Rows(1) '<-- header text in row 1
Set rHeader = .Find( _
What:=cl.Value, _
After:=.Cells(1, 1), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=True, _
SearchFormat:=False)
If rHeader Is Nothing Then
' Header not found. What now?
Else
' delete entire column
rHeader.EntireColumn.Delete
' Optional: clear colour
cl.Interior.Pattern = xlNone
End If
End With
End If
Next
End If
Next
End Sub