【问题标题】:Update data based on serial number根据序列号更新数据
【发布时间】:2019-02-05 21:30:45
【问题描述】:

我有一个用户窗体,它允许用户将数据输入到工作表中。

根据2个ComboBox选择和最后0001为每行数据创建一个序列号。

例如,MAPR0001,其中MA 来自一个组合框,PR 来自另一个组合框,最后添加了0001,并随着MAPR 的另一个选择而递增。 (MAPR0002)

然后我有第二个用户窗体,应该允许我更新我的数据库。
从组合框中选择序列号后,第二个用户窗体将数据从工作表拉回一些文本框。 到这里一切正常。

但我无法将数据添加到特定序列号。

我的命令按钮代码:

Private sub Commandbuttonclick ()
    If Me.ComboBox1.Value = "" Then
        MsgBox "Request No. Can Not be Blank", vbExclamation, "Request No."
        Exit Sub
    End If

    requestno = Me.ComboBox1.Value
    Sheets("DASHBOARD").Select

    Dim rowselect As Double
    rowselect = Me.combobox1.Value
    rowselect = rowselect + 1
    Rows(rowselect).Select

    Cells(rowselect, 2) = Me.TextBox1.Value
    Cells(rowselect, 3) = Me.TextBox2.Value
    Cells(rowselect, 4) = Me.TextBox3.Value

【问题讨论】:

  • 在我看来很好,我只是回答错误的人;)
  • 我觉得不好看 ;) ① Avoid using Select in Excel VBADim rowselect As Double 必须是 Dim rowselect As Long
  • 使用WorksheetFunction.Match method 查找您要更新的序列号。 Match 返回您可以使用而不是 rowselect 来写入数据的行号。

标签: excel vba


【解决方案1】:

使用WorksheetFunction.Match method 查找要更新的序列号。 Match 返回您可以使用而不是 rowselect 来写入数据的行号。

例如这样的:

Dim MySerial As String
MySerial = "MAPR0001" 'adjust to your needs

Dim MyLookupRange As Range
Set MyLookupRange = Sheets("DASHBOARD").Range("A:A") 'adjust to where your serials are

Dim RowToUpdate As Long
On Error Resume Next 'next line throws error if serial not found
RowToUpdate = WorksheetFunction.Match(MySerial, MyLookupRange, 0)
On Error Goto 0 'always re-enable error reporting!

If RowToUpdate > 0 Then
    'serial found, update here eg …
    'Sheets("DASHBOARD").Cells(RowToUpdate, 2) = Me.TextBox1.Value
Else
    MsgBox "Serial " & MySerial & " was not found."
End If

【讨论】:

    【解决方案2】:

    有几种方法可以做到这一点。这是您尝试的一种选择。

    'Private Sub Worksheet_Change(ByVal Target As Range)
    
    Sub ImportDataFromExcel()
        Dim rng As Range
        Dim r As Long
        Dim conn As ADODB.Connection
        Dim strConn As String
        Dim strSQL As String
    
        strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
            "C:\Users\Ryan\Desktop\Coding\Integrating Access and Excel and SQL Server\Access & Excel & SQL Server\" & _
            "EXCEL AND ACCESS AND SQL SERVER\Excel & Access\Select, Insert, Update & Delete\Northwind.mdb"
        Set conn = New ADODB.Connection
        conn.Open strConn
    
        With Worksheets("Sheet1")
            lastrow = .Range("A2").End(xlDown).Row
            lastcolumn = .Range("A2").End(xlToRight).Column
            Set rng = .Range(.Cells(lastrow, 1), .Cells(lastrow, lastcolumn))
        End With
    
            'therow = 1
    
            For i = 2 To lastrow
                'r = rng.Row
                'If r > 1 Then
                    strSQL = "UPDATE PersonInformation SET " & _
                        "FName='" & Worksheets("Sheet1").Range("B" & i).Value & "', " & _
                        "LName='" & Worksheets("Sheet1").Range("C" & i).Value & "', " & _
                        "Address='" & Worksheets("Sheet1").Range("D" & i).Value & "', " & _
                        "Age=" & Worksheets("Sheet1").Range("E" & i).Value & " WHERE " & _
                        "ID=" & Worksheets("Sheet1").Range("A" & i).Value
                    conn.Execute strSQL
                'End If
                'r = r + 1
            Next i
    
    
        conn.Close
        Set conn = Nothing
    End Sub
    

    这仅用于说明目的。请根据您的特定需求进行更改。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-25
      • 2021-01-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多