我在工作表1中为城市创建了一个多选下拉菜单,与此下拉列表相关的邮政编码在工作表2中。 这是我的工作表2的外观。
1。)。允许用户从下拉列表中选择多个城市。用户选择城市后,我想在一个单元格中显示选定的城市和相关的邮政编码。例如如果用户从下拉菜单中选择了Sion和Dadar,则在下拉菜单下方,用户应该可以看到类似的内容。
在Vlookup的帮助下,我既可以检索值之一,也不能在单个等于等号的单元格中显示。
2。)。此外,我还使用了Internet上的VBA代码进行多次选择和删除。该代码工作正常,但我想对其进行一些更改。就像当用户选择两个城市时,该值将填充在由“逗号”分隔的下拉单元格中。我希望每次第二个值都在下一行,但要保留在同一单元格中,并且还要动态调整行高,并在顶部和底部保留一定的边距。我是VBA的新手,不知道如何在下一行获得它。 这是目前的样子。
但我希望它看起来不是这样,
这是我使用的VBA代码。
Private Sub Worksheet_Change(ByVal Target As Range)
Dim xRng As Range
Dim xValue1 As String
Dim xValue2 As String
If Target.Count > 1 Then Exit Sub
On Error Resume Next
Set xRng = Me.Range("J2, K2,L2,M2,N2")
If xRng Is Nothing Then Exit Sub
Application.EnableEvents = False
If Not Application.Intersect(Target, xRng) Is Nothing Then
xValue2 = Target.Value
Application.Undo
xValue1 = Target.Value
Target.Value = xValue2
If xValue1 <> "" Then
If xValue2 <> "" Then
If InStr(1, xValue1, xValue2 & ",") > 0 Then
xValue1 = Replace(xValue1, xValue2 & ", ", "") ' If it's in the middle with comma
Target.Value = xValue1
GoTo jumpOut
End If
If InStr(1, xValue1, ", " & xValue2) > 0 Then
xValue1 = Replace(xValue1, ", " & xValue2, "") ' If it's at the end with a comma in front of it
Target.Value = xValue1
GoTo jumpOut
End If
If xValue1 = xValue2 Then ' If it is the only item in string
xValue1 = ""
Target.Value = xValue1
GoTo jumpOut
End If
Target.Value = xValue1 & ", " & xValue2
End If
jumpOut:
End If
End If
Application.EnableEvents = True
End Sub
答案 0 :(得分:1)
选择公式»定义的名称»名称管理器
用以下公式替换“引用”公式: = OFFSET(查找量!$ A $ 2,0,0,COUNTA(查找量!$ A:$ A)-1)
现在,您可以在“优先级”列表中添加和删除值了,而下拉菜单将具有更新后的值,而无需其他工作!
要分解OFFSET公式的用法(以List_Priority为例):
0:停留在同一列中(因此,仍然是$ A $ 2)
COUNTA(Lookups $ A:$ A)-1:计算A列中 有值,然后减去1(标题单元格:“优先级”);抓 从当前单元格开始的那个高的区域 “选定”($ A $ 2)
添加从属下拉列表
在工作表查找上放置代码
Private Sub Worksheet_Change(ByVal Target As Range)
On Error Resume Next
If Not Intersect(Target, Range("E6")) Is Nothing And Target.Cells.Count = 1 Then
Application.EnableEvents = False
If Len(Target.Offset(1, 0)) = 0 Then ' (1,0) down direction (0,1) right
Target.Offset(1, 0) = Target ' (1,0) down direction (0,1) right
Else
Target.End(xlDown).Offset(1, 0) = Target ' (1,0) down direction (0,1) right
End If
Target.ClearContents
Application.EnableEvents = True
End If
End Sub
答案 1 :(得分:1)
对于 锡永= 400022 您可以使用Vlookup公式
=VLOOKUP(Table1[Segments];Table1[Segments];1;FALSE)&" = "&VLOOKUP(Table1[Segments];Sheet2!A2:B4;2;FALSE)
我不知道如何进行多选。仅当用户从下拉列表中选择单个选项时,此功能才起作用
答案 2 :(得分:0)
另一种解决方案。更改工作表的名称和范围,然后尝试:
Option Explicit
Sub test()
Dim strCitys As String
Dim rng As Range
Dim arr As Variant, strResults As Variant, City As Variant
With ThisWorkbook.Worksheets("Sheet1")
strCitys = .Range("A1").Value
Set rng = .Range("D1:E3")
strResults = ""
If strCitys <> "" Then
If InStr(1, strCitys, ",") = 0 Then
strResults = Application.VLookup(strCitys, rng, 2, False)
If Not IsError(strResults) Then
.Range("B1").Value = strCitys & "=" & strResults
Else
.Range("B1").Value = strCitys & "=" & "Missing Code"
End If
Else
For Each City In Split(strCitys, ",")
strResults = Application.VLookup(Trim(City), rng, 2, False)
If Not IsError(strResults) Then
If .Range("B1").Value = "" Then
.Range("B1").Value = Trim(City) & "=" & strResults
Else
.Range("B1").Value = .Range("B1").Value & vbNewLine & Trim(City) & "=" & strResults
End If
Else
If .Range("B1").Value = "" Then
.Range("B1").Value = Trim(City) & "=" & "Missing Code"
Else
.Range("B1").Value = .Range("B1").Value & vbNewLine & Trim(City) & "=" & "Missing Code"
End If
End If
Next City
End If
Else
.Range("B1").Clear
MsgBox "Please select city/ies."
End If
End With
End Sub
结果: