我有一个useform,它将在打开时打开另一个工作簿,并在关闭时关闭工作簿。第一次可以很好地运行,但是当我再次打开用户窗体时,它会弹出运行时错误。我可以使用一些有关此问题的帮助,但我无法确定问题所在。下面是斜体的错误行:
Sub Auto_Open()
Application.OnKey "{F3}", "ShowForm"
End Sub
Sub ShowForm()
*Auto_Fill_Form.Show*
End Sub
我用来退出用户表单的按钮如下所示:
Private Sub CommandButton4_Click()
Workbooks("WorkOrderDatabase.xlsm").Close SaveChanges:=True
Unload Me
End Sub
用户代码:
Private Sub CommandButton2_Click()
Call UserForm_Initialize
End Sub
Private Sub CommandButton4_Click()
'ComboBox1.Value = ""
Workbooks("WorkOrderDatabase.xlsm").Close SaveChanges:=True
Unload Me
End Sub
Private Sub CommandButton5_Click()
Me.Width = 480
Me.Height = 520
Auto_Fill_Form.Label26.Visible = True
Auto_Fill_Form.TextBox15.Visible = True
Auto_Fill_Form.CommandButton7.Visible = True
Auto_Fill_Form.Label22.Visible = False
Auto_Fill_Form.TextBox16.Visible = False
Auto_Fill_Form.Label23.Visible = False
Auto_Fill_Form.TextBox17.Visible = False
Auto_Fill_Form.Label24.Visible = False
Auto_Fill_Form.TextBox18.Visible = False
Auto_Fill_Form.Label25.Visible = False
Auto_Fill_Form.TextBox19.Visible = False
Auto_Fill_Form.CommandButton8.Visible = False
TextBox15 = Translate(TextBox10.Value, "en", "es", True)
SortAsc2
End Sub
Private Sub CommandButton6_Click()
Me.Width = 480
Me.Height = 520
Auto_Fill_Form.Label22.Visible = True
Auto_Fill_Form.TextBox16.Visible = True
Auto_Fill_Form.Label23.Visible = True
Auto_Fill_Form.TextBox17.Visible = True
Auto_Fill_Form.Label24.Visible = True
Auto_Fill_Form.TextBox18.Visible = True
Auto_Fill_Form.Label25.Visible = True
Auto_Fill_Form.TextBox19.Visible = True
Auto_Fill_Form.CommandButton8.Visible = True
Auto_Fill_Form.Label26.Visible = False
Auto_Fill_Form.TextBox15.Visible = False
Auto_Fill_Form.CommandButton7.Visible = False
TextBox16 = Translate(TextBox11.Value, "en", "es", True)
TextBox17 = Translate(TextBox12.Value, "en", "es", True)
TextBox18 = Translate(TextBox13.Value, "en", "es", True)
TextBox19 = Translate(TextBox14.Value, "en", "es", True)
End Sub
Private Sub CommandButton7_Click()
Dim rng As Range
Set rng = Locations.Worksheets(Page).ListObjects("Table15").Range
Dim LastRow As Long
LastRow = rng.Find(What:="*", _
After:=rng.Cells(1), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
rng.Parent.Cells(LastRow + 1, 1).Value = TextBox10.Value
rng.Parent.Cells(LastRow + 1, 2).Value = TextBox15.Value
SortAsc2
End Sub
Private Sub CommandButton8_Click()
Dim rng As Range
Set rng = Locations.Worksheets(Page).ListObjects("Table24").Range
Dim LastRow As Long
LastRow = rng.Find(What:="*", _
After:=rng.Cells(1), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
rng.Parent.Cells(LastRow + 1, 4).Value = TextBox11.Value
rng.Parent.Cells(LastRow + 1, 5).Value = TextBox12.Value
rng.Parent.Cells(LastRow + 1, 6).Value = TextBox13.Value
rng.Parent.Cells(LastRow + 1, 7).Value = TextBox14.Value
rng.Parent.Cells(LastRow + 1, 8).Value = TextBox16.Value
rng.Parent.Cells(LastRow + 1, 9).Value = TextBox17.Value
rng.Parent.Cells(LastRow + 1, 10).Value = TextBox18.Value
rng.Parent.Cells(LastRow + 1, 11).Value = TextBox19.Value
SortAsc2
End Sub
Private Sub OkButton_Click()
ThisWorkbook.Sheets("WO_Cover").Cells(1, 9).Value = TextBox1.Value
ThisWorkbook.Sheets("First Piece Inspection").Cells(5, 7).Value = TextBox1.Value
ThisWorkbook.Sheets("Production Inspection").Cells(5, 7).Value = TextBox1.Value
ThisWorkbook.Sheets("WO_Cover").Cells(3, 2).Value = TextBox2.Value
ThisWorkbook.Sheets("WO_Cover").Cells(3, 8).Value = TextBox3.Value
ThisWorkbook.Sheets("WO_Cover").Cells(4, 8).Value = TextBox4.Value
ThisWorkbook.Sheets("WO_Cover").Cells(4, 5).Value = TextBox5.Value
ThisWorkbook.Sheets("WO_Cover").Cells(5, 2).Value = ComboBox1.Value
ThisWorkbook.Sheets("WO_Cover").Cells(10, 1).Value = TextBox7.Value
ThisWorkbook.Sheets("WO_Cover").Cells(10, 3).Value = TextBox8.Value
ThisWorkbook.Sheets("WO_Cover").Cells(20, 3).Value = TextBox9.Value
ThisWorkbook.Sheets("WO_Cover").Cells(21, 2).Value = ComboBox2.Value
ThisWorkbook.Sheets("WO_Cover").Cells(24, 2).Value = ComboBox3.Value
ThisWorkbook.Sheets("WO_Cover").Cells(27, 2).Value = ComboBox4.Value
ThisWorkbook.Sheets("WO_Cover").Cells(30, 2).Value = ComboBox5.Value
ThisWorkbook.Sheets("WO_Cover").Cells(33, 2).Value = ComboBox6.Value
ThisWorkbook.Sheets("WO_Cover").Cells(36, 2).Value = ComboBox7.Value
End Sub
Private Sub OptionButton1_Click()
If OptionButton1.Value Then
Me.Width = 375
Me.Height = 520
Auto_Fill_Form.Label17.Visible = True
Auto_Fill_Form.TextBox10.Visible = True
Auto_Fill_Form.CommandButton5.Visible = True
Auto_Fill_Form.Label18.Visible = False
Auto_Fill_Form.TextBox11.Visible = False
Auto_Fill_Form.Label19.Visible = False
Auto_Fill_Form.TextBox12.Visible = False
Auto_Fill_Form.Label20.Visible = False
Auto_Fill_Form.TextBox13.Visible = False
Auto_Fill_Form.Label21.Visible = False
Auto_Fill_Form.TextBox14.Visible = False
Auto_Fill_Form.CommandButton6.Visible = False
Else
Me.Width = 250
Me.Height = 520
Auto_Fill_Form.Label17.Visible = False
Auto_Fill_Form.TextBox10.Visible = False
Auto_Fill_Form.CommandButton5.Visible = False
End If
End Sub
Private Sub OptionButton2_Click()
If OptionButton2.Value Then
Me.Width = 375
Me.Height = 520
Auto_Fill_Form.Label18.Visible = True
Auto_Fill_Form.TextBox11.Visible = True
Auto_Fill_Form.Label19.Visible = True
Auto_Fill_Form.TextBox12.Visible = True
Auto_Fill_Form.Label20.Visible = True
Auto_Fill_Form.TextBox13.Visible = True
Auto_Fill_Form.Label21.Visible = True
Auto_Fill_Form.TextBox14.Visible = True
Auto_Fill_Form.CommandButton6.Visible = True
Auto_Fill_Form.Label17.Visible = False
Auto_Fill_Form.TextBox10.Visible = False
Auto_Fill_Form.CommandButton5.Visible = False
Else
Me.Width = 250
Me.Height = 520
Auto_Fill_Form.Label18.Visible = False
Auto_Fill_Form.TextBox11.Visible = False
Auto_Fill_Form.Label19.Visible = False
Auto_Fill_Form.TextBox12.Visible = False
Auto_Fill_Form.Label20.Visible = False
Auto_Fill_Form.TextBox13.Visible = False
Auto_Fill_Form.Label21.Visible = False
Auto_Fill_Form.TextBox14.Visible = False
Auto_Fill_Form.CommandButton6.Visible = False
End If
End Sub
Private Sub UserForm_Activate()
TextBox1.Value = ThisWorkbook.Sheets("WO_Cover").Cells(1, 9).Value
TextBox2.Value = ThisWorkbook.Sheets("WO_Cover").Cells(3, 2).Value
TextBox3.Value = ThisWorkbook.Sheets("WO_Cover").Cells(3, 8).Value
TextBox4.Value = ThisWorkbook.Sheets("WO_Cover").Cells(4, 8).Value
'TextBox5.Value = ThisWorkbook.Sheets("WO_Cover").Cells(4, 5).Value
ComboBox1.Value = ThisWorkbook.Sheets("WO_Cover").Cells(5, 2).Value
TextBox7.Value = ThisWorkbook.Sheets("WO_Cover").Cells(10, 1).Value
TextBox8.Value = ThisWorkbook.Sheets("WO_Cover").Cells(10, 3).Value
TextBox9.Value = ThisWorkbook.Sheets("WO_Cover").Cells(20, 3).Value
ComboBox2.Value = ThisWorkbook.Sheets("WO_Cover").Cells(21, 2).Value
ComboBox3.Value = ThisWorkbook.Sheets("WO_Cover").Cells(24, 2).Value
ComboBox4.Value = ThisWorkbook.Sheets("WO_Cover").Cells(27, 2).Value
ComboBox5.Value = ThisWorkbook.Sheets("WO_Cover").Cells(30, 2).Value
ComboBox6.Value = ThisWorkbook.Sheets("WO_Cover").Cells(33, 2).Value
ComboBox7.Value = ThisWorkbook.Sheets("WO_Cover").Cells(36, 2).Value
Auto_Fill_Form.Label17.Visible = False
Auto_Fill_Form.TextBox10.Visible = False
Auto_Fill_Form.Label18.Visible = False
Auto_Fill_Form.TextBox11.Visible = False
Auto_Fill_Form.Label19.Visible = False
Auto_Fill_Form.TextBox12.Visible = False
Auto_Fill_Form.Label20.Visible = False
Auto_Fill_Form.TextBox13.Visible = False
Auto_Fill_Form.Label21.Visible = False
Auto_Fill_Form.TextBox14.Visible = False
Auto_Fill_Form.CommandButton5.Visible = False
Auto_Fill_Form.CommandButton6.Visible = False
Auto_Fill_Form.Label26.Visible = False
Auto_Fill_Form.TextBox15.Visible = False
Auto_Fill_Form.CommandButton7.Visible = False
Auto_Fill_Form.Label22.Visible = False
Auto_Fill_Form.TextBox16.Visible = False
Auto_Fill_Form.Label23.Visible = False
Auto_Fill_Form.TextBox17.Visible = False
Auto_Fill_Form.Label24.Visible = False
Auto_Fill_Form.TextBox18.Visible = False
Auto_Fill_Form.Label25.Visible = False
Auto_Fill_Form.TextBox19.Visible = False
Auto_Fill_Form.CommandButton8.Visible = False
'TextBox10.Value =
End Sub
Private Sub UserForm_Initialize()
'Empty Form
TextBox1.Value = ""
TextBox2.Value = ""
TextBox3.Value = ""
TextBox4.Value = ""
ComboBox1.Value = Null
'Initialized value
TextBox5.Value = "A"
ComboBox2.Value = "N/A"
ComboBox3.Value = "N/A"
ComboBox4.Value = "N/A"
ComboBox5.Value = "N/A"
ComboBox6.Value = "N/A"
ComboBox7.Value = "N/A"
Me.ComboBox1.List = Locations.Worksheets(Page).ListObjects("Table15").ListColumns(1).DataBodyRange.Value
Me.ComboBox2.List = Locations.Worksheets(Page).ListObjects("Table24").ListColumns(1).DataBodyRange.Value
Me.ComboBox3.List = Locations.Worksheets(Page).ListObjects("Table24").ListColumns(1).DataBodyRange.Value
Me.ComboBox4.List = Locations.Worksheets(Page).ListObjects("Table24").ListColumns(1).DataBodyRange.Value
Me.ComboBox5.List = Locations.Worksheets(Page).ListObjects("Table24").ListColumns(1).DataBodyRange.Value
Me.ComboBox6.List = Locations.Worksheets(Page).ListObjects("Table24").ListColumns(1).DataBodyRange.Value
Me.ComboBox7.List = Locations.Worksheets(Page).ListObjects("Table24").ListColumns(1).DataBodyRange.Value
TextBox7.Value = ""
TextBox8.Value = ""
TextBox9.Value = ""
End Sub
模块1:
Private wLocations As Workbook
Public Const Page = "Description - Processes"
Public Property Get Locations() As Workbook
Const sPath As String = "V:\My Stuff\Templates\Work Order Batch Creation Template\WorkOrderDatabase.xlsm"
Dim sFile As String
If wLocations Is Nothing Then
'extract file name from full path
sFile = Dir(sPath)
On Error Resume Next
'check if the file is already open
Set wLocations = Workbooks(sFile)
If wLocations Is Nothing Then
Set wLocations = Workbooks.Open(sPath)
End If
On Error GoTo 0
End If
Set Locations = wLocations
End Property
模块2:
Sub Auto_Open()
Application.OnKey "{F3}", "ShowForm"
End Sub
Sub ShowForm()
Auto_Fill_Form.Show
End Sub
模块3:
Sub SortAsc2()
Locations.Worksheets(Page).Range("A2:B16").Sort Key1:=Locations.Worksheets(Page).Range("A2"), _
Order1:=xlAscending, Header:=xlGuess, OrderCustom:=1, MatchCase:=False, _
Orientation:=xlTopToBottom, DataOption1:=xlSortNormal
Locations.Worksheets(Page).Range("D2:K16").Sort Key1:=Locations.Worksheets(Page).Range("D2"), _
Order1:=xlAscending, Header:=xlGuess, OrderCustom:=1, MatchCase:=False, _
Orientation:=xlTopToBottom, DataOption1:=xlSortNormal
' Locations.Worksheets("Description - Processes").Range("A2:B16").Sort Key1:=Locations.Worksheets("Description - Processes").Range("A2"), _
' Order1:=xlDescending, Header:=xlGuess, OrderCustom:=1, MatchCase:=False, _
' Orientation:=xlTopToBottom, DataOption1:=xlSortNormal
'
' Locations.Worksheets("Description - Processes").Range("D2:K16").Sort Key1:=Locations.Worksheets("Description - Processes").Range("D2"), _
' Order1:=xlDescending, Header:=xlGuess, OrderCustom:=1, MatchCase:=False, _
' Orientation:=xlTopToBottom, DataOption1:=xlSortNormal
End Sub
模块4:
Public Function Translate(strInput As String, strFromLanguageCode As String, strToLanguageCode As String, blnTargetAlphabet As Boolean) As String
Dim strURL As String
Dim objHTTP As Object
Dim objHTML As Object
Dim objDivs As Object, objDiv
Dim strTranslatedT0 As String
Dim strTranslatedO1 As String
' send query to web page
strURL = "https://translate.google.com/m?hl=" & strFromLanguageCode & _
"&sl=" & strFromLanguageCode & _
"&tl=" & strToLanguageCode & _
"&ie=UTF-8&prev=_m&q=" & strInput
Set objHTTP = CreateObject("MSXML2.ServerXMLHTTP")
objHTTP.Open "GET", strURL, False
objHTTP.setRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"
objHTTP.send ""
' create a html document
Set objHTML = CreateObject("htmlfile")
With objHTML
.Open
.Write objHTTP.responseText
.Close
End With
' o1 has Anglicised translation, t0 as tranlsation in target language
Set objDivs = objHTML.getElementsByTagName("div")
For Each objDiv In objDivs
If objDiv.className = "o1" Then
strTranslatedO1 = objDiv.innerText
End If
If objDiv.className = "t0" Then
strTranslatedT0 = objDiv.innerText
End If
Next objDiv
' choose which to return
If blnTargetAlphabet Then
Translate = strTranslatedT0
Else
Translate = strTranslatedO1
End If
CleanUp:
Set objHTML = Nothing
Set objHTTP = Nothing
End Function
答案 0 :(得分:0)
打开工作簿以收集数据。
如果立即关闭wb,则必须摆脱“退出”按钮中的workbook.close
行
Private Sub UserForm_Initialize()
Dim wb As Workbook
Set wb = Workbooks.Open("C:\Users\dmorrison\Downloads\TestRemoveLater\WorkOrderDatabase-1.xlsm")
'Empty Form
TextBox1.Value = ""
TextBox2.Value = ""
TextBox3.Value = ""
TextBox4.Value = ""
ComboBox1.Value = Null
'Initialized value
TextBox5.Value = "A"
ComboBox2.Value = "N/A"
ComboBox3.Value = "N/A"
ComboBox4.Value = "N/A"
ComboBox5.Value = "N/A"
ComboBox6.Value = "N/A"
ComboBox7.Value = "N/A"
With wb
Me.ComboBox1.List = .Worksheets(Page).ListObjects("Table15").ListColumns(1).DataBodyRange.Value
Me.ComboBox2.List = .Worksheets(Page).ListObjects("Table24").ListColumns(1).DataBodyRange.Value
Me.ComboBox3.List = .Worksheets(Page).ListObjects("Table24").ListColumns(1).DataBodyRange.Value
Me.ComboBox4.List = .Worksheets(Page).ListObjects("Table24").ListColumns(1).DataBodyRange.Value
Me.ComboBox5.List = .Worksheets(Page).ListObjects("Table24").ListColumns(1).DataBodyRange.Value
Me.ComboBox6.List = .Worksheets(Page).ListObjects("Table24").ListColumns(1).DataBodyRange.Value
Me.ComboBox7.List = .Worksheets(Page).ListObjects("Table24").ListColumns(1).DataBodyRange.Value
'.close
End With
TextBox7.Value = ""
TextBox8.Value = ""
TextBox9.Value = ""
End Sub