簡體   English   中英

“對象‘范圍’的方法‘值’失敗”和 excel 崩潰

[英]“Method 'Value' of object 'Range' failed” and excel crashes

我創建了一個用戶表單,其中一個命令按鈕啟動了另一個可以輸入數據的用戶表單。 然后將此數據添加到工作表中的表中,然后卸載用戶表單,用戶返回到原始用戶表單。 當數據要輸入到工作表中時會發生錯誤。 這個用戶表單可以自己完美運行,但是當它從第一個用戶表單啟動時,就會發生錯誤。

Private Sub CommandButton1_Click()

    'check all fields are filled
    Dim nextRow As Integer
    Dim nextCell As String

    If Len(Trim(ComboBox1.Value)) = 0 Then
        MsgBox "All feilds must be filled"
        Exit Sub
    End If

    If Len(Trim(TextBox1.Value)) = 0 Then
        MsgBox "All feilds must be filled"
        Exit Sub
    End If

    If Len(Trim(TextBox2.Value)) = 0 Then
        MsgBox "All feilds must be filled"
        Exit Sub
    End If

    'Check if supplier ID already exists
    Dim FindString As String
    Dim Rng As Range

    FindString = TextBox1.Value

        If Trim(FindString) <> "" Then
            With Sheet4.Range("B:B")
                Set Rng = .Find(What:=FindString, _
                                After:=.Cells(.Cells.Count), _
                                LookIn:=xlValues, _
                                LookAt:=xlWhole, _
                                SearchOrder:=xlByRows, _
                                SearchDirection:=xlNext, _
                                MatchCase:=False)
                If Not Rng Is Nothing Then
                    Application.Goto Rng, False
                    MsgBox "Sorry Bro, " & FindString & " already exists!"
                    Exit Sub
                Else
                    FindString = TextBox2

                        If Trim(FindString) <> "" Then
                            With Sheet4.Range("D:D")
                                Set Rng = .Find(What:=FindString, _
                                                After:=.Cells(.Cells.Count), _
                                                LookIn:=xlValues, _
                                                LookAt:=xlWhole, _
                                                SearchOrder:=xlByRows, _
                                                SearchDirection:=xlNext, _
                                                MatchCase:=False)
                                If Not Rng Is Nothing Then
                                    Application.Goto Rng, False
                                    MsgBox "Sorry Bro, the Ordering Details you entered:" & vbNewLine & _
                                            "'" & FindString & "'" & vbNewLine & _
                                            "Already exists in our Database!" & vbNewLine & _
                                            "U wanna check ur data?"
                                    Exit Sub
                                End If
                            End With
                        End If

                End If
            End With
        End If



    'enter supplier ID into sheet
    Sheet4.Activate
    nextRow = ActiveSheet.Range("B2", Range("B2").End(xlDown)).Count
    nextCell = Cells(nextRow + 2, 2).Activate

    'this is where the error occurs
    ActiveCell.Value = TextBox1.Value
    ActiveCell.Offset(0, 1).Value = ComboBox1.Value
    ActiveCell.Offset(0, 2).Value = TextBox2.Value

    Sheet2.Activate

    Unload Me
    End Sub

我不確定為什么它不起作用,因為我個人避免使用“激活”。 也許你可以試試這是否有效:

    'Previous code that worked fine

    nextRow = ActiveSheet.Range("B2", Range("B2").End(xlDown)).Count

    With ActiveSheet.Cells(nextRow + 2, 2)
        .Value = TextBox1.Value
        .Offset(0, 1).Value = ComboBox1.Value
        .Offset(0, 2).Value = TextBox2.Value
    End With
    Sheet2.Activate

    Unload Me
    End Sub

希望這能完成工作! (請注意,這是我的第一個答案,所以我非常願意接受反饋)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM