Macro works on first go around, then doesnt work correctly until excel is closed and openned again....only to work once.

Miler13

New Member
Joined
Feb 5, 2017
Messages
43
So I am trying to create a button that opens a userform thatasks questions on what the user wants to do. It is kind of hard to explain so Iam going to try to insert screen shots of what I want to happen.
Below is part of a table that is created on the main pageentitled “Status Sheet.”
[TABLE="width: 297"]
<tbody>[TR]
[TD="width: 44"]
216
[/TD]
[TD="width: 139"]miller
[/TD]
[TD="width: 53"]
2
[/TD]
[TD="width: 33"]
2
[/TD]
[TD="width: 36"]2
[/TD]
[TD="width: 45"]
2
[/TD]
[TD="width: 45"]
2
[/TD]
[/TR]
[TR]
[TD="width: 44"]
217
[/TD]
[TD="width: 139"]Hardy
[/TD]
[TD="width: 53"]
1
[/TD]
[TD="width: 33"]
1
[/TD]
[TD="width: 36"]1
[/TD]
[TD="width: 45"]
1
[/TD]
[TD="width: 45"]
1
[/TD]
[/TR]
</tbody>[/TABLE]
I want to be able to move “miller” and replace “hardy” thenbe asked where “hardy” should then be placed when “miller” is placed into “hardys”room number.
First userform that appears
**one box with title above that asks which person will bemoving**
I want to put “miller” into “hardy” room, and then be askedwhere to put “hardy.”
Second screen shot
**Left side box, has persons name that was entered previous.On the right hand side has a box to enter which number to move said person to**
Asking me which room I want to put “miller” into.
I enter the room, 217, which is currently housed to “hardy.”The macro enters “miller’s” information into hardys room, then copies hardysinformation and asks where to put his.
**Same as above, but has persons name who was in theprevious room, on right were to put said person****
When I then enter a room number for hardy’s it pastes it.

The first time around, everything works perfect, but thesecond time around it doesn’t.

