How to pass a selection in a Form to a variable in VBA











up vote
0
down vote

favorite












I'm having some trouble with the following activity: I need to pass a selection of 5 different drivers (STIMA, STIMA_SEDE, LOTTO, STORICO, UNIFORME) done in a form to a variable in VBA and search the corresponding column in my worksheet.



So far I tried two ways, unfortunately without results:



First way:



    Private Sub UserForm_Initialize()

Dim rngDriver As Range
Dim ws As Worksheet

Set ws = Worksheets("Data validation")

For Each rngDriver In ws.Range("Drivers")
Me.cbDriver.AddItem rngDriver.Value
Next rngDriver

End Sub

Private Sub OKbtn_Click()
Dim destValues() As Variant, driverValues() As Variant, totalDriver As Double
Dim totalValue As Double, offsetDriverCol As Integer, deltaPerc() As Double
Dim i As Long, j As Long, k As Long, decimalPart As Double, driverRange As Range
Dim driverCol As Integer, modCol As Integer, DestRange As Range
...
driverCol = 2
Do While Cells(23, driverCol).Value <> frmDistribuisci.cbDriver.Value
driverCol = driverCol + 1
Loop


Second way:



...
If frmDistribuisci.cbDriver.Value Is LOTTO Then
driverCol = 2
Do While Cells(23, driverCol).Value <> "Lotto"
driverCol = driverCol + 1
Loop
End If
'Identificazione colonna driver Stima
If frmDistribuisci.cbDriver.Value Is STIMA Then
driverCol = 2
Do While Cells(23, driverCol).Value <> "Stima"
driverCol = driverCol + 1
Loop
End If
'Identificazione colonna driver Stima da sede
If frmDistribuisci.cbDriver.Value Is STIMA_SEDE Then
driverCol = 2
Do While Cells(23, driverCol).Value <> "Stima da sede"
driverCol = driverCol + 1
Loop
End If
'Identificazione colonna driver Storico
If frmDistribuisci.cbDriver.Value Is STORICO Then
driverCol = 2
Do While Cells(23, driverCol).Value <> "Storico"
driverCol = driverCol + 1
Loop
End If


Debugging this code it seems to skip the If-steps as well as the first solution, so I'm stuck...



Has anyone an idea of how to do this? It would be highly appreciated, thanks in advance!










share|improve this question
























  • The syntax of your If statements should be in the format If … = … Then like If frmDistribuisci.cbDriver.Value = "LOTTO" Then otherwise it assumes that LOTTO is a variable not a string.
    – Pᴇʜ
    Nov 22 at 15:18








  • 1




    Your Do loops will run off of the Worksheet if you don't find the value you're looking for.
    – Comintern
    Nov 22 at 15:20








  • 1




    Damn, it was that simple! xD Thank you Pᴇʜ!
    – Nik
    Nov 22 at 15:27






  • 1




    @CloseAsTypo voter: this isn't a mere typo, it's a major misunderstanding of language fundamentals, adequately answered below.
    – Mathieu Guindon
    Nov 22 at 15:57















up vote
0
down vote

favorite












I'm having some trouble with the following activity: I need to pass a selection of 5 different drivers (STIMA, STIMA_SEDE, LOTTO, STORICO, UNIFORME) done in a form to a variable in VBA and search the corresponding column in my worksheet.



So far I tried two ways, unfortunately without results:



First way:



    Private Sub UserForm_Initialize()

Dim rngDriver As Range
Dim ws As Worksheet

Set ws = Worksheets("Data validation")

For Each rngDriver In ws.Range("Drivers")
Me.cbDriver.AddItem rngDriver.Value
Next rngDriver

End Sub

Private Sub OKbtn_Click()
Dim destValues() As Variant, driverValues() As Variant, totalDriver As Double
Dim totalValue As Double, offsetDriverCol As Integer, deltaPerc() As Double
Dim i As Long, j As Long, k As Long, decimalPart As Double, driverRange As Range
Dim driverCol As Integer, modCol As Integer, DestRange As Range
...
driverCol = 2
Do While Cells(23, driverCol).Value <> frmDistribuisci.cbDriver.Value
driverCol = driverCol + 1
Loop


Second way:



...
If frmDistribuisci.cbDriver.Value Is LOTTO Then
driverCol = 2
Do While Cells(23, driverCol).Value <> "Lotto"
driverCol = driverCol + 1
Loop
End If
'Identificazione colonna driver Stima
If frmDistribuisci.cbDriver.Value Is STIMA Then
driverCol = 2
Do While Cells(23, driverCol).Value <> "Stima"
driverCol = driverCol + 1
Loop
End If
'Identificazione colonna driver Stima da sede
If frmDistribuisci.cbDriver.Value Is STIMA_SEDE Then
driverCol = 2
Do While Cells(23, driverCol).Value <> "Stima da sede"
driverCol = driverCol + 1
Loop
End If
'Identificazione colonna driver Storico
If frmDistribuisci.cbDriver.Value Is STORICO Then
driverCol = 2
Do While Cells(23, driverCol).Value <> "Storico"
driverCol = driverCol + 1
Loop
End If


