我正在尝试创建一个基于主用户窗体的“类似程序”,该用户窗体具有不同的屏幕(多页)和不同的导航按钮。
代码工作正常,然后,当我添加
Sub btn_find_MouseUp
子并尝试它时,我为每个 Compile Error: only comments may appear after end subs
都有这个 MouseMove
(这是我遇到错误时突出显示的代码)。
我已经检查了每一个End Sub以发现错误,但我没有任何运气。
我还检查了类似问题的所有答案,但没有结果:正如有人还建议我尝试删除所有内容在 Sub 和前一个 End Sub 之间,然后添加一个新行来“重新创建”空间,但每次我尝试我的 Excel 都会崩溃。
我还尝试在每个 End Sub 之后添加虚拟注释(如
'-----
)只是为了看看它是否有帮助,但每次我尝试在 last End Sub 之后添加虚拟注释时,我的 Excel 都会崩溃。
我非常害怕失去迄今为止所做的所有工作......
代码
模块可变性
我在其中定义了所有变量。
Option Explicit
'------------------------------------------------------------
' Dichiarazione variabili
'
Public version As String
Public author As String
Public my_email As String
Public path_base As String
Public path_click As String
Public nome_cw As String
Public ctype As String
Public company As String
Public chours As Integer
Public act_chours As Integer
Public donboard As Date
Public ddate As Date
Public scadenza As String
Public cnotes As String
Public ext1 As Date
Public ext2 As Date
Public ext3 As Date
Public ext4 As Date
Public tnotes As String
Public eteams As String
Public limit As String
Public pic_path As String
Public find As String
'------------------------------------------------------------
模块用户名
我在其中定义了我的用户名函数。
Public Function Username()
Dim nome1 As String, nome As String, arr_nome() As String
Dim check As Integer
nome1 = Application.Username
arr_nome = Split(nome1, " ") ' Creo un array con tutto il nome
check = UBound(arr_nome) - LBound(arr_nome) + 1 ' Conto da quante parole è formato il nome
nome = Split(nome1, " ")(0)
' Se il nome è composta da più di due parole (nome-cognome) allora
' voglio che controlli se è un nome composito (es. Maria Elena o Gian Pietro)
' ed in quel caso voglio che stampi i due nomi
If check > 2 Then
If nome = "Maria" Or nome = "Gian" Then
nome = Split(nome1, " ")(0) & " " & Split(nome1, " ")(1)
Else
nome = Split(nome1, " ")(0)
End If
Else
nome = Split(nome1, " ")(0)
End If
Username = StrConv(nome, vbProperCase)
End Function
main_roster_236 表格
这是我的主要形式。
Sub UserForm_Initialize()
Dim cControl As Control
' Application.Visible = False
version = "v0.0.1 ALPHA"
author = "Name Surname"
my_email = "[email protected]"
path_base = "standard_path\folder1\"
path_click = "standard_path\folder2\"
roster_236_main.BackColor = RGB(249, 249, 249)
lbl_background.BackColor = RGB(240, 240, 240)
' Stile dei bubble dei titoli
'
For Each cControl In Me.Controls
If cControl.Name Like "lbl_bubble_title*" Then cControl.BackColor = RGB(251, 157, 20)
Next
' Stile dei bubble sfondo
'
For Each cControl In Me.Controls
If cControl.Name Like "lbl_bubble_bg*" Then cControl.BackColor = RGB(255, 245, 255)
Next
mp_cerca.BackColor = RGB(240, 240, 240)
mp_cerca.Style = fmTabStyleNone
mp_cerca.Visible = False
mp_1.BackColor = RGB(240, 240, 240)
mp_1.Style = fmTabStyleNone
mp_1.Top = 100
left_shadow.BackColor = RGB(200, 200, 200)
left_shadow2.BackColor = RGB(230, 230, 230)
For Each cControl In Me.Controls
If cControl.Name Like "cmb*" Then cControl.Value = vbNullString
Next
For Each cControl In Me.Controls
If cControl.Name Like "btn*" Then cControl.ForeColor = RGB(33, 35, 48)
Next
For Each cControl In Me.Controls
If cControl.Name Like "lbl*" Then cControl.ForeColor = RGB(33, 35, 48)
Next
For Each cControl In Me.Controls
If cControl.Name Like "btn*" Then cControl.Picture = LoadPicture(path_base & Mid(cControl.Name, 5) & ".jpg")
Next
For Each cControl In Me.Controls
If cControl.Name Like "btn*" Then cControl.BackStyle = fmBackStyleTransparent
Next
' Il bottone settings appare solo se sei l'autore
If StrConv(Application.Username, vbProperCase) <> "Name Surname" Then
btn_settings.Enabled = False
btn_settings.Visible = False
Else
End If
' Il tasto cerca ha uno stile suo
'
btn_find.ForeColor = RGB(255, 255, 255)
btn_find.BackStyle = fmBackStyleOpaque
btn_find.BackColor = RGB(168, 122, 181)
lblDate.Caption = Format(Date, "dd / mm / yyyy")
lblSectionTitle.Caption = "Postpone will always be the solutions."
lblCredits.Caption = "Designed & Developed by " & author
lblVersion.Caption = version
lbl_username = Username
Me.mp_cerca.Value = 0
Me.mp_1.Value = 0
lbl_welcome1.Caption = "Ciao " & Username & "," & vbNewLine & _
"ti do il benvenuto nel nuovo tool "
lbl_welcome2.Caption = "Usa il menu a sinistra per navigare liberamente all'interno del programma e scegliere cosa fare."
With cmb_group
.AddItem "A"
.AddItem "B"
.AddItem "C"
.AddItem "D"
.AddItem "M"
.AddItem "P"
.AddItem "SC"
.AddItem "E"
.AddItem "PT"
.AddItem "E"
.AddItem "AV"
.AddItem "CR"
.AddItem "SORT"
.AddItem "SUPPORT"
.AddItem "R&M"
End With
nome_cw = vbNullString
ctype = vbNullString
company = vbNullString
chours = 0
act_chours = 0
donboard = 0
ddate = 0
cnotes = vbNullString
ext1 = 0
ext2 = 0
ext3 = 0
ext4 = 0
tnotes = vbNullString
eteams = vbNullString
limit = vbNullString
pic_path = vbNullString
End Sub
' Gestisce l'hover (quando il mouse passa sopra) dei bottoni
'
Sub UserForm_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
Dim cControl As Control
For Each cControl In Me.Controls
If cControl.Name Like "btn*" And cControl.Name <> "btn_find" Then cControl.BorderStyle = fmBorderStyleNone
Next
End Sub
'------------------------------------------------------------------------
Sub lbl_background_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
Dim cControl As Control
For Each cControl In Me.Controls
If cControl.Name Like "btn*" Then cControl.BorderStyle = fmBorderStyleNone
Next
End Sub
'------------------------------------------------------------------------
' Cosa accade quando chiudiamo l'userform con la "X"
'
Sub UserForm_QueryClose(Cancel As Integer, ClassMode As Integer)
ThisWorkbook.Save
Application.Visible = True
Unload Me
roster_236_main.Hide
' ThisWorkbook.Close
End Sub
'------------------------------------------------------------------------
' Bottone cerca
'
Sub btn_find_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
Dim count As Integer
Dim rwNum As Integer
rwNum = 2
count = 0
If txt_cid.Value = "" And txt_nome_cw.Value <> "" Then
find = txt_nome_cw.Value
' Ricerca del nome in tabella
Sheets("Database").Activate
ActiveSheet.ListObjects("Anagrafica").Range.AutoFilter Field:=4, Criteria1:="=*find*", Operator:=xlAnd
Do Until Range("A" & rwNum).Value = ""
count = count + 1
rwNum = rwNum + 1
Loop
If count = 1 Then
rwNum = .Range("D" & .Rows.count).End(xlUp).Row
nome_cw = .Range("D" & rwNum).Value
ElseIf count = 0 Then
MsgBox ("! ATTENZIONE !" & vbNewLine & _
"I dati inseriti non hanno portato ad alcun risultato!" & vbNewLine & _
"Inserire dati corretti o cambiare tipo di ricerca.")
ElseIf count > 1 Then
MsgBox ("! ATTENZIONE !" & vbNewLine & _
"I dati inseriti hanno portato a più di un risultato!" & vbNewLine & _
"Inserire dati più specifici o cambiare tipo di ricerca.")
End If
ElseIf txt_cid.Value <> "" And txt_nome_cw.Value = "" Then
find = txt_cid.Value
ElseIf txt_cid.Value <> "" And txt_nome_cw.Value <> "" Then
End If
End Sub
'------------------------------------------------------------------------
' Bottone add_user
'
Sub btn_add_user_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
For Each cControl In Me.Controls
If cControl.Name Like "btn*" Then cControl.Picture = LoadPicture(path_base & Mid(cControl.Name, 5) & ".jpg")
Next
For Each cControl In Me.Controls
If cControl.Name Like "btn*" And cControl.Name <> "btn_find" Then cControl.BackStyle = fmBackStyleTransparent
Next
mp_1.Top = 100
btn_add_user.BackStyle = fmBackStyleOpaque
btn_add_user.BackColor = RGB(168, 122, 181)
btn_add_user.Picture = LoadPicture(path_click & "add_user.jpg")
lblSectionTitle.Caption = "Inserimento Nuovo Co-Worker"
mp_cerca.Visible = False
End Sub
'------------------------------------------------------------------------
Sub btn_add_user_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
btn_add_user.BorderStyle = fmBorderStyleSingle
btn_add_user.BorderColor = &HC0C0C0
End Sub
'------------------------------------------------------------------------
' Bottone remove_user
'
Sub btn_remove_user_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
For Each cControl In Me.Controls
If cControl.Name Like "btn*" Then cControl.Picture = LoadPicture(path_base & Mid(cControl.Name, 5) & ".jpg")
Next
For Each cControl In Me.Controls
If cControl.Name Like "btn*" And cControl.Name <> "btn_find" Then cControl.BackStyle = fmBackStyleTransparent
Next
mp_1.Top = 100
btn_remove_user.BackStyle = fmBackStyleOpaque
btn_remove_user.BackColor = RGB(168, 122, 181)
btn_remove_user.Picture = LoadPicture(path_click & "remove_user.jpg")
lblSectionTitle.Caption = "Cessazione Co-Worker"
mp_cerca.Visible = False
End Sub
'------------------------------------------------------------------------
Sub btn_remove_user_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
btn_remove_user.BorderStyle = fmBorderStyleSingle
btn_remove_user.BorderColor = &HC0C0C0
End Sub
'------------------------------------------------------------------------
' Bottone modifica dati cw
'
Sub btn_modifica_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
For Each cControl In Me.Controls
If cControl.Name Like "btn*" Then cControl.Picture = LoadPicture(path_base & Mid(cControl.Name, 5) & ".jpg")
Next
For Each cControl In Me.Controls
If cControl.Name Like "btn*" And cControl.Name <> "btn_find" Then cControl.BackStyle = fmBackStyleTransparent
Next
mp_1.Top = 156
cmb_group.Enabled = True
cmb_group.Locked = False
lbl_group.Enabled = True
btn_modifica.BackStyle = fmBackStyleOpaque
btn_modifica.BackColor = RGB(168, 122, 181)
btn_modifica.Picture = LoadPicture(path_click & "modifica.jpg")
lblSectionTitle.Caption = "Modifica Dati Co-Worker"
mp_cerca.Visible = True
End Sub
'------------------------------------------------------------------------
Sub btn_modifica_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
btn_modifica.BorderStyle = fmBorderStyleSingle
btn_modifica.BorderColor = &HC0C0C0
End Sub
'------------------------------------------------------------------------
' Bottone modifica flex
'
Sub btn_flex_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
For Each cControl In Me.Controls
If cControl.Name Like "btn*" Then cControl.Picture = LoadPicture(path_base & Mid(cControl.Name, 5) & ".jpg")
Next
For Each cControl In Me.Controls
If cControl.Name Like "btn*" And cControl.Name <> "btn_find" Then cControl.BackStyle = fmBackStyleTransparent
Next
mp_1.Top = 156
cmb_group.Enabled = True
cmb_group.Locked = False
lbl_group.Enabled = True
btn_flex.BackStyle = fmBackStyleOpaque
btn_flex.BackColor = RGB(168, 122, 181)
btn_flex.Picture = LoadPicture(path_click & "flex.jpg")
lblSectionTitle.Caption = "Modifica Flessibilità"
mp_cerca.Visible = True
End Sub
'------------------------------------------------------------------------
Sub btn_flex_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
btn_flex.BorderStyle = fmBorderStyleSingle
btn_flex.BorderColor = &HC0C0C0
End Sub
'------------------------------------------------------------------------
' Bottone modifica contratto
'
Sub btn_contratto_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
For Each cControl In Me.Controls
If cControl.Name Like "btn*" Then cControl.Picture = LoadPicture(path_base & Mid(cControl.Name, 5) & ".jpg")
Next
For Each cControl In Me.Controls
If cControl.Name Like "btn*" And cControl.Name <> "btn_find" Then cControl.BackStyle = fmBackStyleTransparent
Next
mp_1.Top = 156
cmb_group.Enabled = True
cmb_group.Locked = False
lbl_group.Enabled = True
btn_contratto.BackStyle = fmBackStyleOpaque
btn_contratto.BackColor = RGB(168, 122, 181)
btn_contratto.Picture = LoadPicture(path_click & "contratto.jpg")
lblSectionTitle.Caption = "Modifica Contratto"
mp_cerca.Visible = True
End Sub
'------------------------------------------------------------------------
Sub btn_contratto_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
btn_contratto.BorderStyle = fmBorderStyleSingle
btn_contratto.BorderColor = &HC0C0C0
End Sub
'------------------------------------------------------------------------
' Bottone rinnovo
'
Sub btn_rinnovo_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
For Each cControl In Me.Controls
If cControl.Name Like "btn*" Then cControl.Picture = LoadPicture(path_base & Mid(cControl.Name, 5) & ".jpg")
Next
For Each cControl In Me.Controls
If cControl.Name Like "btn*" And cControl.Name <> "btn_find" Then cControl.BackStyle = fmBackStyleTransparent
Next
mp_1.Top = 156
cmb_group.Enabled = True
cmb_group.Locked = False
lbl_group.Enabled = True
btn_rinnovo.BackStyle = fmBackStyleOpaque
btn_rinnovo.BackColor = RGB(168, 122, 181)
btn_rinnovo.Picture = LoadPicture(path_click & "rinnovo.jpg")
lblSectionTitle.Caption = "Rinnovi Contrattuali"
mp_cerca.Visible = True
End Sub
'------------------------------------------------------------------------
Sub btn_rinnovo_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
btn_rinnovo.BorderStyle = fmBorderStyleSingle
btn_rinnovo.BorderColor = &HC0C0C0
End Sub
'------------------------------------------------------------------------
' Bottone scheda
'
Sub btn_scheda_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
For Each cControl In Me.Controls
If cControl.Name Like "btn*" Then cControl.Picture = LoadPicture(path_base & Mid(cControl.Name, 5) & ".jpg")
Next
For Each cControl In Me.Controls
If cControl.Name Like "btn*" And cControl.Name <> "btn_find" Then cControl.BackStyle = fmBackStyleTransparent
Next
mp_1.Top = 156
mp_cerca.Visible = True
cmb_group.Enabled = False
cmb_group.Locked = True
lbl_group.Enabled = False
btn_scheda.BackStyle = fmBackStyleOpaque
btn_scheda.BackColor = RGB(168, 122, 181)
btn_scheda.Picture = LoadPicture(path_click & "scheda.jpg")
lblSectionTitle.Caption = "Scheda Co-Worker"
Me.mp_1.Value = 1
' Riempiamo la scheda con i valori di default o gli ultimi salvati sulle variabili
'
' Colonna sinistra
label_nome_cw.Caption = nome_cw
lbl_CType.Caption = ctype
lbl_Company.Caption = company
If chours <> 0 Then
lbl_CHours.Caption = chours
ElseIf chours = 0 Then
lbl_CHours.Caption = vbNullString
End If
If act_chours <> 0 Then
lbl_ActCHours.Caption = act_chours
ElseIf act_chours = 0 Then
lbl_ActCHours.Caption = vbNullString
End If
If donboard <> 0 Then
lbl_DOnboard.Caption = Format(donboard, "dd/mm/yyyy")
ElseIf donboard = 0 Then
lbl_DOnboard.Caption = vbNullString
End If
If ddate <> 0 Then
lbl_DDate.Caption = Format(ddate, "dd/mm/yyyy")
ElseIf ddate = 0 Then
lbl_DDate.Caption = vbNullString
End If
lbl_Scadenza.Caption = scadenza
lbl_CNotes.Caption = cnotes
' Colonna centrale sup
If ext1 <> 0 Then
lbl_Ext1.Caption = Format(ext1, "dd/mm/yyyy")
ElseIf ext1 = o Then
lbl_Ext1.Caption = vbNullString
End If
If ext2 <> 0 Then
lbl_Ext2.Caption = Format(ext2, "dd/mm/yyyy")
ElseIf ext2 = o Then
lbl_Ext2.Caption = vbNullString
End If
If ext3 <> 0 Then
lbl_Ext3.Caption = Format(ext3, "dd/mm/yyyy")
ElseIf ext3 = o Then
lbl_Ext3.Caption = vbNullString
End If
If ext4 <> 0 Then
lbl_Ext4.Caption = Format(ext4, "dd/mm/yyyy")
ElseIf ext4 = o Then
lbl_Ext4.Caption = vbNullString
End If
' Colonna centrale inf
lbl_TNotes.Caption = tnotes
lbl_ETeams.Caption = eteams
' Colonna destra
lbl_Limit.Caption = limit
' Colonna fotografia
End Sub
'------------------------------------------------------------------------
Sub btn_scheda_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
btn_scheda.BorderStyle = fmBorderStyleSingle
btn_scheda.BorderColor = &HC0C0C0
End Sub
'------------------------------------------------------------------------
' Bottone database
'
Sub btn_database_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
For Each cControl In Me.Controls
If cControl.Name Like "btn*" Then cControl.Picture = LoadPicture(path_base & Mid(cControl.Name, 5) & ".jpg")
Next
For Each cControl In Me.Controls
If cControl.Name Like "btn*" And cControl.Name <> "btn_find" Then cControl.BackStyle = fmBackStyleTransparent
Next
mp_1.Top = 156
cmb_group.Enabled = True
cmb_group.Locked = False
lbl_group.Enabled = True
btn_database.BackStyle = fmBackStyleOpaque
btn_database.BackColor = RGB(168, 122, 181)
btn_database.Picture = LoadPicture(path_click & "database.jpg")
lblSectionTitle.Caption = "Database Co-Worker"
mp_cerca.Visible = True
End Sub
'------------------------------------------------------------------------
Sub btn_database_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
btn_database.BorderStyle = fmBorderStyleSingle
btn_database.BorderColor = &HC0C0C0
End Sub
'------------------------------------------------------------------------
' Bottone settings
'
Sub btn_settings_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
Application.Visible = True
Unload Me
roster_236_main.Hide
End Sub
'------------------------------------------------------------------------
Sub btn_settings_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
btn_settings.BorderStyle = fmBorderStyleSingle
btn_settings.BorderColor = &HC0C0C0
End Sub
'------------------------------------------------------------------------
' Bottone help
'
Sub btn_help_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
btn_help.BorderStyle = fmBorderStyleSingle
btn_help.BorderColor = &HC0C0C0
End Sub
'------------------------------------------------------------------------
' Bottone segnalazione
'
Sub btn_segnalazione_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
For Each cControl In Me.Controls
If cControl.Name Like "btn*" Then cControl.Picture = LoadPicture(path_base & Mid(cControl.Name, 5) & ".jpg")
Next
For Each cControl In Me.Controls
If cControl.Name Like "btn*" And cControl.Name <> "btn_find" Then cControl.BackStyle = fmBackStyleTransparent
Next
mp_1.Top = 100
btn_segnalazione.BackStyle = fmBackStyleOpaque
btn_segnalazione.BackColor = RGB(168, 122, 181)
btn_segnalazione.Picture = LoadPicture(path_click & "segnalazione.jpg")
lblSectionTitle.Caption = "Segnalazioni Problematiche"
mp_cerca.Visible = False
End Sub
'------------------------------------------------------------------------
Sub btn_segnalazione_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
btn_segnalazione.BorderStyle = fmBorderStyleSingle
btn_segnalazione.BorderColor = &HC0C0C0
End Sub
我不确定这是怎么发生的,但代码现在可以工作了......
可能 - 正如在与我类似的其他一些问题中所写的 - 所有
End Sub
和以下 Sub btn_something_MouseMove
之间存在“空格”问题,所需的只是这些空格的完全“重置”。
正如我在问题中所说,我在删除部分代码时遇到了问题(Excel 崩溃),但剪切整个代码是防止它发生的解决方案。然后我从上到下复制粘贴每个
Sub
,每次保存并检查运行代码是否有错误。
它又开始工作了。不知道如何或为什么。 希望这可以帮助别人!