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!
excel vba excel-vba
add a comment |
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!
excel vba excel-vba
The syntax of yourIf
statements should be in the formatIf … = … Then
likeIf frmDistribuisci.cbDriver.Value = "LOTTO" Then
otherwise it assumes thatLOTTO
is a variable not a string.
– Pᴇʜ
Nov 22 at 15:18
1
YourDo
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
add a comment |
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!
excel vba excel-vba
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
excel vba excel-vba
edited Nov 22 at 15:17
Pᴇʜ
20k42650
20k42650
asked Nov 22 at 15:15
Nik
112
112
The syntax of yourIf
statements should be in the formatIf … = … Then
likeIf frmDistribuisci.cbDriver.Value = "LOTTO" Then
otherwise it assumes thatLOTTO
is a variable not a string.
– Pᴇʜ
Nov 22 at 15:18
1
YourDo
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
add a comment |
The syntax of yourIf
statements should be in the formatIf … = … Then
likeIf frmDistribuisci.cbDriver.Value = "LOTTO" Then
otherwise it assumes thatLOTTO
is a variable not a string.
– Pᴇʜ
Nov 22 at 15:18
1
YourDo
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
add a comment |
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.
2
The fact that this code even runs, means OP doesn't have "require variable declarations" setting enabled, andOption 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 activateOption Explicit
: In the VBA editor go to Tools › Options › Require Variable Declaration.
– Pᴇʜ
Nov 22 at 15:43
add a comment |
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.
2
The fact that this code even runs, means OP doesn't have "require variable declarations" setting enabled, andOption 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 activateOption Explicit
: In the VBA editor go to Tools › Options › Require Variable Declaration.
– Pᴇʜ
Nov 22 at 15:43
add a comment |
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.
2
The fact that this code even runs, means OP doesn't have "require variable declarations" setting enabled, andOption 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 activateOption Explicit
: In the VBA editor go to Tools › Options › Require Variable Declaration.
– Pᴇʜ
Nov 22 at 15:43
add a comment |
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.
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.
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, andOption 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 activateOption Explicit
: In the VBA editor go to Tools › Options › Require Variable Declaration.
– Pᴇʜ
Nov 22 at 15:43
add a comment |
2
The fact that this code even runs, means OP doesn't have "require variable declarations" setting enabled, andOption 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 activateOption Explicit
: In the VBA editor go to Tools › Options › Require 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 Tools › Options › Require 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 Tools › Options › Require Variable Declaration.– Pᴇʜ
Nov 22 at 15:43
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
The syntax of your
If
statements should be in the formatIf … = … Then
likeIf frmDistribuisci.cbDriver.Value = "LOTTO" Then
otherwise it assumes thatLOTTO
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