Debugging this code it seems to skip the If-steps as well as the first solution, so I'm stuck...



Has anyone an idea of how to do this? It would be highly appreciated, thanks in advance!










share|improve this question
























  • The syntax of your If statements should be in the format If … = … Then like If frmDistribuisci.cbDriver.Value = "LOTTO" Then otherwise it assumes that LOTTO is a variable not a string.
    – Pᴇʜ
    Nov 22 at 15:18








  • 1




    Your Do loops will run off of the Worksheet if you don't find the value you're looking for.
    – Comintern
    Nov 22 at 15:20








  • 1




    Damn, it was that simple! xD Thank you Pᴇʜ!
    – Nik
    Nov 22 at 15:27






  • 1




    @CloseAsTypo voter: this isn't a mere typo, it's a major misunderstanding of language fundamentals, adequately answered below.
    – Mathieu Guindon
    Nov 22 at 15:57













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I'm having some trouble with the following activity: I need to pass a selection of 5 different drivers (STIMA, STIMA_SEDE, LOTTO, STORICO, UNIFORME) done in a form to a variable in VBA and search the corresponding column in my worksheet.



So far I tried two ways, unfortunately without results:



First way:



    Private Sub UserForm_Initialize()

Dim rngDriver As Range
Dim ws As Worksheet

Set ws = Worksheets("Data validation")

For Each rngDriver In ws.Range("Drivers")
Me.cbDriver.AddItem rngDriver.Value
Next rngDriver

End Sub

Private Sub OKbtn_Click()
Dim destValues() As Variant, driverValues() As Variant, totalDriver As Double
Dim totalValue As Double, offsetDriverCol As Integer, deltaPerc() As Double
Dim i As Long, j As Long, k As Long, decimalPart As Double, driverRange As Range
Dim driverCol As Integer, modCol As Integer, DestRange As Range
...
driverCol = 2
Do While Cells(23, driverCol).Value <> frmDistribuisci.cbDriver.Value
driverCol = driverCol + 1
Loop


Second way:



...
If frmDistribuisci.cbDriver.Value Is LOTTO Then
driverCol = 2
Do While Cells(23, driverCol).Value <> "Lotto"
driverCol = driverCol + 1
Loop
End If
'Identificazione colonna driver Stima
If frmDistribuisci.cbDriver.Value Is STIMA Then
driverCol = 2
Do While Cells(23, driverCol).Value <> "Stima"
driverCol = driverCol + 1
Loop
End If
'Identificazione colonna driver Stima da sede
If frmDistribuisci.cbDriver.Value Is STIMA_SEDE Then
driverCol = 2
Do While Cells(23, driverCol).Value <> "Stima da sede"
driverCol = driverCol + 1
Loop
End If
'Identificazione colonna driver Storico
If frmDistribuisci.cbDriver.Value Is STORICO Then
driverCol = 2
Do While Cells(23, driverCol).Value <> "Storico"
driverCol = driverCol + 1
Loop
End If


Debugging this code it seems to skip the If-steps as well as the first solution, so I'm stuck...



Has anyone an idea of how to do this? It would be highly appreciated, thanks in advance!










share|improve this question















I'm having some trouble with the following activity: I need to pass a selection of 5 different drivers (STIMA, STIMA_SEDE, LOTTO, STORICO, UNIFORME) done in a form to a variable in VBA and search the corresponding column in my worksheet.



So far I tried two ways, unfortunately without results:



First way:



    Private Sub UserForm_Initialize()

Dim rngDriver As Range
Dim ws As Worksheet

Set ws = Worksheets("Data validation")

For Each rngDriver In ws.Range("Drivers")
Me.cbDriver.AddItem rngDriver.Value
Next rngDriver

End Sub

Private Sub OKbtn_Click()
Dim destValues() As Variant, driverValues() As Variant, totalDriver As Double
Dim totalValue As Double, offsetDriverCol As Integer, deltaPerc() As Double
Dim i As Long, j As Long, k As Long, decimalPart As Double, driverRange As Range
Dim driverCol As Integer, modCol As Integer, DestRange As Range
...
driverCol = 2
Do While Cells(23, driverCol).Value <> frmDistribuisci.cbDriver.Value
driverCol = driverCol + 1
Loop


Second way:



