【问题标题】:Updating excel data with Userform to different worksheet使用 Userform 将 Excel 数据更新到不同的工作表
【发布时间】:2018-04-10 05:38:51
【问题描述】:

我创建了一个用户表单,用于将新数据输入到工作表中。表单通过工作表顶部的 FormControlButton 加载。但是,我想将 FormControlButton 放在不同的工作表上,并从该工作表完成用户表单。当我从不同的工作表(当数据集在工作表 12 中时说工作表 11)将数据输入用户表单时,用户表单不会输入新数据。

这是用户窗体上输入按钮的代码:

Dim currentrow As Long

Private Sub CommandButton2_Click()
Dim lastrow
Dim myfname As String
lastrow = Sheet11.Range("A" & Rows.Count).End(xlUp).row
myfname = Me.Reg8.Value
For currentrow = 2 To lastrow

If Cells(currentrow, 1).Text = myfname Then
 Cells(currentrow, 68).Value = Me.Reg10.Value
 Cells(currentrow, 69).Value = Me.Reg11.Value
 Cells(currentrow, 10).Value = Me.Reg5.Value
 Cells(currentrow, 9).Value = Me.Reg6.Value
 Cells(currentrow, 70).Value = Me.Reg7.Value
End If
Next

If MsgBox("Information has" & vbNewLine & "been updated") = vbOK Then
    Unload Me
    Sheet1.Select
End If
End Sub

我正在尝试将数据从 Sheet1 输入到 Sheet11。 提前致谢!

【问题讨论】:

    标签: vba excel userform worksheet


    【解决方案1】:

    试试下面的代码。鉴于该按钮将分配在您要更新的工作表上,您可以创建一个变量 WS 并将其分配给当前活动工作表(设置 ws = ActiveSheet)。请让我知道这是否可以解决您的问题。

    Private Sub CommandButton2_Click()
    Dim lastrow
    Dim myfname As String
    Dim ws as worksheet
    Set ws = ActiveSheet
    lastrow = ws.Range("A1" & Rows.Count).End(xlUp).row
    myfname = Me.Reg8.Value
    For currentrow = 2 To lastrow
    
    If ws.Cells(currentrow, 1).Text = myfname Then
          ws.Cells(currentrow, 68).Value = Me.Reg10.Value
          ws.Cells(currentrow, 69).Value = Me.Reg11.Value
          ws.Cells(currentrow, 10).Value = Me.Reg5.Value
          ws.Cells(currentrow, 9).Value = Me.Reg6.Value
          ws.Cells(currentrow, 70).Value = Me.Reg7.Value
    End If
    Next
    
    If MsgBox("Information has" & vbNewLine & "been updated") = vbOK Then
          Unload Me
          Sheet1.Select
    End If
    End Sub
    

    【讨论】:

    • @A.Celchko 但是我确实改变了: Set ws = Sheet11 lastrow = Sheet11.Range("A1" & Rows.Count).End(xlUp).row....为了指定工作表。
    猜你喜欢
    • 1970-01-01
    • 2014-11-24
    • 2012-07-26
    • 1970-01-01
    • 1970-01-01
    • 2017-11-08
    • 2019-04-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多