Dynamically adding column values based on combo box selection










0















I need your help. It seems what I have written in code does not accomplish what I am trying to do here.



The objective would be to have 2 userform combo boxes one for the (floor) values which are manually added once [3,4,5] and the other combo boxes (offices) in which values are dynamically added based on the selection made in the floor selection box.



Let's say for example that if I chose the value [3] in my floor combo box that the office combo box would contain the following values:



A-01
A-02
A-03
A-04
A-05
A-06
A-07
A-08


I thought this code would work but it doesn't:



'Cells(row, col)

Private Sub floor_Change()
lRow = Sheets("Office Spaces").UsedRange.Rows.Count

With Sheets("Office Spaces")
For i = 2 To lRow
If .Cells(i, 1).Value = UserForm1.floor.Value Then
UserForm1.office.AddItem .Cells(i, 2).Value
End If
Next i
End With
End Sub


Here's what the data looks in my excel sheet:



enter image description here










share|improve this question



















  • 1





    try debug.? UserForm1.floor.Value before, make sure you definitely have what you are expecting. Also, what does go into the combo if anything?

    – Nathan_Sav
    Nov 13 '18 at 16:00







  • 3





    What is the problem? Is it the wrong values or not being populated at all or?

    – Kubie
    Nov 13 '18 at 16:01











  • you should clear your office combo box before adding items. First action in floor_Change should be UserForm1.office.Clear otherwise it will add more and more items to the combo box each time you change the floor.

    – Pᴇʜ
    Nov 13 '18 at 16:19
















0















I need your help. It seems what I have written in code does not accomplish what I am trying to do here.



The objective would be to have 2 userform combo boxes one for the (floor) values which are manually added once [3,4,5] and the other combo boxes (offices) in which values are dynamically added based on the selection made in the floor selection box.



Let's say for example that if I chose the value [3] in my floor combo box that the office combo box would contain the following values:



A-01
A-02
A-03
A-04
A-05
A-06
A-07
A-08


I thought this code would work but it doesn't:



'Cells(row, col)

Private Sub floor_Change()
lRow = Sheets("Office Spaces").UsedRange.Rows.Count

With Sheets("Office Spaces")
For i = 2 To lRow
If .Cells(i, 1).Value = UserForm1.floor.Value Then
UserForm1.office.AddItem .Cells(i, 2).Value
End If
Next i
End With
End Sub


Here's what the data looks in my excel sheet:



enter image description here










share|improve this question



















  • 1





    try debug.? UserForm1.floor.Value before, make sure you definitely have what you are expecting. Also, what does go into the combo if anything?

    – Nathan_Sav
    Nov 13 '18 at 16:00







  • 3





    What is the problem? Is it the wrong values or not being populated at all or?

    – Kubie
    Nov 13 '18 at 16:01











  • you should clear your office combo box before adding items. First action in floor_Change should be UserForm1.office.Clear otherwise it will add more and more items to the combo box each time you change the floor.

    – Pᴇʜ
    Nov 13 '18 at 16:19














0












0








0








I need your help. It seems what I have written in code does not accomplish what I am trying to do here.



The objective would be to have 2 userform combo boxes one for the (floor) values which are manually added once [3,4,5] and the other combo boxes (offices) in which values are dynamically added based on the selection made in the floor selection box.



Let's say for example that if I chose the value [3] in my floor combo box that the office combo box would contain the following values:



A-01
A-02
A-03
A-04
A-05
A-06
A-07
A-08


I thought this code would work but it doesn't:



'Cells(row, col)

Private Sub floor_Change()
lRow = Sheets("Office Spaces").UsedRange.Rows.Count

With Sheets("Office Spaces")
For i = 2 To lRow
If .Cells(i, 1).Value = UserForm1.floor.Value Then
UserForm1.office.AddItem .Cells(i, 2).Value
End If
Next i
End With
End Sub


Here's what the data looks in my excel sheet:



enter image description here










share|improve this question
















I need your help. It seems what I have written in code does not accomplish what I am trying to do here.