...
If frmDistribuisci.cbDriver.Value Is LOTTO Then
driverCol = 2
Do While Cells(23, driverCol).Value <> "Lotto"
driverCol = driverCol + 1
Loop
End If
'Identificazione colonna driver Stima
If frmDistribuisci.cbDriver.Value Is STIMA Then
driverCol = 2
Do While Cells(23, driverCol).Value <> "Stima"
driverCol = driverCol + 1
Loop
End If
'Identificazione colonna driver Stima da sede
If frmDistribuisci.cbDriver.Value Is STIMA_SEDE Then
driverCol = 2
Do While Cells(23, driverCol).Value <> "Stima da sede"
driverCol = driverCol + 1
Loop
End If
'Identificazione colonna driver Storico
If frmDistribuisci.cbDriver.Value Is STORICO Then
driverCol = 2
Do While Cells(23, driverCol).Value <> "Storico"
driverCol = driverCol + 1
Loop
End If


Debugging this code it seems to skip the If-steps as well as the first solution, so I'm stuck...



Has anyone an idea of how to do this? It would be highly appreciated, thanks in advance!







excel vba excel-vba






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 at 15:17









Pᴇʜ

20k42650




20k42650










asked Nov 22 at 15:15









Nik

112




112












  • The syntax of your If statements should be in the format If … = … Then like If frmDistribuisci.cbDriver.Value = "LOTTO" Then otherwise it assumes that LOTTO is a variable not a string.
    – Pᴇʜ
    Nov 22 at 15:18








  • 1




    Your Do loops will run off of the Worksheet if you don't find the value you're looking for.
    – Comintern
    Nov 22 at 15:20








  • 1




    Damn, it was that simple! xD Thank you Pᴇʜ!
    – Nik
    Nov 22 at 15:27






  • 1




    @CloseAsTypo voter: this isn't a mere typo, it's a major misunderstanding of language fundamentals, adequately answered below.
    – Mathieu Guindon
    Nov 22 at 15:57


















  • The syntax of your If statements should be in the format If … = … Then like If frmDistribuisci.cbDriver.Value = "LOTTO" Then otherwise it assumes that LOTTO is a variable not a string.
    – Pᴇʜ
    Nov 22 at 15:18








  • 1




    Your Do loops will run off of the Worksheet if you don't find the value you're looking for.
    – Comintern
    Nov 22 at 15:20








  • 1




    Damn, it was that simple! xD Thank you Pᴇʜ!
    – Nik
    Nov 22 at 15:27






  • 1




    @CloseAsTypo voter: this isn't a mere typo, it's a major misunderstanding of language fundamentals, adequately answered below.
    – Mathieu Guindon
    Nov 22 at 15:57
















The syntax of your If statements should be in the format If … = … Then like If frmDistribuisci.cbDriver.Value = "LOTTO" Then otherwise it assumes that LOTTO is a variable not a string.
– Pᴇʜ
Nov 22 at 15:18






The syntax of your If statements should be in the format If … = … Then like If frmDistribuisci.cbDriver.Value = "LOTTO" Then otherwise it assumes that LOTTO is a variable not a string.
– Pᴇʜ
Nov 22 at 15:18






1




1




Your Do loops will run off of the Worksheet if you don't find the value you're looking for.
– Comintern
Nov 22 at 15:20






Your Do loops will run off of the Worksheet if you don't find the value you're looking for.
– Comintern
Nov 22 at 15:20






1




1




Damn, it was that simple! xD Thank you Pᴇʜ!
– Nik
Nov 22 at 15:27




Damn, it was that simple! xD Thank you Pᴇʜ!
– Nik
Nov 22 at 15:27




1




1




@CloseAsTypo voter: this isn't a mere typo, it's a major misunderstanding of language fundamentals, adequately answered below.
– Mathieu Guindon
Nov 22 at 15:57




@CloseAsTypo voter: this isn't a mere typo, it's a major misunderstanding of language fundamentals, adequately answered below.
– Mathieu Guindon
Nov 22 at 15:57












1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










This is no valid syntax for a value comparison:



If frmDistribuisci.cbDriver.Value Is LOTTO Then


It should be If … = … Then and LOTTO is considered as a type/class (because of the Is operator, which is used for something else entirely), not as the intended string. String literals must be delimited with double-quotes:



If frmDistribuisci.cbDriver.Value = "LOTTO" Then


That should work as intended.






share|improve this answer



















  • 2




    The fact that this code even runs, means OP doesn't have "require variable declarations" setting enabled, and Option Explicit is missing. This is very likely hiding many more bugs.
    – Mathieu Guindon
    Nov 22 at 15:41










  • @MathieuGuindon Yes, good point. I forgot to recommend to activate Option Explicit: In the VBA editor go to ToolsOptionsRequire Variable Declaration.
    – Pᴇʜ
    Nov 22 at 15:43











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',
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%2f53433906%2fhow-to-pass-a-selection-in-a-form-to-a-variable-in-vba%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








