作為 Excel VBA 新手,我沒有找到解決方案。
我有一張 1,000 多行的表格。我正在嘗試創建一個宏,它將隱藏與我輸入到單元格 A1(范圍中的起始行)和單元格 A2(范圍中的結束行)中的值匹配的所有行。
這是我要達到的基本思想。
Sub Macro1()
'
' Hide rows when row numbers are between the values in cell A1 and Cell A2.
'
' Range("A1:A15").Select 'Trying to modify this so that I get values from Cells A1 and A2.
Rows(Range("A1").Value:Range("A2").Value).Select ' This is my failed attempt.
Selection.EntireRow.Hidden = True
Range("A1").Select
End Sub
謝謝你。
幫助贊賞。
蓋伊
uj5u.com熱心網友回復:
根據我的評論,我假設您想要隱藏您在A1和A2中指定的兩個值之間的行(包括) 。
您需要清理它以確保正確的作業表已應用于它。現在,它適用于活動作業表。
我還假設如果您更改數字,您想要取消隱藏所有以前隱藏的行。
Public Sub HideRows()
Dim lngRowFrom As Long, lngRowTo As Long, objSheet As Worksheet
Set objSheet = ActiveSheet
With objSheet
On Error GoTo ErrorCatchNotNumeric
lngRowFrom = .Range("A1").Value
lngRowTo = .Range("A2").Value
On Error GoTo 0
If lngRowFrom > .Rows.Count Or lngRowFrom < 1 Then GoTo ErrorCatchNotNumeric
If lngRowTo > .Rows.Count Or lngRowTo < 1 Then GoTo ErrorCatchNotNumeric
If lngRowFrom > lngRowTo Then
MsgBox "Your ""From"" row is greater than your ""To"" row.", vbCritical, "Error"
Else
.Rows.Hidden = False
.Rows(lngRowFrom & ":" & lngRowTo).EntireRow.Hidden = True
End If
End With
Exit Sub
ErrorCatchNotNumeric:
MsgBox "The values you supplied are not valid!", vbInformation, "Error"
End Sub
按照您認為合適的方式進行調整。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/413788.html
標籤:
