下面是代码的摘录,其中出现“错误(更像是不必要的事件)”。单击命令按钮后,我的代码在userform(而不是userform1)中运行。初始表单显示在工作簿上。第二个用户窗体 - 下面代码中的UserForm1 - 是一个复选框,我想在我的IF条件下呈现给用户。当程序到达时,它会打开UserForm1,但也会打开一个空白的灰色Excel工作簿。在关闭Userformm1界面之前,无法关闭工作簿。
注意:在关闭用户窗体之后,第二个msgbox才会显示。 我尝试搜索此问题,但无法找到有关UserForm的问题。提前感谢您的帮助。
Private Sub CommandButton1_Click()
Dim wbName As String, wb As Workbook, ws As Worksheet
Dim sht As Worksheet
Dim myValue As Variant
Dim counter As Integer
Dim EstNumAttempt As Boolean
EstNumAttemp = False
counter = 0
Dim ThisNum As String
Dim EstNum As String
Dim CopyFromBook As Workbook
Dim CopyToWbk As Workbook
Dim ShToCopy As Worksheet
Set CopyToWbk = ThisWorkbook
Application.ScreenUpdating = False
ThisUserPath = Application.ActiveWorkbook.Path
userDataPath = ThisUserPath & "\SBNBidDataSet01112018.xlsx"
Dim StartDate As String
Dim EndDate As String
StartDate = TextBox1.Value
EndDate = TextBox2.Value
EstNum = TextBox3.Value
Set wb = Workbooks.Open(userDataPath)
Set CopyFromWbk = wb
WSCount = wb.Worksheets.Count
If EstNum <> "" Then
EstNumAttempt = True
For X = 1 To WSCount
Set ws = wb.Sheets(X)
ThisNum = ws.Cells(2, 2)
If ThisNum = EstNum Then
counter = counter + 1
Set ShToCopy = CopyFromWbk.Worksheets(X)
ShToCopy.Copy After:=CopyToWbk.Sheets(CopyToWbk.Sheets.Count)
End If
Next X
If counter = 0 Then
MsgBox " That's probably not a valid EST# in SmartBidNet"
End If
End If
If IsDate(StartDate) = True And IsDate(EndDate) = True And EstNumAttempt = False Then
MsgBox "h"
wb.Application.Visible = False
UserForm1.Show
wb.Application.Visible = True
MsgBox "h"
'BLANK GREY SHEET HERE
For X = 100 To 101
Set ws = wb.Sheets(X)
lastrow = ws.Cells(Rows.Count, 1).End(xlUp).Row
'future code
Next X
End If
wb.Close
Application.ScreenUpdating = False ' from before also!
End Sub
答案 0 :(得分:0)
这不是解决方案,我测试了一段代码,以下是我的反馈。
我在名为testbook.xslm的工作簿中运行了这个代码,并创建了一个表单(myForm只是一个灰色的布局,没有任何内容):
Private Sub CommandButton1_Click()
Dim wb As Workbook
Dim anotherbook As Workbook
Dim anotherbook2 As Workbook
Dim anotherbook3 As Workbook
Set wb = Workbooks.Add
Set anotherbook = wb
Set anotherbook2 = wb
Set anotherbook3 = wb
MsgBox "h"
wb.Application.Visible = False
myForm.Show
wb.Application.Visible = True
MsgBox "h"
wb.Close
Application.ScreenUpdating = False ' from before also!
End Sub
从用户的角度来看:
1)测试书打开
2)点击按钮 - &gt;书#在testbook上打开
3)msgbox“h”弹出
4)x out of msgbox“h” - &gt; 两个工作簿都消失了(testbook和book1)
5)我的灰色用户表单弹出(空)
6)x out of userform
7)测试本再次显示在其背后创建的新书
8)msgbox“h”#2弹出
9)x out of msgbox
10)测试书背后的书#消失了
就是这样。没有灰盒残留物。你是如何关闭用户形式的(第一个和第二个)?很明显我用x终止了。我没有添加任何代码。那么如何在没有代码的情况下关闭表单?
如果我对wb进行多个工作簿引用,则会发生添加相同的行为。 所以这个代码区域不是你的问题。 。 。 。
第二次测试,仍然没有问题,添加工作表,运行多个工作表,将它们复制到测试手册。
Option Explicit
Private Sub CommandButton1_Click()
Dim wb As Workbook
Dim wb2 As Workbook
Dim ws As Worksheet
Dim anotherbook As Workbook
Dim anotherbook2 As Workbook
Dim anotherbook3 As Workbook
Dim X As Double
Dim lastrow As Double
Dim thisNum As String
Set wb = Workbooks.Add
Set anotherbook = wb
Set anotherbook2 = wb
Set anotherbook3 = wb
Set wb2 = ThisWorkbook
wb.Worksheets.Add
wb.Worksheets.Add
wb.Worksheets.Add
wb.Worksheets.Add
wb.Worksheets.Add
wb.Worksheets.Add
myForm2.Show
MsgBox "h"
wb.Application.Visible = False
myForm.Show
wb.Application.Visible = True
MsgBox "h"
For X = 1 To wb.Worksheets.Count
Set ws = wb.Sheets(X)
thisNum = ws.Cells(2, 2)
lastrow = ws.Cells(Rows.Count, 1).End(xlUp).Row
ws.Copy After:=wb2.Worksheets(wb2.Worksheets.Count)
'future code
Next X
wb.Close
Application.ScreenUpdating = False ' from before also!
End Sub
也许你有很多数据被采取行动。粘贴后清除剪贴板?用:
Application.CutCopyMode = False
挖掘 - WWC