我有一个像日历一样的工作表 - 每列都是一个工作日,直到月底,A列是任务的名称,每行是一个特定的任务(8个任务),然后下个月使用相同的任务。
目前我们使用每个任务的切换按钮,使用以下代码隐藏每一行(为了节省空间而移除其他行):
@ECHO OFF
SET "PathSave=C:\Test.txt"
ECHO(
ECHO Collecting BIOS, CPU and Network Information
ECHO(
( WMIC BIOS Get Manufacturer,SerialNumber
WMIC CPU Get Name
WMIC NICConfig)|MORE>"%PathSave%"
ECHO Press any key to complete ...
PAUSE>NUL
这很好,但是这个日历现在不断增长,其他任务要求我们进入每个togglebutton和每一行来改变它以适应新的任务。由于日历不属于我,我没有任何形式的编码经验,所以之前发现,实际的用户不想手动更改(我可以理解为什么,这可能是耗时的)。我想知道我们是否可以在togglebutton中使用'IF'功能?
如此有效:
按ToggleButton13
Private Sub ToggleButton13_Click()
If ToggleButton13.Value = True Then
Rows(22).EntireRow.Hidden = True
Else
Rows(22).EntireRow.Hidden = False
End If
End Sub
答案 0 :(得分:0)
假设:A栏中的条目是连续的......当您的表格不断增长时,它会派上用场,所以只需要遵循一种格式。
Private Sub ToggleButton13_Click()
Dim indexCol, indexRow as Integer
dim myKey as String
myKey = "Admin"
indexCol = 1 ' For Column A
indexRow = 2 'row index from where the first entry starts in the Column
' loop will traves each row utill columnA entry correspondig to that row is not empty
Do While IsEmpty(ActiveSheet.Cells(indexRow, indexCol)) = False
If ActiveSheet.Cells(indexRow, indexCol).Value = myKey Then
Rows(indexRow).EntireRow.Hidden = True
Else
Rows(indexRow).EntireRow.Hidden = False
End If
indexRow = indexRow + 1
Loop
End Sub
答案 1 :(得分:0)
尝试一般循环,类似于Ron Rosenfield在评论中建议的内容。因为你说你对编码缺乏经验,所以会对它进行评论:
Dim i, LR as Integer 'i will be variable for row in the loop, LR as variable for last row
LR = Cells(Rows.Count,1).End(xlUp).Row 'Determines the last row based on contiguous cells in column 1, aka column A
For i = 2 to LR 'Starting at Row 2 with the assumption that you have headers
If Cells(i,1).Value="Admin" Then 'Cells(row,column) here is column 1, aka column A.
Rows(i).EntireRow.Hidden=True 'Hides row if true
Else
Rows(i).EntireRow.Hidden=False 'Ensure row is not hidden if false.
End If
Next i
这将在你的按钮点击宏中。
答案 2 :(得分:0)
我能想到的两种方式:
遍历每一行并设置隐藏属性。
由于代码在ToggleButton后面,我们不需要指定工作表名称。代码将在当前活动的工作表上运行,这是正确的工作表,因为您在其上按下了ToggleButton。
当ToggleButton返回TRUE或FALSE时,我们可以将hidden属性设置为该值。
Private Sub ToggleButton1_Click()
Dim rDataRange As Range
Dim rCell As Range
'Set rDataRange = Range("A2", Cells(Rows.Count, 1).End(xlUp))
'Take into account hidden rows:
Set rDataRange = Range("A2", HiddenLastCell(ActiveSheet))
For Each rCell In rDataRange
If rCell = "Admin" Then
Rows(rCell.Row).EntireRow.Hidden = ToggleButton1
End If
Next rCell
End Sub
在rDataRange
中设置范围使用此功能查找最后一个单元格,即使它被隐藏了:
Public Function HiddenLastCell(wrkSht As Worksheet) As Range
Dim rLastCell As Range
Dim bHasHiddenData As Boolean
Dim rSearch As Range
Dim lLastCol As Long, lLastRow As Long
Dim lRow As Long
With wrkSht
Set rLastCell = .Columns(1).Find("*", , , , xlByColumns, xlPrevious)
If Not rLastCell Is Nothing Then
bHasHiddenData = rLastCell.Row <> .UsedRange.Rows.Count
Else
bHasHiddenData = .UsedRange.Rows.Count > 1
End If
If bHasHiddenData Then
Set rSearch = .Range(.Cells(1, 1), .Cells(.UsedRange.Row + .UsedRange.Rows.Count - 1, 1))
For lRow = rSearch.Rows.Count + 1 To 2 Step -1
If .Cells(lRow, 1) = vbNullString And .Cells(lRow - 1, 1) <> vbNullString Then
Set HiddenLastCell = .Cells(lRow, 1)
End If
Next lRow
Else
On Error Resume Next
lLastCol = .Cells.Find("*", , , , xlByColumns, xlPrevious).Column
lLastRow = .Columns(1).Find("*", , , , xlByColumns, xlPrevious).Row
If lLastCol = 0 Then lLastCol = 1
If lLastRow = 0 Then lLastRow = 1
Set HiddenLastCell = wrkSht.Cells(lLastRow, lLastCol)
On Error GoTo 0
End If
End With
End Function
使用AutoFilter隐藏行。
将此代码添加到按钮:
Private Sub ToggleButton1_Click()
FilterAdmin ToggleButton1.Value
End Sub
此代码将应用或删除过滤器:
Sub FilterAdmin(ToggleOn As Boolean)
Dim rDataRange As Range
Dim rLastCell As Range
With ThisWorkbook
If ToggleOn Then
Set rLastCell = LastCell(.Worksheets("Sheet1"))
With .Worksheets("Sheet1")
'Get reference to data range (A1 to last used cell).
'Or you can manually set the range.
Set rDataRange = .Range(.Cells(1, 1), rLastCell)
'If auto-filter isn't turned on then turn it on.
If Not .AutoFilterMode Then rDataRange.AutoFilter
'Remove any applied filters.
If .FilterMode Then .ShowAllData
rDataRange.AutoFilter Field:=1, Criteria1:="<>Admin", Operator:=xlAnd
End With
Else
If .Worksheets("Sheet1").FilterMode Then .Worksheets("Sheet1").ShowAllData
End If
End With
End Sub
此函数将返回包含数据的最后一个单元格,并在上述过程中使用:
Public Function LastCell(wrkSht As Worksheet, Optional Col As Long = 0) As Range
Dim lLastCol As Long, lLastRow As Long
On Error Resume Next
With wrkSht
If Col = 0 Then
lLastCol = .Cells.Find("*", , , , xlByColumns, xlPrevious).Column
lLastRow = .Cells.Find("*", , , , xlByRows, xlPrevious).Row
Else
lLastCol = .Cells.Find("*", , , , xlByColumns, xlPrevious).Column
lLastRow = .Columns(Col).Find("*", , , , xlByColumns, xlPrevious).Row
End If
If lLastCol = 0 Then lLastCol = 1
If lLastRow = 0 Then lLastRow = 1
Set LastCell = wrkSht.Cells(lLastRow, lLastCol)
End With
On Error GoTo 0
End Function
您可以更改代码中的工作表引用以使用ActiveSheet
而不是Sheet1
(再次,当您按下切换按钮时,正确的工作表将处于活动状态。)