某行用户表单用VBA代码填充数据

Filling Data with VBA Code by User Form in Certain Row

我在这个 VBA 代码中有一个案例。

基本上,我有 2 sheets:

我想要做的是在 Lending & Funding sheet 中添加数据从水平方向从 A7507 开始,在 MUFG Client 中也从 A103 开始​​。

这是我目前拥有的 VBA 代码

Private Sub CommandButton1_Click()
whichSheet = InputBox("In which sheet do you wish to enter data? Specify Sheet as Lending & Funding or MUFG Client only.", "Sheet Name")
If whichSheet = "" Then
MsgBox "You didn't specify a sheet!"
Exit Sub
End If

Worksheets(whichSheet).Activate
Dim lastrow
lastrow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
 
lastrow = lastrow + 1
Cells(lastrow, 1) = TextBox1
If Application.WorksheetFunction.CountIf(Range("A7507:A" & lastrow), Cells(lastrow, 1)) > 1 Then
MsgBox "Duplicate Data! Only Unique CIFs allowed", vbCritical, "Remove Data", Cells(lastrow, 1) = ""
ElseIf Application.WorksheetFunction.CountIf(Range("A7507:A" & lastrow), Cells(lastrow, 1)) = 1 Then
answer = MsgBox("Are you sure you want to add the record?", vbYesNo + vbQuestion, "Add Record")
If answer = vbYes Then
Cells(lastrow, 1) = TextBox1.Text
Cells(lastrow, 2) = TextBox2.Text
Cells(lastrow, 3) = TextBox3.Text
Cells(lastrow, 4) = TextBox4.Text
Cells(lastrow, 5) = TextBox5.Text
Cells(lastrow, 6) = TextBox6.Text
Cells(lastrow, 7) = TextBox7.Text
Cells(lastrow, 8) = TextBox8.Text
Cells(lastrow, 9) = TextBox9.Text
Cells(lastrow, 10) = TextBox10.Text
Cells(lastrow, 11) = TextBox11.Text
Cells(lastrow, 12) = TextBox12.Text
Cells(lastrow, 13) = TextBox13.Text
Cells(lastrow, 14) = TextBox14.Text
Cells(lastrow, 15) = TextBox15.Text
End If
End If
End Sub

Private Sub CommandButton2_Click()
Unload Me
End Sub

问题是每当我在 MUFG 客户端中添加数据时,它都会填充第 3 行而不是第 103 行。

这是您在贷款和资金中添加数据时发生的情况

有效!

但是当我在 MUFG 客户端中添加数据时

第 3 行添加了数据,而不是第 103 行。它不起作用!

如有任何帮助,我们将不胜感激。

在此先感谢您。

您的请求有点混乱,因为我们看不到您的 sheet。但是这里尝试将您的代码重写为:

  1. 不使用Activate
  2. 删除需要输入Sheet姓名。
  3. 根据所选 sheet 将 lastrow 值强制为最小值。
  4. 如果用户不想添加记录,则不添加部分记录。
  5. 为了调试而写window为了辅助调试而写的

Private Sub CommandButton1_Click()

    Dim whichsheet As String
    whichsheet = InputBox("In which sheet do you wish to enter data? Enter 1 for Lending & Funding or 2 for MUFG Client", "Sheet selector")
    
    If whichsheet <> "1" And whichsheet <> "2" Then
       MsgBox "You didn't specify a valid sheet!"
       Exit Sub
    End If
    Dim firstusablerow As Long
    If whichsheet = "1" Then
       firstusablerow = 7507
       whichsheet = "Lending & Funding"
    Else
       firstusablerow = 103
       whichsheet = "MUFG Client"
    End If
    
    With Worksheets(whichsheet)
        Dim lastrow As Long
        lastrow = .Cells(.Rows.Count, 1).End(xlUp).Row + 1
        
        If lastrow < firstusablerow Then lastrow = firstusablerow
        
        If Application.WorksheetFunction.CountIf(.Range("A" & firstusablerow & ":A" & lastrow), .Cells(lastrow, 1)) > 0 Then
            MsgBox "Duplicate Data! Only Unique CIFs allowed", vbCritical, "Remove Data", .Cells(lastrow, 1) = ""
        Else
            answer = MsgBox("Are you sure you want to add the record?", vbYesNo + vbQuestion, "Add Record")
            If answer = vbYes Then
                Debug.Print "Writing " & TextBox1.Text & " to row " & lastrow & " on sheet " & whichsheet
                .Cells(lastrow, 1) = TextBox1.Text
                .Cells(lastrow, 2) = TextBox2.Text
                .Cells(lastrow, 3) = TextBox3.Text
                .Cells(lastrow, 4) = TextBox4.Text
                .Cells(lastrow, 5) = TextBox5.Text
                .Cells(lastrow, 6) = TextBox6.Text
                .Cells(lastrow, 7) = TextBox7.Text
                .Cells(lastrow, 8) = TextBox8.Text
                .Cells(lastrow, 9) = TextBox9.Text
                .Cells(lastrow, 10) = TextBox10.Text
                .Cells(lastrow, 11) = TextBox11.Text
                .Cells(lastrow, 12) = TextBox12.Text
                .Cells(lastrow, 13) = TextBox13.Text
                .Cells(lastrow, 14) = TextBox14.Text
                .Cells(lastrow, 15) = TextBox15.Text
            End If
        End If
    End With
End Sub