gpt4 book ai didi

VBA 代码在隐藏行后停止执行而不会出错

转载 作者:行者123 更新时间:2023-12-04 20:12:12 25 4
gpt4 key购买 nike

我在处理一些过去可以正常工作的代码时遇到了很大的困难。在我的一个潜艇中,我隐藏了一堆行(准确地说是 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

一切都很好,直到代码隐藏行(“Range(“A”+....).Hidden”行的第一个)。它很好地执行了隐藏操作,但没有到达下一行代码,这将是取消隐藏一组不同的行。
我尝试过的事情:
  • 硬编码范围字符串(即 Range("A25:A1623") 而不是 Range("A" + CStr(secondRow) + ":A" + ....) )
  • 通过 Rows 执行这两个隐藏操作对象(即 Rows("25:1623").Hidden = True )
  • 将这两条隐藏线提取到自己的button_click新模块中的事件以查看它是否与当前位于
  • 中的模块有关

    所有这些都产生了完全相同的结果——代码在 1599 行被隐藏后停止了所有执行。

    帮助?!

    最佳答案

    感谢@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

    关于VBA 代码在隐藏行后停止执行而不会出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35853960/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com