gpt4 book ai didi

vba - Excel:在 VBA 中选择单个单元格与整列

转载 作者:行者123 更新时间:2023-12-02 21:30:37 24 4
gpt4 key购买 nike

这是一个新问题:

我有两张纸。 Sheet 1 是用于输入数据的表格。当您双击 A 列中的任何单元格时,会弹出一个用户表单。您输入工作表 2 A 列中任何条目中的几个键,它会自动完成。

我遇到的问题是:我只想在特定单元格上输入数据,例如 A1 ..而不是 A 的整个列。我想要的第二件事是,我希望它不是双击,而是只需单击一下即可工作。任何人都可以帮忙吗?

这是工作表 1 的 VBA 代码,您可以在其中输入数据

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Dim uiChosen As String
Dim MyList As Range
Dim myPrompt As String

If Target.Column <> 1 Then Exit Sub

Set MyList = Sheet2.Range("Cariler")
myPrompt = "Lütfen Bir Cari Seçin"
uiChosen = UserForm1.ChooseFromList(MyList, myPrompt, Default:=Target.Value, xlFilterStyle:=xlContains)

If StrPtr(uiChosen) <> 0 Then
Target.Value = uiChosen
Cancel = True
End If
End Sub

这是用户表单的代码:

Option Explicit

' in userform's code module

Dim FullList As Variant
Dim FilterStyle As XlContainsOperator
Dim DisableMyEvents As Boolean
Dim AbortOne As Boolean
Const xlNoFilter As Long = xlNone

Private Sub butCancel_Click()
Unload Me
End Sub

Private Sub butOK_Click()
Me.Tag = "OK"
Me.Hide
End Sub

Private Sub ComboBox1_Change()
Dim oneItem As Variant
Dim FilteredItems() As String
Dim NotFlag As Boolean
Dim Pointer As Long, i As Long

If DisableMyEvents Then Exit Sub
If AbortOne Then AbortOne = False: Exit Sub
If TypeName(FullList) Like "*()" Then
ReDim FilteredItems(1 To UBound(FullList))
DisableMyEvents = True
Pointer = 0
With Me.ComboBox1
Select Case FilterStyle
Case xlBeginsWith: .Tag = LCase(.Text) & "*"
Case xlContains: .Tag = "*" & LCase(.Text) & "*"
Case xlDoesNotContain: .Tag = "*" & LCase(.Text) & "*": NotFlag = True
Case xlEndsWith: .Tag = "*" & LCase(.Text)
Case xlNoFilter: .Tag = "*"
End Select

For Each oneItem In FullList
If (LCase(oneItem) Like .Tag) Xor NotFlag Then
Pointer = Pointer + 1
FilteredItems(Pointer) = oneItem
End If
Next oneItem

.List = FilteredItems
.DropDown

DisableMyEvents = False
If Pointer = 1 Then .ListIndex = 0
End With
End If
End Sub

Private Sub ComboBox1_Click()
butOK.SetFocus
End Sub

Private Sub ComboBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
Select Case KeyCode
Case vbKeyReturn: Call butOK_Click
Case vbKeyUp, vbKeyDown: AbortOne = True
End Select
End Sub

Private Sub Label1_Click()

End Sub

Private Sub UserForm_Activate()
ComboBox1.SetFocus
If ComboBox1.Text <> vbNullString Then
Call ComboBox1_Change
End If
End Sub

Private Sub UserForm_Initialize()
ComboBox1.MatchEntry = fmMatchEntryNone
End Sub

Public Function ChooseFromList(ListSource As Variant, Optional Prompt As String = "Choose one item", _
Optional Title As String = "Cari Arama Programı", Optional Default As String, _
Optional xlFilterStyle As XlContainsOperator = xlBeginsWith) As String

Dim Pointer As Long, oneItem As Variant
If TypeName(ListSource) = "Range" Then
With ListSource
Set ListSource = Application.Intersect(.Cells, .Parent.UsedRange)
End With
If ListSource Is Nothing Then Exit Function
If ListSource.Cells.Count = 1 Then
ReDim FullList(1 To 1): FullList(1) = ListSource.Value
ElseIf ListSource.Rows.Count = 1 Then
FullList = Application.Transpose(Application.Transpose(ListSource))
Else
FullList = Application.Transpose(ListSource)
End If
ElseIf TypeName(ListSource) Like "*()" Then
ReDim FullList(1 To 1)
For Each oneItem In ListSource
Pointer = Pointer + 1
If UBound(FullList) < Pointer Then ReDim Preserve FullList(1 To 2 * Pointer)
FullList(Pointer) = oneItem
Next oneItem
ReDim Preserve FullList(1 To Pointer)
ElseIf Not IsObject(ListSource) Then
ReDim FullList(1 To 1)
FullList(1) = CStr(ListSource)
Else
Err.Raise 1004
End If

Me.Caption = Title
Label1.Caption = Prompt
FilterStyle = xlFilterStyle

DisableMyEvents = True
ComboBox1.Text = Default
ComboBox1.List = FullList
DisableMyEvents = False

butOK.SetFocus
Me.Show

With UserForm1
If .Tag = "OK" Then ChooseFromList = .ComboBox1.Text
End With
End Function

最佳答案

没有单击事件。使用Intersect测试目标单元格是否在给定范围内。

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If Not Intersect(Target, Range("A1")) Is Nothing Then
Dim uiChosen As String
Dim MyList As Range
Dim myPrompt As String

If Target.Column <> 1 Then Exit Sub

Set MyList = Sheet2.Range("Cariler")
myPrompt = "Lütfen Bir Cari Seçin"
uiChosen = UserForm1.ChooseFromList(MyList, myPrompt, Default:=Target.Value, xlFilterStyle:=xlContains)

If StrPtr(uiChosen) <> 0 Then
Target.Value = uiChosen
Cancel = True
End If

End If

End Sub

关于vba - Excel:在 VBA 中选择单个单元格与整列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38180572/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com