具有相对参考的VBA列表框

时间:2018-11-16 11:10:40

标签: reference listbox relative

我对列表框有问题。 在下面的屏幕截图中,您可以看到我有“模板”,您可以在其中填写方案的名称(此处为1,2,3),并通过列表框选择该方案的受影响人员。当“受影响的人”-单元格(B5)旁边的单元格处于活动状态时,将出现列表框。在选择了受影响的人并选择了另一个单元之后,然后选择B5,应将所选选项插入B5。

我已经设法做的是使代码适用于Listbox1。现在的问题是通过Macro插入一个新的场景模板(以及一个新的listbox)。我不知道如何将代码中的固定引用更改为相对引用,以使代码适用于每个新列表框。 如果您有一个解决该问题的想法,我将万分感谢! :)

[1) screenshot][1]
[2) screenshot][2]
[3) screenshot][3]
[4) screenshot][4]

Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim aCR As Integer      ' aCR= Row of the ActiveCell
Dim aCC As Integer      'aCC =Column of the ActiveCell 
Dim m As Integer
Dim c As String
aCR = ActiveCell.Row
aCC = ActiveCell.Column
m = aCR Mod 5
If m = 0 Then
            If aCC = 2 Then
                Listbox1.Visible = True             ‘ Problem! Here the Listbox should be made visible which is above the ActiveCell (not visible yet!) !  No relative reference here
End If 
End If
End Sub

Sub ListBox1_LostFocus()                                                     ‘ Problem! No relative reference here
Dim str_selected_items As String, i As Long
With Listbox1                                                                  ‘ Problem! No relative reference here
    For i = 0 To .ListCount - 1
                 If .Selected(i) Then
                                str_selected_items = str_selected_items & "- " & .List(i) & Chr(10)
                     End If
    Next i
End With
If Len(listitems_spalte_1) > 0 Then                                                      
Range("B5").Value = Left(str_selected_items, Len(str_selected_items) - 1)       ‘ ‘ Problem! No relative reference here 
Else
Range("B5") = ""         ‘ Problem! No relative reference here 
End If
Listbox1.Visible = False            ‘ Problem! No relative reference here
End Sub

  [1]: https://i.stack.imgur.com/Og1Lr.png
  [2]: https://i.stack.imgur.com/PmzK3.png
  [3]: https://i.stack.imgur.com/4QdWh.png
  [4]: https://i.stack.imgur.com/i2ml6.png

0 个答案:

没有答案