2017-07-19 74 views
0

在片「選擇」我輸入A列中一個性別(M或F),和在列B中的對應值的excel VBA基於條件從列表中選擇(在另一個片)的值

enter image description here

在表格「尺寸」中,我列出了每種性別的可用尺寸,如下所示。

enter image description here

在片「選擇」,我希望它在列C編寫相應尺寸(即必須始終大於塔B更高)。如果沒有可用尺寸,則必須寫入「不可用」!

enter image description here

回答

2

Ctrl鍵 + + 輸入Cell C2輸入Selection

=IFERROR(IF(A2="M",INDEX(Sizes!$A$3:$A$10,MATCH(TRUE,Sizes!$A$3:$A$10>B2,0)),INDEX(Sizes!$B$3:$B$10,MATCH(TRUE,Sizes!$B$3:$B$10>B2,0))),"Not Available") 

的下式這是陣列式所以提交它。根據需要拖放/複製。參考

見圖像

enter image description here

編輯:


以下爲VBA溶液:

Sub Demo() 
    With Application 
     .ScreenUpdating = False    'stop screen flickering 
     .Calculation = xlCalculationManual 'prevent calculation while execution 
    End With 

    Dim selectionSht As Worksheet, sizeSht As Worksheet 
    Dim selectionLR As Long, sizeLR As Long, lastColumn As Long 
    Dim dict As Object 
    Dim rng As Range, cel As Range, genderRng As Range, valueRng As Range 
    Dim key As Variant 
    Dim colName As String 

    Set dict = CreateObject("Scripting.Dictionary") 
    Set selectionSht = ThisWorkbook.Sheets("Selection") 'Selection sheet 
    Set sizeSht = ThisWorkbook.Sheets("Sizes") 'Sizes sheet 
    selectionLR = selectionSht.Cells(selectionSht.Rows.Count, "A").End(xlUp).Row 'last row in Selection sheet 
    sizeLR = sizeSht.Cells(sizeSht.Rows.Count, "A").End(xlUp).Row 'last row in Sizes sheet 
    lastColumn = sizeSht.Cells(2, sizeSht.Columns.Count).End(xlToLeft).Column 'last column in Sizes sheet using row 2 
    Set valueRng = selectionSht.Range("B2:B" & selectionLR) 'data with value in Selection sheet 

    'storing all genders and corresponding column number from Sizes sheet in a dictionary 
    With sizeSht 
     Set rng = .Range(.Cells(2, 1), .Cells(2, lastColumn)) 
     For Each cel In rng 
      dict.Add cel.Value, cel.Column 
     Next cel 
    End With 

    With selectionSht 
     For Each cel In .Range(.Cells(2, 3), .Cells(selectionLR, 3)) '3 is column no for results to be displayed 
      colName = Replace(.Cells(1, dict(CStr(cel.Offset(0, -2)))).Address(True, False), "$1", "") 'get column name from column 2 
      Set genderRng = sizeSht.Range(colName & "3:" & colName & sizeLR) 'set column for index/match formula 
      cel.FormulaArray = "=IFERROR(INDEX(Sizes!" & genderRng.Address & ",MATCH(TRUE,Sizes!" & genderRng.Address & ">" & cel.Offset(0, -1) & ",0)),""Not Available"")" 
      cel.Value = cel.Value 
     Next cel 
    End With 

    With Application 
     .ScreenUpdating = True 
     .Calculation = xlCalculationAutomatic 
    End With 
End Sub 

如果有什麼不清楚,請告訴我。

+0

以及感謝,但我並不想用一個公式,因爲我想用在同一電子表格中用於其他目的/案例的代碼(但哲學是相同的)。我以一種簡單的方式描述了我的問題,但是例如在A列中,我可以有兩種以上的性別,例如M,F,jrM,jrF等等。這將創造一個巨大的,如果。或者比尺寸,材料更多。就像不同的材料有不同的尺寸等等。 –

+0

@ZedA。 - 我已經添加了VBA解決方案,請參閱我的答案中的編輯。 – Mrig

1

可以在C2使用(正常)公式和向下填充:你的回答

C2: 
=IFERROR(AGGREGATE(15,6,Sizes!$A$3:$B$10 
    /(Sizes!$A$2:$B$2=A2)/(Sizes!$A$3:$B$10>=B2),1), "Not Available!") 

enter image description here

+0

@ZedA這個公式可以輕鬆處理任意數量的「性別」。輸入後,您可以在「尺寸」表中「插入」欄,並自動進行調整。 –

+0

如果尺寸不是數字,該怎麼辦?假設對於100到200的值,相應的大小是S,從201到300,大小是M等等。 –

相關問題