excel VBA根据条件从列表(在另一张图纸中)中选择一个值
在“选择”表中,我在a列输入性别(M或F),在B列输入相应的值 在“尺寸”表中,我列出了每种性别的可用尺寸,如下所示 在“选择”表中,我希望它在C列上写下相应的大小(必须始终高于B列)。如果没有可用的大小,它必须写“不可用!”excel VBA根据条件从列表(在另一张图纸中)中选择一个值,excel,vba,Excel,Vba,在“选择”表中,我在a列输入性别(M或F),在B列输入相应的值 在“尺寸”表中,我列出了每种性别的可用尺寸,如下所示 在“选择”表中,我希望它在C列上写下相应的大小(必须始终高于B列)。如果没有可用的大小,它必须写“不可用!” 在选择工作表的单元格C2中输入以下公式 =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,
在
选择
工作表的单元格C2
中输入以下公式
=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")
这是一个数组公式,请按Ctrl+Shift+Enter键提交它。根据需要向下拖动/复制
请参见图片以供参考
编辑:
以下是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
如果有任何不清楚的地方,请告诉我。在
选择的单元格C2
中输入以下公式
=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")
这是一个数组公式,请按Ctrl+Shift+Enter键提交它。根据需要向下拖动/复制
请参见图片以供参考
编辑:
以下是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
如果有任何不清楚的地方,请告诉我。您可以在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!")
您可以在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!")
非常感谢您的回答,但我不想使用公式,因为我想在同一个电子表格中将代码用于其他目的/情况(但原理是一样的)。我用一种简单的方式描述了我的问题,但例如在a列中,我可以有两种以上的性别,比如M、F、jrM、jrF等等。这将产生一个巨大的if。或者超过尺寸、材料。就像不同的材料有不同的尺寸,等等…@ZedA.-我已经添加了VBA解决方案,请参见我答案中的编辑。非常感谢您的回答,但我不想使用公式,因为我想在同一电子表格中将代码用于其他目的/情况(但原理是相同的)。我用一种简单的方式描述了我的问题,但例如在a列中,我可以有两种以上的性别,比如M、F、jrM、jrF等等。这将产生一个巨大的if。或者超过尺寸、材料。就像不同的材料有不同的尺寸,等等…@ZedA.-我已经添加了VBA解决方案,请参见我答案中的编辑。@ZedA此公式可以轻松处理任意数量的“性别”。输入后,您可以在“尺寸”表中“插入”列,它将自动调整。如果尺寸不是数字怎么办?假设值从100到200,对应的大小是s,从201到300,大小是M,等等@ZedA这个公式可以轻松处理任意数量的“性别”。输入后,您可以在“尺寸”表中“插入”列,它将自动调整。如果尺寸不是数字怎么办?假设值从100到200,对应的大小是s,从201到300,大小是M,以此类推。