使用 Userform 将 excel 数据更新到不同的工作表
Updating excel data with Userform to different worksheet
我创建了一个用户表单,用于将新数据输入到工作表中。表单通过工作表顶部的 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。
提前致谢!
试试下面的代码。鉴于该按钮将分配给您要更新的 sheet,您可以创建一个变量 WS 并将其分配给当前活动的 sheet(设置 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
我创建了一个用户表单,用于将新数据输入到工作表中。表单通过工作表顶部的 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。 提前致谢!
试试下面的代码。鉴于该按钮将分配给您要更新的 sheet,您可以创建一个变量 WS 并将其分配给当前活动的 sheet(设置 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