我正在编写一个代码来隐藏传递当前月份(IntMonth)的数据。因此,例如,如果在IntMonth中选择了8,则应隐藏直到12日的第9个月。我尝试了不同的东西,但我无法设法做出一致的宏。公平地说,我对VBA很新。
编码目标
到目前为止我写的代码下面,这段代码有以下缺陷
当前编码错误
Sub Groeperen()
'----------------------------------------------------------------------------------------------------
'Update variables from this point
Dim IntMonth As Integer, rData As Range, rCel As Range, x As Integer, i As Long
IntMonth = Range("IntMonth")
'----------------------------------------------------------------------------------------------------
'Define sheetnumber where to start running the macro (as i)
x = 5
'----------------------------------------------------------------------------------------------------
'Don't change anything after this point
'----------------------------------------------------------------------------------------------------
Application.ScreenUpdating = False
'ActiveWorkbook.RefreshAll
For i = x To Worksheets.Count
Worksheets(i).Select
If Right(ActiveSheet.Name, 2) <> "CC" Then
Range("C5").Select
Set rData = Range(ActiveCell, ActiveCell.End(xlToRight))
Range(Columns("A"), Selection.End(xlToRight)).Select
On Error Resume Next
Selection.Columns.Ungroup
On Error Resume Next
Selection.EntireColumn.Hidden = False
Range("A1").Select
For Each rCel In rData
If rCel >= IntMonth Then
Columns(rCel.Column).Group
rCel.EntireColumn.Hidden = True
Else
If rCel = "End" Then Stop
End If
Next 'rCel
End If 'Right(Sheets(i).Name, 2) <> "CC"
Next 'i
Application.ScreenUpdating = True
End Sub
答案 0 :(得分:0)
我根据你写的内容清理了For Next
块及其中的所有内容,调整代码以完成你所追求的目标。我修复了错误的语法,以及逻辑。你非常接近在那里,你只是有一点逻辑。
For i = x To Worksheets.Count
Dim ws As Worksheet
Set ws = Worksheets(i)
With ws
If Left(.Name, 2) <> "CC" Then
.Activate
Set rData = .Range(.Range("C5"), .Range("N5")) 'can set to N5 since it's constant and it will stop before it gets to "End"
With .Range(.Range("A5").EntireColumn, .Range("N5").End(xlToRight))
On Error Resume Next
.Columns.Ungroup
On Error GoTo 0
.EntireColumn.Hidden = False
End With
For Each rCel In rData
If rCel >= IntMonth Then
.Columns(rCel.Column).Group
rCel.EntireColumn.Hidden = True
End If
Next 'rCel
End If 'Left(ws.Name, 2) <> "CC" Then
End With
Next 'i