比较Sheet的单元格数据在Excel VBA中不起作用

问题描述 投票:-2回答:1
Option Explicit
Private Sub CommandButton1_Click()
    '//this macro checks whether two sheet's data is correspondent.
    Dim i1 As Integer
    Dim i2 As Integer
    For i2 = 1 To i2 = 31
        For i1 = 1 To i1 = 27
            If ThisWorkbook.Worksheets(UserForm2.TextBox1.Value).Cells(i1, i2).Value <> _
            ThisWorkbook.Worksheets(UserForm2.TextBox2.Value).Cells(i1, i2).Value Then
                MsgBox "Value of " & i1 & "rows " & i2 & "column : " & Chr(13) & Chr(10) _
                & UserForm2.TextBox1.Value & " sheets : " & ThisWorkbook.Worksheets(UserForm2.TextBox1.Value).Cells(i1, i2).Value & Chr(13) & Chr(10) _
                & UserForm2.TextBox2.Value & " sheets :  : " & ThisWorkbook.Worksheets(UserForm2.TextBox2.Value).Cells(i1, i2).Value
            End If
        Next
    Next
    MsgBox "End"
End Sub

以上是我的代码。

它的功能很简单。

它从UserForm TextBox接收两个工作表的名称

并检查两张纸是否具有相同的内容

但是,即使没有错误消息,它也不起作用。

我按了按钮,但没有回应。

只弹出“结束”消息。

我怎么解决这个问题?

提前感谢您的回答。

excel vba
1个回答
0
投票

这更多是通过扩展评论。

请参阅以下内容作为重写(我尚未测试),但展示了如何使用变量来存储值,从而提高效率。我建议你逐步检查代码,检查每个变量的值(在本地窗口或手表中),并确保它们符合您的预期。运行某些您希望看到失败的情况,并为您的监视值的值更改插入中断?这里有debugging的参考。

Private Sub CommandButton1_Click()
    '//this macro checks whether two sheet's data is correspondent.
    Dim i1 As Long
    Dim i2 As Long

    Dim text1 As String
    Dim text2 As String

    text1 = UserForm2.TextBox1.Value
    text2 = UserForm2.TextBox2.Value

    Dim testSheet1 As Worksheet
    Dim testSheet2 As Worksheet

    testSheet1 = ThisWorkbook.Worksheets(text1)
    testValue2 = ThisWorkbook.Worksheets(text2)

    For i2 = 1 To 31

        For i1 = 1 To 27

            If testSheet1.Cells(i1, i2).Value <> testSheet2.Cells(i1, i2).Value Then

                MsgBox "Value of " & i1 & "rows " & i2 & "column : " & Chr(13) & Chr(10) _
                & text1 & " sheets : " & testSheet1.Cells(i1, i2).Value & Chr(13) & Chr(10) _
                & text2 & " sheets :  : " & testSheet2.Cells(i1, i2).Value

            End If

        Next i1

    Next i2

    MsgBox "End"

End Sub
© www.soinside.com 2019 - 2024. All rights reserved.