up vote
1
down vote



accepted










This is no valid syntax for a value comparison:



If frmDistribuisci.cbDriver.Value Is LOTTO Then


It should be If … = … Then and LOTTO is considered as a type/class (because of the Is operator, which is used for something else entirely), not as the intended string. String literals must be delimited with double-quotes:



If frmDistribuisci.cbDriver.Value = "LOTTO" Then


That should work as intended.






share|improve this answer



















  • 2




    The fact that this code even runs, means OP doesn't have "require variable declarations" setting enabled, and Option Explicit is missing. This is very likely hiding many more bugs.
    – Mathieu Guindon
    Nov 22 at 15:41










  • @MathieuGuindon Yes, good point. I forgot to recommend to activate Option Explicit: In the VBA editor go to ToolsOptionsRequire Variable Declaration.
    – Pᴇʜ
    Nov 22 at 15:43















up vote
1
down vote



accepted










This is no valid syntax for a value comparison:



If frmDistribuisci.cbDriver.Value Is LOTTO Then


It should be If … = … Then and LOTTO is considered as a type/class (because of the Is operator, which is used for something else entirely), not as the intended string. String literals must be delimited with double-quotes:



If frmDistribuisci.cbDriver.Value = "LOTTO" Then


That should work as intended.






share|improve this answer



















  • 2




    The fact that this code even runs, means OP doesn't have "require variable declarations" setting enabled, and Option Explicit is missing. This is very likely hiding many more bugs.
    – Mathieu Guindon
    Nov 22 at 15:41










  • @MathieuGuindon Yes, good point. I forgot to recommend to activate Option Explicit: In the VBA editor go to ToolsOptionsRequire Variable Declaration.
    – Pᴇʜ
    Nov 22 at 15:43













up vote
1
down vote



accepted







up vote
1
down vote



accepted






This is no valid syntax for a value comparison:



If frmDistribuisci.cbDriver.Value Is LOTTO Then


It should be If … = … Then and LOTTO is considered as a type/class (because of the Is operator, which is used for something else entirely), not as the intended string. String literals must be delimited with double-quotes:



If frmDistribuisci.cbDriver.Value = "LOTTO" Then


That should work as intended.






share|improve this answer














This is no valid syntax for a value comparison:



If frmDistribuisci.cbDriver.Value Is LOTTO Then


It should be If … = … Then and LOTTO is considered as a type/class (because of the Is operator, which is used for something else entirely), not as the intended string. String literals must be delimited with double-quotes:



If frmDistribuisci.cbDriver.Value = "LOTTO" Then


That should work as intended.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 22 at 16:08









Mathieu Guindon

40.4k762139




40.4k762139










answered Nov 22 at 15:39









Pᴇʜ

20k42650




20k42650








  • 2




    The fact that this code even runs, means OP doesn't have "require variable declarations" setting enabled, and Option Explicit is missing. This is very likely hiding many more bugs.
    – Mathieu Guindon
    Nov 22 at 15:41










  • @MathieuGuindon Yes, good point. I forgot to recommend to activate Option Explicit: In the VBA editor go to ToolsOptionsRequire Variable Declaration.
    – Pᴇʜ
    Nov 22 at 15:43














  • 2




    The fact that this code even runs, means OP doesn't have "require variable declarations" setting enabled, and Option Explicit is missing. This is very likely hiding many more bugs.
    – Mathieu Guindon
    Nov 22 at 15:41










  • @MathieuGuindon Yes, good point. I forgot to recommend to activate Option Explicit: In the VBA editor go to ToolsOptionsRequire Variable Declaration.
    – Pᴇʜ
    Nov 22 at 15:43








2




2




The fact that this code even runs, means OP doesn't have "require variable declarations" setting enabled, and Option Explicit is missing. This is very likely hiding many more bugs.
– Mathieu Guindon
Nov 22 at 15:41




The fact that this code even runs, means OP doesn't have "require variable declarations" setting enabled, and Option Explicit is missing. This is very likely hiding many more bugs.
– Mathieu Guindon
Nov 22 at 15:41












@MathieuGuindon Yes, good point. I forgot to recommend to activate Option Explicit: In the VBA editor go to ToolsOptionsRequire Variable Declaration.
– Pᴇʜ
Nov 22 at 15:43




@MathieuGuindon Yes, good point. I forgot to recommend to activate Option Explicit: In the VBA editor go to ToolsOptionsRequire Variable Declaration.
– Pᴇʜ
Nov 22 at 15:43


















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.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • 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%2f53433906%2fhow-to-pass-a-selection-in-a-form-to-a-variable-in-vba%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

Trompette piccolo

How do I get these specific pathlines to nodes?

What visual should I use to simply compare current year value vs last year in Power BI desktop