0

I am attempting to create a drop-down list populated with a range from another sheet. But what I want to do is show the full list, and when the user selects and item, it will only enter into that cell all characters before the dash.

So, if I had "David - Project Manager" upon selection the cell with populate with just "David" and remove everything after the dash. Everything after is just there to make things easier to see.

4

3 回答 3

0

VBA 方法不会破坏验证,因为 VBA 可以绕过这些要求。假设要调整 DV 的单元格范围是 B2:B100,试试这个:

Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)
Dim cell As Range

If Not Intersect(Target, Range("B2:B100")) Is Nothing Then
    For Each cell In Intersect(Target, Range("B2:B100"))
        If InStr(1, cell.Value, "-") > 0 Then
            Application.EnableEvents = False
            cell.Value = Trim(Left(cell.Value, InStr(1, cell.Value, "-") - 1))
            Application.EnableEvents = True
        End If
    Next cell
End If

End Sub

该宏进入 SHEET 模块。

于 2012-05-11T00:40:52.000 回答
0

我在Contextures Video中找到了答案。

在可下载的 XLS 文件中,有一个指向工作表所需代码的链接。

Option Explicit
' Developed by Contextures Inc.
' www.contextures.com
Private Sub Worksheet_Change(ByVal Target As Range)
On Error GoTo errHandler
If Target.Cells.Count > 1 Then GoTo exitHandler
If Target.Column = 6 Then
  If Target.Value = "" Then GoTo exitHandler
  Application.EnableEvents = False
  Target.Value = Worksheets("codes").Range("C1").Offset(Application.WorksheetFunction.Match(Target.Value, Worksheets("codes").Range("activitycodes"), 0) - 1, -2)
End If

exitHandler:
  Application.EnableEvents = True
  Exit Sub

errHandler:
  If Err.Number = 13 Or Err.Number = 1004 Then
    GoTo exitHandler
  Else
    Resume Next
  End If

End Sub
于 2012-05-11T11:29:24.767 回答
0

你不能用Data Validation. VBA 可用于修改已验证的单元格(但这会破坏验证,不是吗?)Data Validation提供完整条目的折衷方案如何,然后相邻的单元格将其修剪为您的要求,我认为这是:

=IFERROR(TRIM(LEFT(A1,SEARCH("-",A1)-1)),A1)

A1一个单元格包含在哪里Data Validation

于 2012-05-10T22:15:29.337 回答