The objective would be to have 2 userform combo boxes one for the (floor) values which are manually added once [3,4,5] and the other combo boxes (offices) in which values are dynamically added based on the selection made in the floor selection box.



Let's say for example that if I chose the value [3] in my floor combo box that the office combo box would contain the following values:



A-01
A-02
A-03
A-04
A-05
A-06
A-07
A-08


I thought this code would work but it doesn't:



'Cells(row, col)

Private Sub floor_Change()
lRow = Sheets("Office Spaces").UsedRange.Rows.Count

With Sheets("Office Spaces")
For i = 2 To lRow
If .Cells(i, 1).Value = UserForm1.floor.Value Then
UserForm1.office.AddItem .Cells(i, 2).Value
End If
Next i
End With
End Sub


Here's what the data looks in my excel sheet:



enter image description here







excel vba excel-vba






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 16:14









Pᴇʜ

21.4k42750




21.4k42750










asked Nov 13 '18 at 15:56









Jason KellyJason Kelly

1,07972653




1,07972653







  • 1





    try debug.? UserForm1.floor.Value before, make sure you definitely have what you are expecting. Also, what does go into the combo if anything?

    – Nathan_Sav
    Nov 13 '18 at 16:00







  • 3





    What is the problem? Is it the wrong values or not being populated at all or?

    – Kubie
    Nov 13 '18 at 16:01











  • you should clear your office combo box before adding items. First action in floor_Change should be UserForm1.office.Clear otherwise it will add more and more items to the combo box each time you change the floor.

    – Pᴇʜ
    Nov 13 '18 at 16:19













  • 1





    try debug.? UserForm1.floor.Value before, make sure you definitely have what you are expecting. Also, what does go into the combo if anything?

    – Nathan_Sav
    Nov 13 '18 at 16:00







  • 3





    What is the problem? Is it the wrong values or not being populated at all or?

    – Kubie
    Nov 13 '18 at 16:01











  • you should clear your office combo box before adding items. First action in floor_Change should be UserForm1.office.Clear otherwise it will add more and more items to the combo box each time you change the floor.

    – Pᴇʜ
    Nov 13 '18 at 16:19








1




1





try debug.? UserForm1.floor.Value before, make sure you definitely have what you are expecting. Also, what does go into the combo if anything?

– Nathan_Sav
Nov 13 '18 at 16:00






try debug.? UserForm1.floor.Value before, make sure you definitely have what you are expecting. Also, what does go into the combo if anything?

– Nathan_Sav
Nov 13 '18 at 16:00





3




3





What is the problem? Is it the wrong values or not being populated at all or?

– Kubie
Nov 13 '18 at 16:01





What is the problem? Is it the wrong values or not being populated at all or?

– Kubie
Nov 13 '18 at 16:01













you should clear your office combo box before adding items. First action in floor_Change should be UserForm1.office.Clear otherwise it will add more and more items to the combo box each time you change the floor.

– Pᴇʜ
Nov 13 '18 at 16:19






you should clear your office combo box before adding items. First action in floor_Change should be UserForm1.office.Clear otherwise it will add more and more items to the combo box each time you change the floor.

– Pᴇʜ
Nov 13 '18 at 16:19













1 Answer
1






active

oldest

votes


















0














'Cells(row, col)

Private Sub floor56_Change()

UserForm1.office.Clear

Dim sh
Dim rw

Set sh = Sheets("Office Spaces")

For Each rw In sh.Rows

If sh.Cells(rw.row, 1).Text = UserForm1.floor.Value Then

UserForm1.office.AddItem (sh.Cells(rw.row, 2).Value)

End If

Next rw

End Sub


or



Private Sub floor_Change()

If UserForm1.floor.Value <> "" Then

UserForm1.office.Clear

Dim ws
Set ws = ThisWorkbook.Worksheets("Office Spaces")

Dim rng
Set rng = ws.Range("A:A")

For Each cell In rng

If cell.Text = UserForm1.floor.Value Then

UserForm1.office.AddItem (cell.Offset(0, 1).Value)

End If

Next cell

End If

End Sub





