excel - 将多个值选择到一个单元格中的 VBA VLookup
问题描述
我目前正在从事一个 VBA 项目的工作。我正在尝试编写一个 VBA 代码,当从数据验证下拉列表中选择值时,该代码会将多个值返回到一个单元格中。然而,我并没有取得太大进展。我将发布我正在处理的代码以及另外两个独立执行类似功能的代码。
这是我一直在处理的代码。Zone 是对具有我要使用的值的工作表的引用:
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rngDV As Range
Dim oldVal As String
Dim newVal As String
Dim lUsed As Long
Dim lOld As Long
Dim selectedNa As String
Dim selectedNum As String
Dim selectedNum1 As String
Dim selectedNa1 As String
If Target.Count > 1 Then GoTo exitHandler
On Error Resume Next
Set rngDV = Cells.SpecialCells(xlCellTypeAllValidation)
On Error GoTo exitHandler
If rngDV Is Nothing Then GoTo exitHandler
If Intersect(Target, rngDV) Is Nothing Then
'do nothing
Else
Application.EnableEvents = False
newVal = Target.Value
Application.Undo
oldVal = Target.Value
Target.Value = newVal
selectedNa = Target.Value
If Target.Column = 8 Then
selectedNum = Application.VLookup(selectedNa, Worksheets("Zones").Range("Zone"), 2, False)
If Not IsError(selectedNum) Then
Target.Value = selectedNum
oldVal = Target.Value
Else
lOld = Len(oldVal)
If Left(newVal, lOld) <> selectedNa Then
selectedNa1 = Target.Value
selectedNum1 = Application.VLookup(selectedNa1, Worksheets("Zones").Range("Zone"), 2, False)
If Not IsError(selectedNum1) Then
Target.Value = selectedNum1
newVal = Target.Value
If oldVal = "" Then
'do nothing
Else
If newVal = "" Then
'do nothing
Else
If Left(newVal, lOld) = oldVal Then
Target.Value = newVal
Else
lUsed = InStr(1, oldVal, newVal)
If lUsed > 0 Then
Target.Value = oldVal
Else
Target.Value = oldVal _
& "|" & newVal
End If
End If
End If
End If
End If
End If
End If
End If
End If
exitHandler:
Application.EnableEvents = True
End Sub
这是一个代码,允许您从下拉列表中选择多个值而无需 vlookup:
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rngDV As Range
Dim oldVal As String
Dim newVal As String
Dim lUsed As Long
Dim lOld As Long
If Target.Count > 1 Then GoTo exitHandler
On Error Resume Next
Set rngDV = Cells.SpecialCells(xlCellTypeAllValidation)
On Error GoTo exitHandler
If rngDV Is Nothing Then GoTo exitHandler
If Intersect(Target, rngDV) Is Nothing Then
'do nothing
Else
Application.EnableEvents = False
newVal = Target.Value
Application.Undo
oldVal = Target.Value
Target.Value = newVal
If Target.Column = 8 Then
If oldVal = "" Then
'do nothing
Else
If newVal = "" Then
'do nothing
Else
lOld = Len(oldVal)
If Left(newVal, lOld) = oldVal Then
Target.Value = newVal
Else
lUsed = InStr(1, oldVal, newVal)
If lUsed > 0 Then
Target.Value = oldVal
Else
Target.Value = oldVal _
& "|" & newVal
End If
End If
End If
End If
End If
End If
exitHandler:
Application.EnableEvents = True
End Sub
最后,这是一个执行 vlookup 的代码。希望可以有人帮帮我。谢谢!
Private Sub Worksheet_Change(ByVal Target As Range)
selectedNa = Target.Value
If Target.Column = 8 Then
selectedNum = Application.VLookup(selectedNa, Worksheets("Zones").Range("Zone"), 2, False)
If Not IsError(selectedNum) Then
Target.Value = selectedNum
oldVal = Target.Value
解决方案
推荐阅读
- python - 在 Python 中使用自定义异常动态扩展异常?
- php - 更改服务器时间的 PHP 脚本
- python - 如何读取包含多个记录类型的文件?
- r - R 代码根据单元格值读取 Google Sheet 特定行
- docker - 构建 Dockerfile 的问题
- applescript - AppleScript Bounds 从当前监视器移动应用程序
- java - 如何理解 Java API 文档中的这个接口 Executor 示例
- javascript - fs.readFileSync 不等待
- php - 每天从命令行获取 PHP lint / PhpStorm 检查错误
- javascript - 具有多输入的 Angular formArray