简体   繁体   English

相同的代码在不同的用户表单上使用不同

[英]Same code working differently on different userforms

I've created a userform for recording and retrieving data about engines being tested. 我创建了一个用户表单,用于记录和检索有关正在测试的引擎的数据。 For one type of engine the code is working fine, for another I'm getting a problem. 对于一种类型的引擎,代码工作正常,另一种我遇到了问题。 The only difference between the two is the name of the page where the data is stored, for the working one it's DW10 data and for the non working one it's XUD9 data. 两者之间的唯一区别是存储数据的页面的名称,对于工作的页面,它是DW10数据,而对于非工作的页面,它是XUD9数据。 It will write to the XUD9 data and retrieve from it with no problem but it won't allow me to update it so it can't be the name of the sheet as none of the userform would work. 它将写入XUD9数据并从中检索没有任何问题,但它不允许我更新它,因此它不能是工作表的名称,因为userform都不起作用。

The XUD9 userform is exactly the same as the DW10 one, it's a copy with just the name references changed. XUD9用户窗体与DW10用户窗体完全相同,只是更改了名称引用的副本。

Private Sub confirmupdate_Click()

Dim Rerow As Range

Set Rerow = Worksheets("DW10 Data").Range("H:H").Find(Codetext, searchdirection:=xlPrevious)


Worksheets("DW10 Data").Cells(Rerow.Row, 2).Value = Rigtext2.Text
Worksheets("DW10 Data").Cells(Rerow.Row, 4).Value = Serialtext2.Text
Worksheets("DW10 Data").Cells(Rerow.Row, 5).Value = Hourstext2.Text
Worksheets("DW10 Data").Cells(Rerow.Row, 3).Value = CDbl(CDate(Datetext2))
Worksheets("DW10 Data").Cells(Rerow.Row, 6).Value = parttext2.Text
Worksheets("DW10 Data").Cells(Rerow.Row, 7).Value = commentstext2.Text
Worksheets("DW10 Data").Cells(Rerow.Row, 8).Value = codetext2.Text

confirmupdate.Visible = False

End Sub

Private Sub confirmupdate_Click()

Dim Rerow As Range

Set Rerow = Worksheets("XUD9 Data").Range("H:H").Find(Codetext, searchdirection:=xlPrevious)


Worksheets("XUD9 Data").Cells(Rerow.Row, 2).Value = Rigtext2.Text
Worksheets("XUD9 Data").Cells(Rerow.Row, 4).Value = Serialtext2.Text
Worksheets("XUD9 Data").Cells(Rerow.Row, 5).Value = Hourstext2.Text
Worksheets("XUD9 Data").Cells(Rerow.Row, 3).Value = CDbl(CDate(Datetext2))
Worksheets("XUD9 Data").Cells(Rerow.Row, 6).Value = parttext2.Text
Worksheets("XUD9 Data").Cells(Rerow.Row, 7).Value = commentstext2.Text
Worksheets("XUD9 Data").Cells(Rerow.Row, 8).Value = codetext2.Text

confirmupdate.Visible = False

End Sub

The error message I get on the XUD9 version is basically Rerow is Nothing, so it obviously can't find the Codetext value to return the row it's looking for, but DW10 works fine with exactly the same code. 我在XUD9版本上得到的错误信息基本上是Rerow is Nothing,所以它显然找不到Codetext值来返回它正在寻找的行,但是DW10在完全相同的代码下工作正常。

Is there something obvious I'm missing? 有什么明显的东西我不见了吗? I am new to VBA and am self taught (google is your friend) so it could be something very simple. 我是VBA的新手并且自学成才(google是你的朋友)所以它可能非常简单。

You could use the below code to ensure that what you are looking for appears in the sheets. 您可以使用以下代码确保您要查找的内容显示在工作表中。

Option Explicit
Public Rerow As Range

Sub test(ByVal wsName As String, strCodeText As String)

    With ThisWorkbook.Worksheets(wsName)

        Set Rerow = .Range("H:H").Find(strCodeText, searchdirection:=xlPrevious)

        If Not Rerow Is Nothing Then
            .Cells(Rerow.Row, 2).Value = Rigtext2.Text
            .Cells(Rerow.Row, 4).Value = Serialtext2.Text
            .Cells(Rerow.Row, 5).Value = Hourstext2.Text
            .Cells(Rerow.Row, 3).Value = CDbl(CDate(Datetext2))
            .Cells(Rerow.Row, 6).Value = parttext2.Text
            .Cells(Rerow.Row, 7).Value = commentstext2.Text
            .Cells(Rerow.Row, 8).Value = codetext2.Text
        Else
            MsgBox strCodeText & " not appears in column H"
        End If

    End With

    confirmupdate.Visible = False

End Sub

Private Sub confirmupdate_Click()

    Dim Codetext As String

    Codetext = "Test" 'Change the Codetext to what you are looking for
    Call Module1.test("DW10 Data", Codetext)

    Codetext = "Test2" 'Change the Codetext to what you are looking for
    Call Module1.test("XUD9 Data", Codetext)


End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM