VBA 将数据从一个 sheet 传输到另一个
VBA Transfer of data from one sheet to another
Private Sub CommandButton11_Click()
Load UserForm5
If TextBox1.Value = "" Or ComboBox1.Value = "" Then
MsgBox "Incomplete Data", vbCritical, ""
Exit Sub
End If
UserForm5.TextBox1 = UserForm4.TextBox1.Value
UserForm5.TextBox2 = UserForm4.ComboBox1.Value
UserForm5.Show
End Sub
注意:单击 CommandButton11(上方 - userform4)时,它会在 userform5 下方打开。 Userform5 textbox1 和 textbox2 然后从 sheet1 检索数据,如上面的 userform4 所示。
Private Sub CommandButton3_Click()
Dim emptyRow As Long
'Make Sheet 2 active
Sheets("Sheet2").Activate
'Determine emptyRow
emptyRow = WorksheetFunction.CountA(Range("A:G")) + 1
'Transfer information from userform fields to Sheet 2
Cells(emptyRow, 2).Value = TextBox1.Value
Cells(emptyRow, 3).Value = TextBox2.Value
Cells(emptyRow, 5).Value = TextBox3.Value
Cells(emptyRow, 7).Value = TextBox4.Value
Cells(emptyRow, 8).Value = TextBox5.Value
End Sub
单击 Userform5 Commandbutton3 时,我希望在 sheet1 中选择的行(即基于从 sheet1 中提取的 textbox1 的值)被剪切并粘贴到 sheet2
大家好,
希望有人能帮我解决这个问题。如上代码所示,我可以获取要添加到 sheet 的用户窗体文本框中的值 2. 用户窗体从 sheet 检索一些记录 1. 我想要实现的是当检索到 sheet 1 的值,我想将其从 sheet 1 剪切成 sheet 2。最好的方法是什么?
谢谢
此致,
凯文
这不是唯一的方法,但可以尝试数组。有关详细信息,请参阅 VBA Arrays And Worksheet Ranges。
Dim rngOldRow, rngNewRow, arrValuesInRow
Set rngOldRow = Worksheets(1).Range("A10:N10") 'Defines the old row.
Set rngNewRow = Worksheets(2).Range("A5:N5") 'Defines the new row.
arrValuesInRow = rngOldRow 'Copies values to an array variable.
arrValuesInRow(1, 2) = TextBox1.Value 'Changes values in the array.
arrValuesInRow(1, 3) = TextBox2.Value
arrValuesInRow(1, 5) = TextBox3.Value
arrValuesInRow(1, 7) = TextBox4.Value
arrValuesInRow(1, 8) = TextBox5.Value
rngNewRow.Value = arrValuesInRow 'Copies from the array to Sheet 2.
rngOldRow.Delete Shift:=xlUp 'Deletes the row on Sheet 1.
Private Sub CommandButton11_Click()
Load UserForm5
If TextBox1.Value = "" Or ComboBox1.Value = "" Then
MsgBox "Incomplete Data", vbCritical, ""
Exit Sub
End If
UserForm5.TextBox1 = UserForm4.TextBox1.Value
UserForm5.TextBox2 = UserForm4.ComboBox1.Value
UserForm5.Show
End Sub
注意:单击 CommandButton11(上方 - userform4)时,它会在 userform5 下方打开。 Userform5 textbox1 和 textbox2 然后从 sheet1 检索数据,如上面的 userform4 所示。
Private Sub CommandButton3_Click()
Dim emptyRow As Long
'Make Sheet 2 active
Sheets("Sheet2").Activate
'Determine emptyRow
emptyRow = WorksheetFunction.CountA(Range("A:G")) + 1
'Transfer information from userform fields to Sheet 2
Cells(emptyRow, 2).Value = TextBox1.Value
Cells(emptyRow, 3).Value = TextBox2.Value
Cells(emptyRow, 5).Value = TextBox3.Value
Cells(emptyRow, 7).Value = TextBox4.Value
Cells(emptyRow, 8).Value = TextBox5.Value
End Sub
单击 Userform5 Commandbutton3 时,我希望在 sheet1 中选择的行(即基于从 sheet1 中提取的 textbox1 的值)被剪切并粘贴到 sheet2
大家好,
希望有人能帮我解决这个问题。如上代码所示,我可以获取要添加到 sheet 的用户窗体文本框中的值 2. 用户窗体从 sheet 检索一些记录 1. 我想要实现的是当检索到 sheet 1 的值,我想将其从 sheet 1 剪切成 sheet 2。最好的方法是什么?
谢谢
此致, 凯文
这不是唯一的方法,但可以尝试数组。有关详细信息,请参阅 VBA Arrays And Worksheet Ranges。
Dim rngOldRow, rngNewRow, arrValuesInRow
Set rngOldRow = Worksheets(1).Range("A10:N10") 'Defines the old row.
Set rngNewRow = Worksheets(2).Range("A5:N5") 'Defines the new row.
arrValuesInRow = rngOldRow 'Copies values to an array variable.
arrValuesInRow(1, 2) = TextBox1.Value 'Changes values in the array.
arrValuesInRow(1, 3) = TextBox2.Value
arrValuesInRow(1, 5) = TextBox3.Value
arrValuesInRow(1, 7) = TextBox4.Value
arrValuesInRow(1, 8) = TextBox5.Value
rngNewRow.Value = arrValuesInRow 'Copies from the array to Sheet 2.
rngOldRow.Delete Shift:=xlUp 'Deletes the row on Sheet 1.