2016-04-05 21 views
0

ユーザーフォームのリストボックスの値を、同じユーザーフォームの別のリストボックスの値を変更したときに動的に変更する方法を教えてください。私に困っている主な部分はlstMonthです。 lstMonthの値は、最初のリストボックスに表示される異なるシートのデータと相関する月と等しくなければなりません。私はフォームとデータの写真を提供して、これ以降すべてがより多くなるようにします。 enter image description hereリストボックスの値をワークシートで動的に変更する

コード

Option Explicit 


Private Sub cmdCalc_Click() 
    'declare variables and assign address to rngData variable 
    Dim strId As String, intRow As Integer, intNumSales As Integer, curSales As Currency 
    Dim rngData As Range 
    Set rngData = Application.Workbooks("t13-ex-e4d.xls").Worksheets("jan").Range("a3").CurrentRegion 
    'assign selected list box item to strId variable 
    strId = lstId.Value 
    'locate first occurrence of ID 
    intRow = 2 
    Do Until rngData.Cells(RowIndex:=intRow, columnindex:=4).Value = strId 
     intRow = intRow + 1 
    Loop 
    'accumulate and count salesperson's sales, stop when loop encounters different ID 
    Do While rngData.Cells(RowIndex:=intRow, columnindex:=4).Value = strId 
     curSales = curSales + rngData.Cells(RowIndex:=intRow, columnindex:=3).Value 
     intNumSales = intNumSales + 1 
     intRow = intRow + 1 
    Loop 
    'display appropriate amount 
    If optTotal.Value = True Then 
     lblAnswer.Caption = Format(expression:=curSales, Format:="currency") 
    Else 
     lblAnswer.Caption = Format(expression:=curSales/intNumSales, Format:="currency") 
    End If 

End Sub 

Private Sub cmdCancel_Click() 
    'close custom dialog box 
    Unload frmSalesCalc 

End Sub 


Private Sub UserForm_Initialize() 

lstMonth.Value = Application.Workbooks("T13-EX-E4D.xls").ActiveSheet.Range("b3").CurrentRegion 


End Sub 
+0

私は本当に質問を理解していない、どのような月のリストボックスにする必要がありますか?すべての月があるか、または営業担当者が販売しているすべての月? –

+0

月リストボックスは、ブック内のすべてのワークシートの名前と同じにする必要があります。今は3つ(2月、3月、4月)ですが、誰かが5月を追加すると、自動的にリストボックスに入れることが必要になります。それが動作したら、月の左側のリストボックスは、選択されているワークシート上の販売IDに変更する必要があります。私はそれが助けて欲しい! –

答えて

1

フォームの残りの部分は月のリストをクリックすると、他のリストボックスを埋めるためにbasicalyあるすべてのavaibleシート

 ' Declare Current as a worksheet object variable. 
    Dim Current As Worksheet 

    ' Loop through all of the worksheets in the active workbook. 
    For Each Current In Worksheets 

     ' Insert your code here. 
     ListBox_Month.AddItem Current.Name 
    Next 

と月のリストボックスを埋める開き、すべてのavaible idとですから、このようなものが必要Listbox_Change方法で:

Dim sales_ids as Variant 
sales_ids = UniquesFromRange(Worksheets(Listbox_Month.value).Range(D)) 

Function UniquesFromRange(rng As Range) 
Dim d As Object, c As Range, tmp 
Set d = CreateObject("scripting.dictionary") 
For Each c In rng.Cells 
    tmp = Trim(c.Value) 
    If Len(tmp) > 0 Then 
     If Not d.Exists(tmp) Then d.Add tmp, 1 
    End If 
Next c 
UniquesFromRange = d.keys 
End Function 

今、あなたはすべてのIDを持っている、第二のリストボックス、出来上がりでそれらを記入し、残りは明確にする必要がありますが、あなたはまだ疑問を持っている場合だけで尋ねる

関連する問題