Excel VBA在循环不到300行后崩溃。

huangapple go评论64阅读模式
英文:

Excel VBA keeps crashing after looping for less than 300 rows

问题

对不起,由于您的要求,我将只返回已翻译的部分,不包括代码部分。

我第一次尝试使用Excel VBA,但在循环大约280行后,代码崩溃的问题让我很困扰。目标是使Sheet1中与Sheet2中具有相同前一列的单元格在每次进行更改时更新(反之亦然)。以下是Sheet1的代码,Sheet2的代码相同,只有Worksheets()对象不同。

我曾尝试过以下方法:

为Sheet1的代码如下:

Private Sub Worksheet_Change(ByVal Target As Range)
Dim counter As Integer
    For counter = 10 To 290
        If Worksheets("Sheet2").Cells(counter, 6).Value = Target.Offset(0, -1).Value Then
            Worksheets("Sheet2").Cells(counter, 7).Value = Target.Value
        End If
    Next counter
End Sub

Sheet2的代码如下:

Private Sub Worksheet_Change(ByVal Target As Range)
Dim counter As Integer
    For counter = 19 To 66
        If Worksheets("Sheet1").Cells(counter, 6).Value = Target.Offset(0, -1).Value Then
            Worksheets("Sheet1").Cells(counter, 7).Value = Target.Value
        End If
    Next counter
End Sub

非常感谢您的帮助,提前谢谢!

英文:

I'm currently working on Excel VBA for the first time and I'm having some trouble getting the code below to not crash after looping for about 280 rows. The goal is to have the cells in Sheet1 that have the same previous column as the cells in Sheet2 to update each time we make a change (and vice versa). The code below is for Sheet1, the code for Sheet2 is identical except for the Worksheets() object.

I was testing this on a smaller Excel workbook and it was working with no issues on either worksheets however going into the actual Excel I want to update is crashing Excel with the Error:
-2147417848 (80010108): Method 'Value' of Object 'Range' Failed.

Here's what I tried:

Private Sub Worksheet_Change(ByVal Target As Range)
Dim counter As Integer
    For counter = 10 To 290
        If Worksheets("Sheet2").Cells(counter, 6).Value = Target.Offset(0, -1).Value Then
            Worksheets("Sheet2").Cells(counter, 7).Value = Target.Value
        End If
    Next counter
End Sub```

For the sheet 2, the code is the following:

Private Sub Worksheet_Change(ByVal Target As Range)
Dim counter As Integer
    For counter = 19 To 66
        If Worksheets("Sheet1").Cells(counter, 6).Value = Target.Offset(0, -1).Value Then
            Worksheets("Sheet1").Cells(counter, 7).Value = Target.Value
        End If
    Next counter
End Sub```

Any help is greatly appreciated, thank you in advance!

答案1

得分: 0

Private Sub Worksheet_Change(ByVal Target As Range)
Dim rg As Range
Dim counter As Integer
Application.EnableEvents = False
Set rg = Range("G10:G290")
If Not Intersect(Target, rg) Is Nothing Then
For counter = 10 To 290
If Sheets("Sheet2").Cells(counter, 6).Value = Target.Offset(0, -1).Value Then
Sheets("Sheet2").Cells(counter, 7).Value = Target.Value
End If
Next
End If
Application.EnableEvents = True
End Sub

rg 用于限制更改的单元格区域。因此,如果更改的单元格位于 G10:G290 之外,它不会触发循环。

并且应该在子程序开头设置 application.enableevents=false,并在子程序结束前将其设置为 true,以避免触发另一个工作表模块中的 Sheet_Change 子程序。

这种代码的工作方式类似于这样:
假设在 sheet1 和 sheet2 中,单元格 F15、F20 和 F25 的值都是 "A"。
sheet1 和 sheet2 中,单元格 G15 的值 = 1,G20 = 2,G25 = 3。

在 sheet1 的单元格 G20 中,用户键入 7。
结果是 sheet2 的单元格 G15、G20 和 G25 的值都为 7,
但是在 sheet1 的单元格 G15 的值仍然是 1,单元格 G25 的值仍然是 3。

稍后,另一个用户在 sheet2 的单元格 G25 中键入 100。
结果是 sheet1 的单元格 G15、G20 和 G25 的值都为 100,
但是在 sheet2 的单元格 G15 和 G20 的值仍然是 7。

这是您期望的结果吗?

英文:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rg As Range: Dim counter As Integer
Application.EnableEvents = False
Set rg = Range("G10:G290")
    If Not Intersect(Target, rg) Is Nothing Then
        For counter = 10 To 290
            If Sheets("Sheet2").Cells(counter, 6).Value = Target.Offset(0, -1).Value Then
                Sheets("Sheet2").Cells(counter, 7).Value = Target.Value
            End If
        Next
    End If
Application.EnableEvents = True
End Sub

the rg is to restrict the area of the cell which is changed.
So, if the cell changed is outside G10:G290, it won't trigger the loop.

And it should have the application.enableevents=false in the beginning and set it back to true ---> application.enableevents=true just before the end sub to avoid the sheet_change sub in another sheet module is triggered.

FYI, this kind of code will work something like this :<br>
Say, in sheet1 and sheeet2 cell F15,F20 and F25 value is "A".<br>
And in sheet1 and sheeet2 cell G15 value = 1, G20 = 2, G25 = 3.<br>

In sheet1 cell G20, a user type 7.<br>
Result in sheet2 cell G15, G20 and G25 value is 7, <br>
but in sheet1 cell G15 value is still 1 and cell G25 value is still 3.

Later on, another user type 100 in sheet2 cell G25.<br>
Result in sheet1 cell G15, G20 and G25 value is 100, <br>
but in sheet2 cell G15 and cell G20 value is still 7.

Is that your expected result ?

huangapple
  • 本文由 发表于 2023年4月11日 05:21:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/75980837.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定