VBA listbox with relative references



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















I have a problem with Listboxes.
In the Screenshots below you can see that I have “Templates” in which you can fill in the name of a scenario (here: 1,2, 3) and select the affected people for this scenario via a Listbox. The Listbox should appear when the cell next to the “Affected people”-Cell ( B5) is active. After selecting the affected people and selecting another cell then B5 the selected options should be inserted into B5.



What I have already managed to do is to make the code work for Listbox1. The problem now is inserting a new scenario-template (and thus also a new listbox ) via Macro. I don´t know how to change the fixed references in the code into relative references to make the code work for each new listbox.
If you have an Idea how to fix that problem, I would be extremely grateful! :)



[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









share|improve this question




























    0















    I have a problem with Listboxes.
    In the Screenshots below you can see that I have “Templates” in which you can fill in the name of a scenario (here: 1,2, 3) and select the affected people for this scenario via a Listbox. The Listbox should appear when the cell next to the “Affected people”-Cell ( B5) is active. After selecting the affected people and selecting another cell then B5 the selected options should be inserted into B5.



    What I have already managed to do is to make the code work for Listbox1. The problem now is inserting a new scenario-template (and thus also a new listbox ) via Macro. I don´t know how to change the fixed references in the code into relative references to make the code work for each new listbox.
    If you have an Idea how to fix that problem, I would be extremely grateful! :)



    [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









    share|improve this question
























      0












      0








      0








      I have a problem with Listboxes.
      In the Screenshots below you can see that I have “Templates” in which you can fill in the name of a scenario (here: 1,2, 3) and select the affected people for this scenario via a Listbox. The Listbox should appear when the cell next to the “Affected people”-Cell ( B5) is active. After selecting the affected people and selecting another cell then B5 the selected options should be inserted into B5.



      What I have already managed to do is to make the code work for Listbox1. The problem now is inserting a new scenario-template (and thus also a new listbox ) via Macro. I don´t know how to change the fixed references in the code into relative references to make the code work for each new listbox.
      If you have an Idea how to fix that problem, I would be extremely grateful! :)



      [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









      share|improve this question














      I have a problem with Listboxes.
      In the Screenshots below you can see that I have “Templates” in which you can fill in the name of a scenario (here: 1,2, 3) and select the affected people for this scenario via a Listbox. The Listbox should appear when the cell next to the “Affected people”-Cell ( B5) is active. After selecting the affected people and selecting another cell then B5 the selected options should be inserted into B5.



      What I have already managed to do is to make the code work for Listbox1. The problem now is inserting a new scenario-template (and thus also a new listbox ) via Macro. I don´t know how to change the fixed references in the code into relative references to make the code work for each new listbox.
      If you have an Idea how to fix that problem, I would be extremely grateful! :)



      [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






      reference listbox relative






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 16 '18 at 11:10









      Johanna OttJohanna Ott

      11




      11






















          0






          active

          oldest

          votes












          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53336702%2fvba-listbox-with-relative-references%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53336702%2fvba-listbox-with-relative-references%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          27

          Top Tejano songwriter Luis Silva dead of heart attack at 64

          Category:Rhetoric