我在使用过去工作得很好的代码时遇到了很多困难。在我的一个潜艇中,我隐藏了一堆行(准确地说是1599),然后取消隐藏我需要的行,通常是200左右。查看代码:
Private Sub HideUnneededRows(numberToShow As Integer)
Dim thisSheet As Worksheet
Dim allHideableRows As String, rowsToShow As String
Dim firstRow As Integer
Dim secondRow As Integer
Dim lastRow As Integer
Dim lastRowToShow As Integer
Set thisSheet = ThisWorkbook.Sheets(1)
firstRow = thisSheet.Range("mass1").row
secondRow = firstRow + 1
lastRow = firstRow - 1 + maxNumberOfRows
lastRowToShow = firstRow - 1 + numberToShow '//numberToShow is usually 200
'//THIS NEXT LINE RUNS, BUT STOPS EXECUTING AFTER THE ACTION IS PERFORMED
Range("A" + CStr(secondRow) + ":A" + CStr(lastRow)).EntireRow.Hidden = True
Range("A" + CStr(secondRow) + ":A" + CStr(lastRowToShow)).EntireRow.Hidden = False
End Sub
一切都很好,直到代码隐藏行("范围中的第一个(" A" + ....)。隐藏"行)。它执行隐藏操作就好了,但是没有到达下一行代码,这将是取消隐藏不同的行集。 我尝试过的事情:
Range("A25:A1623")
而不是Range("A" + CStr(secondRow) + ":A" + ....)
)Rows
对象(即Rows("25:1623").Hidden = True
)button_click
事件中,以查看它是否与当前所在模块有关所有这些都产生了完全相同的结果 - 代码在隐藏1599行后停止所有执行。
帮助?!
答案 0 :(得分:1)
以下用于调试目的的代码片段可以正常工作(隐藏/取消隐藏行)
Private Sub HideUnneededRows()
Dim thisSheet As Worksheet
Dim allHideableRows As String, rowsToShow As String
Dim firstRow As Integer
Dim secondRow As Integer
Dim lastRow As Integer
Dim lastRowToShow As Integer
'for debugging purpose
numberToShow = 200
maxNumberOfRows = 300
'--------------------
Set thisSheet = ThisWorkbook.Sheets(1)
'firstRow = thisSheet.Range("mass1").Row
' for debugging purpose
firstRow = 1
secondRow = firstRow + 1
lastRow = firstRow - 1 + maxNumberOfRows
lastRowToShow = firstRow - 1 + numberToShow '//numberToShow is usually 200
'//THIS NEXT LINE RUNS, BUT STOPS EXECUTING AFTER THE ACTION IS PERFORMED
Range("A" + CStr(secondRow) + ":A" + CStr(lastRow)).EntireRow.Hidden = True
Range("A" + CStr(secondRow) + ":A" + CStr(lastRowToShow)).EntireRow.Hidden = False
End Sub
检查所有"魔术数字"比如maxNumberOfRows
变量(设置为300用于调试目的:它来自/声明?)和firstRow
来自那个"神秘的"范围" mass1"。
注意 :在VBA代码中,字符串连接通常使用符号" &
"来完成,但此语法可以正常工作很好:
Range("A" + CStr(secondRow) + ":A" + CStr(lastRow))
希望这能解决您的问题。
答案 1 :(得分:1)
感谢@PeterT,我的问题的解决方案是在运行代码之前关闭屏幕更新,然后在代码运行完毕后重新打开它。新代码是这样的:
Private Sub HideUnneededRows(numberToShow As Integer)
Dim thisSheet As Worksheet
Dim firstRow As Integer
Dim secondRow As Integer
Dim lastRow As Integer
Dim lastRowToShow As Integer
Set thisSheet = ThisWorkbook.Sheets(1)
firstRow = thisSheet.Range("mass1").row
secondRow = firstRow + 1
lastRow = firstRow - 1 + maxNumberOfRows
lastRowToShow = firstRow - 1 + numberToShow '//numberToShow is usually 200
'//Turn off ScreenUpdating
Application.ScreenUpdating = False
'//THIS NEXT LINE WAS GIVING ME TROUBLES
Range("A" + CStr(secondRow) + ":A" + CStr(lastRow)).EntireRow.Hidden = True
Range("A" + CStr(secondRow) + ":A" + CStr(lastRowToShow)).EntireRow.Hidden = False
'//Turn ScreenUpdating back on
Application.ScreenUpdating = True
End Sub
答案 2 :(得分:0)
看起来你来自C#/ Java方面 - 使用+
连接字符串,在VBA中,使用&符号&
。
假设maxNumberOfRows
是全局变量或函数,请尝试以下。
Private Sub HideUnneededRows(numberToShow As Integer)
Dim firstRow As Long, secondRow As Long
Dim lastRow As Long, lastRowToShow As Long
Dim lCalcMode As Long
firstRow = ThisWorkbook.Names("mass1").RefersToRange.Row
secondRow = firstRow + 1
lastRow = firstRow - 1 + maxNumberOfRows
lastRowToShow = firstRow - 1 + numberToShow '//numberToShow is usually 200
lCalcMode = Application.Calculation
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Rows(secondRow & ":" & lastRow).Hidden = True
Rows(secondRow & ":" & lastRowToShow).Hidden = False
Application.Calculation = lCalcMode
Application.ScreenUpdating = True
End Sub
将与行相关的数据类型更改为Long
(保存Excel执行转换)。