***What happens?***
When I enter the name of the person the second time tryingto work the button, on the second screen, the name of the person who I entereddoes not show up. I enter in the room number in the area, then it just ends. IfI close excel and reopen it, it runs perfect the first time, but does the sameagain. Below is my code for each userform.
Userform2 (first screen that pops up)
Code:
[FONT=Calibri][SIZE=3][COLOR=#000000]Private Sub CommandButton1_Click()[/COLOR][/SIZE][/FONT]


[FONT=Calibri][SIZE=3][COLOR=#000000]If (Me.TextBox1.Value) = "" Then[/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000]MsgBox "is empty"[/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000]Me.Hide[/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000]Else: MsgBox "copy"[/COLOR][/SIZE][/FONT]

[FONT=Calibri][SIZE=3][COLOR=#000000]Cells.find(What:=Me.TextBox1.Value, After:=ActiveCell, LookIn:=xlValues,LookAt:= _[/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000]xlWhole,SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _[/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000],SearchFormat:=False).Activate[/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000]Range(ActiveCell,ActiveCell.Offset(0, 6)).Copy[/COLOR][/SIZE][/FONT]

[FONT=Calibri][SIZE=3][COLOR=#000000]Sheets("Sheet1").Select[/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000]Range("A1:F1").Insert[/COLOR][/SIZE][/FONT]

[FONT=Calibri][SIZE=3][COLOR=#000000]Sheets("Status Sheet").Activate[/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000]Cells.find(What:=Me.TextBox1.Value, After:=ActiveCell,LookIn:=xlValues, LookAt:= _[/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000]xlWhole,SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _[/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000],SearchFormat:=False).Activate[/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000]Range(ActiveCell,ActiveCell.Offset(0, 6)).ClearContents[/COLOR][/SIZE][/FONT]

[FONT=Calibri][SIZE=3][COLOR=#000000]UserForm1.Show[/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000]Me.Hide[/COLOR][/SIZE][/FONT]

[FONT=Calibri][SIZE=3][COLOR=#000000]End If[/COLOR][/SIZE][/FONT]


[FONT=Calibri][SIZE=3][COLOR=#000000]Me.TextBox1.Value = ""[/COLOR][/SIZE][/FONT]


[FONT=Calibri][SIZE=3][COLOR=#000000]End Sub[/COLOR][/SIZE][/FONT]

Userform1, which is second screen
Code:
[FONT=Calibri][SIZE=3][COLOR=#000000]Private Sub UserForm_Initialize()[/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000]UserForm1.TextBox2.Value = CStr(UserForm2.TextBox1.Value)[/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000]End Sub[/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000]Private Sub CommandButton1_Click()[/COLOR][/SIZE][/FONT]


[FONT=Calibri][SIZE=3][COLOR=#000000]If (Me.TextBox2.Value) = "" Then[/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000]MsgBox "isempty"[/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000]Me.Hide[/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000]Else: MsgBox "run"[/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000]Sheets("StatusSheet").Activate[/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000]ActiveSheet.Cells.find(What:=Me.TextBox3.Value, After:=ActiveCell,LookIn:=xlValues, LookAt:= _[/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000]xlWhole,SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _[/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000],SearchFormat:=False).Activate[/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000]IfActiveCell.Offset(0, 1).Value = "" Then[/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000]MsgBox"will paste"[/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000]Sheets("Sheet1").Activate[/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000]ActiveSheet.Cells.find(Me.TextBox2.Value, ActiveCell, xlValues, xlWhole,xlByRows, xlNext, False, False).Activate[/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000]Range(ActiveCell, ActiveCell.Offset(0, 5)).Copy[/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000]Sheets("Status Sheet").Activate[/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000]Cells.find(Me.TextBox3.Value, ActiveCell, xlValues, xlWhole, xlByRows,xlNext, False, False).Activate[/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000]ActiveCell.Offset(0, 1).PasteSpecial[/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000]Sheets("Sheet1").Activate[/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000]ActiveSheet.Cells.find(Me.TextBox2.Value,ActiveCell, xlValues, xlWhole, xlByRows, xlNext, False, False).Activate[/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000]ActiveCell.EntireRow.Delete[/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000]Sheets("Status Sheet").Activate[/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000]Me.TextBox2.Value = ""[/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000]Me.TextBox3.Value = ""[/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000]Me.Hide[/COLOR][/SIZE][/FONT]

[FONT=Calibri][SIZE=3][COLOR=#000000]Else: MsgBox"space full"[/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000]Range(ActiveCell.Offset(0, 1), ActiveCell.Offset(0, 6)).Copy[/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000]Sheets("Sheet1").Select[/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000]Range("A1:F1").Insert[/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000]Sheets("Status Sheet").Activate[/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000]ActiveSheet.Cells.find(What:=Me.TextBox3.Value, After:=ActiveCell,LookIn:=xlValues, LookAt:= _[/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000]xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext,MatchCase:=False _[/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000],SearchFormat:=False).Activate[/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000]Range(ActiveCell.Offset(0, 1), ActiveCell.Offset(0, 6)).ClearContents[/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000]Sheets("Sheet1").Activate[/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000]ActiveSheet.Cells.find(Me.TextBox2.Value, ActiveCell, xlValues, xlWhole,xlByRows, xlNext, False, False).Activate[/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000]Range(ActiveCell, ActiveCell.Offset(0, 5)).Copy[/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000]Sheets("Status Sheet").Activate[/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000]ActiveSheet.Cells.find(Me.TextBox3.Value, ActiveCell, xlValues, xlWhole,xlByRows, xlNext, False, False).Activate[/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000]ActiveCell.Offset(0, 1).PasteSpecial[/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000]Sheets("Sheet1").Activate[/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000]ActiveSheet.Cells.find(Me.TextBox2.Value, ActiveCell, xlValues, xlWhole,xlByRows, xlNext, False, False).Activate[/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000]ActiveCell.EntireRow.Delete[/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000]Sheets("Status Sheet").Activate[/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000]UserForm3.Show[/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000]Me.TextBox2.Value = ""[/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000]Me.TextBox3.Value = ""[/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000]Me.Hide[/COLOR][/SIZE][/FONT]

[FONT=Calibri][SIZE=3][COLOR=#000000]End If[/COLOR][/SIZE][/FONT]




[FONT=Calibri][SIZE=3][COLOR=#000000]End If[/COLOR][/SIZE][/FONT]




[FONT=Calibri][SIZE=3][COLOR=#000000]End Sub[/COLOR][/SIZE][/FONT]

The final userform3
Code:
[FONT=Calibri][SIZE=3][COLOR=#000000]Private Sub UserForm_Initialize()[/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000]UserForm3.TextBox1.Value =CStr(ThisWorkbook.Sheets("Sheet1").Range("A1").Value)[/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000]End Sub[/COLOR][/SIZE][/FONT]

[FONT=Calibri][SIZE=3][COLOR=#000000]Private Sub CommandButton1_Click()[/COLOR][/SIZE][/FONT]


[FONT=Calibri][SIZE=3][COLOR=#000000]Sheets("Sheet1").Activate[/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000]Cells.find(Range("A1"), ActiveCell, xlValues, xlWhole,xlByRows, xlNext, False, False).Activate[/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000]Range(ActiveCell, ActiveCell.Offset(0, 5)).Copy[/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000]Sheets("Status Sheet").Activate[/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000]Cells.find(Me.TextBox2.Value, ActiveCell, xlValues, xlWhole, xlByRows,xlNext, False, False).Activate[/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000]ActiveCell.Offset(0,1).PasteSpecial[/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000]Sheets("Sheet1").Activate[/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000]Cells.find(Range("A1"), ActiveCell, xlValues, xlWhole,xlByRows, xlNext, False, False).Activate[/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000]ActiveCell.EntireRow.Delete[/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000]Sheets("Status Sheet").Activate[/COLOR][/SIZE][/FONT]

[FONT=Calibri][SIZE=3][COLOR=#000000]Me.TextBox1.Value = ""[/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000]Me.TextBox2.Value = ""[/COLOR][/SIZE][/FONT]


[FONT=Calibri][SIZE=3][COLOR=#000000]Me.Hide[/COLOR][/SIZE][/FONT]

[FONT=Calibri][SIZE=3][COLOR=#000000]End Sub[/COLOR][/SIZE][/FONT]


I really hope this makes sense. Please help.
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
i would start by renaming your buttons
Private Sub CommandButton1_Click() with three different uses might cause problems
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,182
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top