share|improve this answer























  • I recommend never to use Variants if possible (ws, rng). Proper variable type specification is a very good practice. Also I recommend to activate Option Explicit: In the VBA editor go to ToolsOptionsRequire Variable Declaration. And declare all variables. • Also For Each cell In rng where rng is the whole column A will take an unnecessarily long time. Better reduce it to the range where data is in (and omit all the blanks in the end of column A).

    – Pᴇʜ
    Nov 14 '18 at 7:38












  • Also you should explain what you did and why. Code answers without text or explanation in most cases are poor answers.

    – Pᴇʜ
    Nov 14 '18 at 8:18










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%2f53284812%2fdynamically-adding-column-values-based-on-combo-box-selection%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














'Cells(row, col)

Private Sub floor56_Change()

UserForm1.office.Clear

Dim sh
Dim rw

Set sh = Sheets("Office Spaces")

For Each rw In sh.Rows

If sh.Cells(rw.row, 1).Text = UserForm1.floor.Value Then

UserForm1.office.AddItem (sh.Cells(rw.row, 2).Value)

End If

Next rw

End Sub


or



Private Sub floor_Change()

If UserForm1.floor.Value <> "" Then

UserForm1.office.Clear

Dim ws
Set ws = ThisWorkbook.Worksheets("Office Spaces")

Dim rng
Set rng = ws.Range("A:A")

For Each cell In rng

If cell.Text = UserForm1.floor.Value Then

UserForm1.office.AddItem (cell.Offset(0, 1).Value)

End If

Next cell

End If

End Sub





share|improve this answer























  • I recommend never to use Variants if possible (ws, rng). Proper variable type specification is a very good practice. Also I recommend to activate Option Explicit: In the VBA editor go to ToolsOptionsRequire Variable Declaration. And declare all variables. • Also For Each cell In rng where rng is the whole column A will take an unnecessarily long time. Better reduce it to the range where data is in (and omit all the blanks in the end of column A).

    – Pᴇʜ
    Nov 14 '18 at 7:38












  • Also you should explain what you did and why. Code answers without text or explanation in most cases are poor answers.

    – Pᴇʜ
    Nov 14 '18 at 8:18















0














'Cells(row, col)

Private Sub floor56_Change()

UserForm1.office.Clear

Dim sh
Dim rw

Set sh = Sheets("Office Spaces")

For Each rw In sh.Rows

If sh.Cells(rw.row, 1).Text = UserForm1.floor.Value Then

UserForm1.office.AddItem (sh.Cells(rw.row, 2).Value)

End If

Next rw

End Sub


or



Private Sub floor_Change()

If UserForm1.floor.Value <> "" Then

UserForm1.office.Clear

Dim ws
Set ws = ThisWorkbook.Worksheets("Office Spaces")

Dim rng
Set rng = ws.Range("A:A")

For Each cell In rng

If cell.Text = UserForm1.floor.Value Then

UserForm1.office.AddItem (cell.Offset(0, 1).Value)

End If

Next cell

End If

End Sub





share|improve this answer























  • I recommend never to use Variants if possible (ws, rng). Proper variable type specification is a very good practice. Also I recommend to activate Option Explicit: In the VBA editor go to ToolsOptionsRequire Variable Declaration. And declare all variables. • Also For Each cell In rng where rng is the whole column A will take an unnecessarily long time. Better reduce it to the range where data is in (and omit all the blanks in the end of column A).

    – Pᴇʜ
    Nov 14 '18 at 7:38












  • Also you should explain what you did and why. Code answers without text or explanation in most cases are poor answers.

    – Pᴇʜ
    Nov 14 '18 at 8:18













0












0








0







'Cells(row, col)

Private Sub floor56_Change()

UserForm1.office.Clear

Dim sh
Dim rw

Set sh = Sheets("Office Spaces")

For Each rw In sh.Rows

If sh.Cells(rw.row, 1).Text = UserForm1.floor.Value Then

UserForm1.office.AddItem (sh.Cells(rw.row, 2).Value)

End If

Next rw

End Sub


or



Private Sub floor_Change()

