简体   繁体   English

VBA将数据从一张纸传输到另一张纸

[英]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

Note: When CommandButton11 is clicked (above - userform4), it opens below userform5. 注意:单击CommandButton11(在userform4上方)时,它将在userform5下方打开。 Userform5 textbox1 and textbox2 then retrieves data from sheet1 as shown in userform4 above. 然后,如上面userform4所示,Userform5 textbox1和textbox2从sheet1检索数据。

 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

When Userform5 Commandbutton3 is clicked, I want the rows selected in sheet1 (ie based on the value from textbox1 which is pulled from sheet1) to be cut and pasted into sheet2 单击Userform5 Commandbutton3时,我希望将sheet1中选择的行(即基于从box1中拉出的textbox1中的值)剪切并粘贴到sheet2中

Hello All, 大家好,

Hoping someone could help me out on this one. 希望有人可以帮助我解决这一问题。 As the code shows above, I can get the values in the userform textbox to be added to sheet 2. The userform retrieves some of the records from sheet 1. What I am trying to achieve is when that value from sheet 1 is retrieved, I want to cut it from sheet 1 into sheet 2. What is the best way to do this? 如上面的代码所示,我可以在userform文本框中获取要添加到工作表2的值。该用户窗体从工作表1中检索一些记录。我试图实现的是从工作表1中检索该值时,我想要将其从工作表1切成工作表2。什么是最好的方法?

Thank you 谢谢

Regards, Kevin 问候,凯文

It isn't the only way, but try an array. 这不是唯一的方法,但是尝试一个数组。 For more info see VBA Arrays And Worksheet Ranges . 有关更多信息,请参见VBA数组和工作表范围

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.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM