当其他macros运行并closures一个用户窗体时,macros给第二次错误91

我有两个macros。 一个macros用于通过最小化色带和放大function来重新调整不同显示器的Excel表格大小。 另一个macros,我可以写在一个特定的Excel表格使用从用户的文字input一些问题。 该macros激活用户可以input数据的用户表单。 第一个macros按预期在所有的Excel表格中完美运行。 如果我一次运行第二个macros,并试图再次运行第一个macros。 在“MaxCol = Rows(1)….”行中的第一个macros总是给出对象variables未设置错误 我必须closures并重新打开它才能再次运行第一个macros。

我不知道为什么会出现这种情况。 如果用户按下ok或取消,我已经正确closures(Unloaded)Userform的对象。

下面是第一个macros的代码。

Sub OptimalSize() Dim SheetName As String Dim MaxCol As Long SheetName = "" MaxCol = 0 CommandBars.ExecuteMso "MinimizeRibbon" Application.WindowState = xlMaximized SheetName = ActiveSheet.Name MaxCol = Rows(1).Find(1, SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:=False).Column Sheets(SheetName).Select ActiveSheet.Range("A1", Cells(1, MaxCol)).Select ActiveWindow.Zoom = True Application.Goto Reference:=Worksheets(SheetName).Range("A1"), Scroll:=True End Sub 

经过一些调用userform的计算之后的第二个macros

 Sub ProblemBoard() ' code here to find out empty space in a table!!' Userform1.Show End sub 

下面的用户表单代码

 Sub CommandButton1_Click() Application.ScreenUpdating = False Worksheets("Problem Board").Cells(z, 2).Value = TextBox1.Text Worksheets("Problem Board").Cells(z, 5).Value = TextBox2.Text Unload Me Sheets(ActWS).Select Application.ScreenUpdating = True End Sub Private Sub CommandButton2_Click() Unload Me Sheets(ActWS).Select Application.ScreenUpdating = True End Sub Private Sub UserForm_Initialize() Dim lng As Long 'ActWS = ActiveSheet.Name lng = Sheets("Listen").Range("A65536").End(xlUp).Row Me.ComboBox2.List = Sheets("Listen").Range("A2:A" & lng ).Value TextBox1 = "" TextBox2 = "" End Sub