首页 > 解决方案 > 如何使用 VBA 在多个工作表中修改选定 Excel 工作表中的记录

问题描述

我有一个 Excel 数据输入表单和两个 Excel 数据库表,分别是 Table1 和 Table2。在数据输入表单中,我提供了一个选择 Table1 或 Table2 的选项,并且我想使用数据输入表单中的修改按钮根据我的表选择来修改 Table1 记录或 Table2 记录。表记录应根据序号进行修改。但是当我选择一个表并使用序列号修改所选表中的记录时,错误显示为“未找到记录”。这是为什么?如何解决这个问题?下面给出了用于修改记录的 module1 编码

Sub ModifyRecord()
    
    

    Dim iRow As Long
    Dim iSerial As Long
    
    iSerial = Application.InputBox("Please enter Serial Number to make modification.", "Modify", , , , , , 1)
    
    On Error Resume Next
    
    iRow = Application.WorksheetFunction.IfError _
    (Application.WorksheetFunction.Match(iSerial, Sheets("Database").Range("A:A"), 0), 0)
    
    On Error GoTo 0
    
    If iRow = 0 Then
     
        MsgBox "No record found.", vbOKOnly + vbCritical, "No Record"
        Exit Sub
        
    End If
    
    
    Sheets("Form").Range("L1").Value = iRow
    Sheets("Form").Range("M1").Value = iSerial
    
    Sheets("Form").Range("H9").Value = Sheets("Database").Cells(iRow, 2).Value
     
    Sheets("Form").Range("H11").Value = Sheets("Database").Cells(iRow, 3).Value
    
    Sheets("Form").Range("H13").Value = Sheets("Database").Cells(iRow, 4).Value
    
    Sheets("Form").Range("H15").Value = Sheets("Database").Cells(iRow, 5).Value
    
    Sheets("Form").Range("H17").Value = Sheets("Database").Cells(iRow, 6).Value
    
    Sheets("Form").Range("H19").Value = Sheets("Database").Cells(iRow, 7).Value
    
    Sheets("Form").Range("H21").Value = Sheets("Database").Cells(iRow, 8).Value
    
    Sheets("Form").Range("H23").Value = Sheets("Database").Cells(iRow, 9).Value
    
    
    
End Sub

sheet1(form) coding for modifying a record is given below

Private Sub cmdModify_Click()

    Dim msgValue As VbMsgBoxResult
    
    msgValue = MsgBox("Do you want to modify the record?", vbYesNo + vbQuestion, "Modify Record")
    
        If msgValue = vbYes Then
        
            Call ModifyRecord
            
        End If
       
End Sub

如何修复此错误。请帮我

标签: excelvbarecord

解决方案


推荐阅读