If UserForm1.floor.Value <> "" Then

UserForm1.office.Clear

Dim ws
Set ws = ThisWorkbook.Worksheets("Office Spaces")

Dim rng
Set rng = ws.Range("A:A")

For Each cell In rng

If cell.Text = UserForm1.floor.Value Then

UserForm1.office.AddItem (cell.Offset(0, 1).Value)

End If

Next cell

End If

End Sub





share|improve this answer













'Cells(row, col)

Private Sub floor56_Change()

UserForm1.office.Clear

Dim sh
Dim rw

Set sh = Sheets("Office Spaces")

For Each rw In sh.Rows

If sh.Cells(rw.row, 1).Text = UserForm1.floor.Value Then

UserForm1.office.AddItem (sh.Cells(rw.row, 2).Value)

End If

Next rw

End Sub


or



Private Sub floor_Change()

If UserForm1.floor.Value <> "" Then

UserForm1.office.Clear

Dim ws
Set ws = ThisWorkbook.Worksheets("Office Spaces")

Dim rng
Set rng = ws.Range("A:A")

For Each cell In rng

If cell.Text = UserForm1.floor.Value Then

UserForm1.office.AddItem (cell.Offset(0, 1).Value)

End If

Next cell

End If

End Sub






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 13 '18 at 19:08









Jason KellyJason Kelly

1,07972653




1,07972653












  • I recommend never to use Variants if possible (ws, rng). Proper variable type specification is a very good practice. Also I recommend to activate Option Explicit: In the VBA editor go to ToolsOptionsRequire Variable Declaration. And declare all variables. • Also For Each cell In rng where rng is the whole column A will take an unnecessarily long time. Better reduce it to the range where data is in (and omit all the blanks in the end of column A).

    – Pᴇʜ
    Nov 14 '18 at 7:38












  • Also you should explain what you did and why. Code answers without text or explanation in most cases are poor answers.

    – Pᴇʜ
    Nov 14 '18 at 8:18

















  • I recommend never to use Variants if possible (ws, rng). Proper variable type specification is a very good practice. Also I recommend to activate Option Explicit: In the VBA editor go to ToolsOptionsRequire Variable Declaration. And declare all variables. • Also For Each cell In rng where rng is the whole column A will take an unnecessarily long time. Better reduce it to the range where data is in (and omit all the blanks in the end of column A).

    – Pᴇʜ
    Nov 14 '18 at 7:38












  • Also you should explain what you did and why. Code answers without text or explanation in most cases are poor answers.

    – Pᴇʜ
    Nov 14 '18 at 8:18
















I recommend never to use Variants if possible (ws, rng). Proper variable type specification is a very good practice. Also I recommend to activate Option Explicit: In the VBA editor go to ToolsOptionsRequire Variable Declaration. And declare all variables. • Also For Each cell In rng where rng is the whole column A will take an unnecessarily long time. Better reduce it to the range where data is in (and omit all the blanks in the end of column A).

– Pᴇʜ
Nov 14 '18 at 7:38






I recommend never to use Variants if possible (ws, rng). Proper variable type specification is a very good practice. Also I recommend to activate Option Explicit: In the VBA editor go to ToolsOptionsRequire Variable Declaration. And declare all variables. • Also For Each cell In rng where rng is the whole column A will take an unnecessarily long time. Better reduce it to the range where data is in (and omit all the blanks in the end of column A).

– Pᴇʜ
Nov 14 '18 at 7:38














Also you should explain what you did and why. Code answers without text or explanation in most cases are poor answers.

– Pᴇʜ
Nov 14 '18 at 8:18





Also you should explain what you did and why. Code answers without text or explanation in most cases are poor answers.

– Pᴇʜ
Nov 14 '18 at 8:18

















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%2f53284812%2fdynamically-adding-column-values-based-on-combo-box-selection%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







這個網誌中的熱門文章

How to read a connectionString WITH PROVIDER in .NET Core?

Node.js Script on GitHub Pages or Amazon S3

Museum of Modern and Contemporary Art of Trento and Rovereto