如果现有单元格中已有内容,如何创建将复制userbox的代码

时间:2018-09-11 10:29:47

标签: excel vba userform

所以我有一个正在处理的文档是一份状态表。我想要做的是有一个按钮,该按钮可以将人们从以前的房间移到新的房间。 现在,我已经设置好了,可以将一个房间中的一个人复制到另一个页面,然后只要其他人不在该新房间中,就可以将其粘贴到新房间。如果有人在新房间里,它将被覆盖。我不希望它那样做。我希望是有人在新房间里,它将复制他们的信息并将其粘贴到旧表中,然后拉出另一个用户表单,询问将那个人放在新房间里的位置。如果那有道理 附件是我的代码。

Private Sub CommandButton1_Click()
If Trim(TextBox1.Value) = "" Then
MsgBox "error"
Exit Sub
Else
'old room number
Me.TextBox1.Value = Me.TextBox1.Value
''find first person
Cells.find(What:=Me.TextBox1.Value, After:=ActiveCell, LookIn:=xlValues, LookAt:= _
    xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
    , SearchFormat:=False).Activate
Range(ActiveCell.Offset(0, 1), ActiveCell.Offset(0, 6)).Copy
''find location to paste in sheet1
Sheets("Sheet1").Select
Range("A1:F1").Insert
'clear first person


Sheets("Status Sheet").Activate
Cells.find(What:=Me.TextBox1.Value, After:=ActiveCell, LookIn:=xlValues, LookAt:= _
    xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
    , SearchFormat:=False).Activate
    Range(ActiveCell.Offset(0, 1), ActiveCell.Offset(0, 6)).ClearContents
UserForm1.Show
Me.TextBox1.Value = ""

Me.Hide
End If
End Sub

打开的第二个用户窗体的第二个代码

Private Sub CommandButton1_Click()

If Trim(TextBox2.Value) = "" Then
MsgBox "error"
Exit Sub
Else
'old room number
Me.TextBox2.Value = Me.TextBox2.Value
Me.TextBox3.Value = Me.TextBox3.Value
''find person
Sheets("Sheet1").Activate
Cells.find(What:=Me.TextBox3.Value, After:=ActiveCell, LookIn:=xlValues, LookAt:= _xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _,SearchFormat:=False).Activate
Range(ActiveCell.Offset(0, -1), ActiveCell.Offset(0, 4)).Copy

Sheets("Status Sheet").Activate
Cells.find(What:=Me.TextBox2.Value, After:=ActiveCell, LookIn:=xlValues, LookAt:= _
    xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
    , SearchFormat:=False).Activate
ActiveCell.Offset(, 1).PasteSpecial

Sheets("Sheet1").Activate
Cells.find(What:=Me.TextBox3.Value, After:=ActiveCell, LookIn:=xlValues, LookAt:= _
    xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
    , SearchFormat:=False).Activate
Range(ActiveCell.Offset(0, -1), ActiveCell.Offset(0, 4)).EntireRow.Delete

Sheets("Status Sheet").Activate


Me.TextBox2.Value = ""
Me.TextBox3.Value = ""


Me.Hide

End If


End Sub

谢谢!!!!

0 个答案:

没有答案