2013-03-18 87 views
3

列表框不會將選定的值分配給「n」。無論我是否從列表框中選擇值,「n」值爲0。我正在學習,所以這可能是簡單的,我錯過了...建議?謝謝!循環多選列表框值創建並命名工作簿

Private Sub UserForm_Initialize() 

With cbomonth 
    .AddItem "January" 
    .AddItem "February" 
End With 
With cboyear 
    .AddItem "2013" 
    .AddItem "2014" 
End With 
With cboteam 
    .AddItem "Team1" 
    .AddItem "Team2" 
End With 
With cbodocument 
    .AddItem "Task1" 
    .AddItem "Task2" 
End With 
With ListBox1 
.AddItem "Name" 
.AddItem "Name" 
End With 
cboteam.ListIndex = 0 
cboyear.ListIndex = 4 
cbomonth.ListIndex = 6 
cbodocument.ListIndex = 1 

End Sub 

Private Sub cmdSubmit_Click() 

    Dim year As String 
    Dim month As String 
    Dim days As Integer 
    Dim team As String 
    Dim n as Long 
    Dim tallboxynames As Variant 
    Dim tallynewfile As String 

    Unload Me 

    year = cboyear.Value 
    month = cbomonth.Value 
    team = cboteam.Value 
    document = cbodocument.Value 

    TallyPath = "\\network path\Tally of orders\Master Template\" 
    TallyPath1 = "\\network path\Tally of orders\" & year & "\" 
    TallyPath2 = "\\network path\Tally of orders\" & year & "\" & month & "\" 
    TallyTemplate = "Tally_Template_ver1.xls" 

If document = "Tally Sheets" Then 
    For n = 0 To ListBox1.ListCount - 1 
     If ListBox1.Selected(n) Then 
      tallynewfile = ListBox1.Selected(n) & ".xls" 
     Else 
      MsgBox "No data from listbox" 
     End If 
If Len(Dir(TallyPath1, vbDirectory)) = 0 Then 
    MkDir TallyPath1 
End If 
If Len(Dir(TallyPath2, vbDirectory)) = 0 Then 
    MkDir TallyPath2 
    FileCopy TallyPath & TallyTemplate, TallyPath2 & tallynewfile 
End If 
    Next n 
End If 
Exit Sub 
End Sub 
+1

move將我卸載到Exit Sub之前的末尾。請注意,它最好隱藏用戶窗體上的點擊,並在主模塊中卸載 – 2013-03-18 18:17:34

+0

嘗試使用相同的結果。它是對它們進行計數,然後是「n」的0值。和Kaz的建議一樣。 – Mike 2013-03-18 18:39:27

+0

除此之外,我用完全相同的結果評論了它。 – Mike 2013-03-18 18:41:23

回答

6

移動卸載我的程序的末尾:

Private Sub cmdSubmit_Click() 
    ' code here ... 
    Unload Me 
End Sub 

要獲得所選擇的項目使用價值如果ListBox1.MultiSelect = 0(fmMultiSelectSingle):

Me.ListBox1.Value 

如果MultiSelect> 0,則使用Selected屬性,例如:

Private Function GetSelectedItems() As String 
    Dim text As String 
    Dim i As Integer 
    For i = 0 To Me.ListBox1.ListCount - 1 
     If Me.ListBox1.Selected(i) Then 
      text = text & Me.ListBox1.List(i) & vbNewLine 
     End If 
    Next i 
    MsgBox "Selected items are: " & text 
    GetSelectedItemsText = text 
End Function 
+0

這是正確的,實際上我有。最終,這是一個不同模塊的問題。呸。我結束了它的工作。 :) – Mike 2013-03-21 21:11:26

1

而不是

Unload Me 

嘗試使用

Me.Hide 

當你unload窗體上的所有值都將被刪除。如果您使用Hide,它們將保留。

+0

大聲笑好嘗試花花公子 – 2013-03-18 18:29:40

相關問題