vba convert string to int with string not a number
I need to convert a string into a numerical value.
However, my string has not a number in it. So I define the numerical value of this string in my program.
This is the test I am performing:
-
Sub TestFinal()
Dim Result As Integer
Dim StartNumber As Integer
Dim EndNumber As Integer
Dim b As Variant
Dim d As Variant
Dim ValCellY As String
Dim ValCellZ As String
StartNumber = 2 'First line value of my table
EndNumber = 3 'Last line value of my table
b = 80
d = 20
For StartNumber = 2 To EndNumber 'Begining of my loops
ValCellY = Cells(StartNumber, 2).Value
ValCellZ = Cells(StartNumber, 3).Value
If Cells(StartNumber, 1) = "Yes" Then 'First if condition
Result = 100
End If
If Cells(StartNumber, 1) = "No" Then 'Second if condition
Result = 75 * CInt(ValCellY) + 25 * CInt(ValCellZ) 'I use CInt function to convert my string into a integer
End If
Cells(StartNumber, 4).Value = Result 'I associate the varaible Result to the column D
Next StartNumber 'End of my loops
End Sub
The main issue is that I don't understand why the value b and d are not taken into account.
Moreover a message error : "type mismatch" appear when I run the program.
Does anyone can explain me where is my mistake?
Thanks a lot
excel string excel-vba type-conversion integer
add a comment |
I need to convert a string into a numerical value.
However, my string has not a number in it. So I define the numerical value of this string in my program.
This is the test I am performing:
-
Sub TestFinal()
Dim Result As Integer
Dim StartNumber As Integer
Dim EndNumber As Integer
Dim b As Variant
Dim d As Variant
Dim ValCellY As String
Dim ValCellZ As String
StartNumber = 2 'First line value of my table
EndNumber = 3 'Last line value of my table
b = 80
d = 20
For StartNumber = 2 To EndNumber 'Begining of my loops
ValCellY = Cells(StartNumber, 2).Value
ValCellZ = Cells(StartNumber, 3).Value
If Cells(StartNumber, 1) = "Yes" Then 'First if condition
Result = 100
End If
If Cells(StartNumber, 1) = "No" Then 'Second if condition
Result = 75 * CInt(ValCellY) + 25 * CInt(ValCellZ) 'I use CInt function to convert my string into a integer
End If
Cells(StartNumber, 4).Value = Result 'I associate the varaible Result to the column D
Next StartNumber 'End of my loops
End Sub
The main issue is that I don't understand why the value b and d are not taken into account.
Moreover a message error : "type mismatch" appear when I run the program.
Does anyone can explain me where is my mistake?
Thanks a lot
excel string excel-vba type-conversion integer
1
You are trying to do calculations on the letter "b".
– SJR
Nov 12 '18 at 13:16
add a comment |
I need to convert a string into a numerical value.
However, my string has not a number in it. So I define the numerical value of this string in my program.
This is the test I am performing:
-
Sub TestFinal()
Dim Result As Integer
Dim StartNumber As Integer
Dim EndNumber As Integer
Dim b As Variant
Dim d As Variant
Dim ValCellY As String
Dim ValCellZ As String
StartNumber = 2 'First line value of my table
EndNumber = 3 'Last line value of my table
b = 80
d = 20
For StartNumber = 2 To EndNumber 'Begining of my loops
ValCellY = Cells(StartNumber, 2).Value
ValCellZ = Cells(StartNumber, 3).Value
If Cells(StartNumber, 1) = "Yes" Then 'First if condition
Result = 100
End If
If Cells(StartNumber, 1) = "No" Then 'Second if condition
Result = 75 * CInt(ValCellY) + 25 * CInt(ValCellZ) 'I use CInt function to convert my string into a integer
End If
Cells(StartNumber, 4).Value = Result 'I associate the varaible Result to the column D
Next StartNumber 'End of my loops
End Sub
The main issue is that I don't understand why the value b and d are not taken into account.
Moreover a message error : "type mismatch" appear when I run the program.
Does anyone can explain me where is my mistake?
Thanks a lot
excel string excel-vba type-conversion integer
I need to convert a string into a numerical value.
However, my string has not a number in it. So I define the numerical value of this string in my program.
This is the test I am performing:
-
Sub TestFinal()
Dim Result As Integer
Dim StartNumber As Integer
Dim EndNumber As Integer
Dim b As Variant
Dim d As Variant
Dim ValCellY As String
Dim ValCellZ As String
StartNumber = 2 'First line value of my table
EndNumber = 3 'Last line value of my table
b = 80
d = 20
For StartNumber = 2 To EndNumber 'Begining of my loops
ValCellY = Cells(StartNumber, 2).Value
ValCellZ = Cells(StartNumber, 3).Value
If Cells(StartNumber, 1) = "Yes" Then 'First if condition
Result = 100
End If
If Cells(StartNumber, 1) = "No" Then 'Second if condition
Result = 75 * CInt(ValCellY) + 25 * CInt(ValCellZ) 'I use CInt function to convert my string into a integer
End If
Cells(StartNumber, 4).Value = Result 'I associate the varaible Result to the column D
Next StartNumber 'End of my loops
End Sub
The main issue is that I don't understand why the value b and d are not taken into account.
Moreover a message error : "type mismatch" appear when I run the program.
Does anyone can explain me where is my mistake?
Thanks a lot
excel string excel-vba type-conversion integer
excel string excel-vba type-conversion integer
edited Nov 12 '18 at 14:34
Davesexcel
5,07921936
5,07921936
asked Nov 12 '18 at 13:06
A.MNIERA.MNIER
1
1
1
You are trying to do calculations on the letter "b".
– SJR
Nov 12 '18 at 13:16
add a comment |
1
You are trying to do calculations on the letter "b".
– SJR
Nov 12 '18 at 13:16
1
1
You are trying to do calculations on the letter "b".
– SJR
Nov 12 '18 at 13:16
You are trying to do calculations on the letter "b".
– SJR
Nov 12 '18 at 13:16
add a comment |
2 Answers
2
active
oldest
votes
Convert them back to the variables.
ValCellY = IIf(Cells(StartNumber, 2).Value = "b", b, d)
ValCellZ = IIf(Cells(StartNumber, 3).Value = "b", b, d)
Hello, I am not sure to understand the purpose of this. Can you explain me? Thanks
– A.MNIER
Nov 12 '18 at 15:04
It makes ValCellY & ValCellZ equal to the variables in your code, did you not try it?
– Davesexcel
Nov 12 '18 at 15:40
Yes I tried and it works! Thanks you so much
– A.MNIER
Nov 13 '18 at 12:14
You need to click this as the answer.
– Davesexcel
Nov 13 '18 at 16:02
add a comment |
You are using strings to calculate instead of actual values. You might use if, or select case but if your actual problem is more complex then I suggest using dictionary.
Option Explicit
Sub TestFinal()
Dim Result As Integer
Dim StartNumber As Integer
Dim EndNumber As Integer
Dim b As Variant
Dim d As Variant
Dim ValCellY As String
Dim ValCellZ As String
StartNumber = 2 'First line value of my table
EndNumber = 3 'Last line value of my table
b = 80
d = 20
For StartNumber = 2 To EndNumber 'Begining of my loops
ValCellY = Cells(StartNumber, 2).Value
ValCellZ = Cells(StartNumber, 3).Value
If Cells(StartNumber, 1) = "Yes" Then 'First if condition
Result = 100
End If
If Cells(StartNumber, 1) = "No" Then 'Second if condition
If ValCellY = "b" Then
ValCellY = b
ElseIf ValCellY = "d" Then
ValCellY = d
End If
If ValCellZ = "b" Then
ValCellZ = b
ElseIf ValCellZ = "d" Then
ValCellZ = d
End If
Result = 75 * CInt(ValCellY) + 25 * CInt(ValCellZ) 'I use CInt function to convert my string into a integer
End If
Cells(StartNumber, 4).Value = Result 'I associate the varaible Result to the column D
Next StartNumber 'End of my loops
End Sub
Thanks a lot it is working!
– A.MNIER
Nov 12 '18 at 14:57
Can you explain me what do you mean by dictionary?
– A.MNIER
Nov 12 '18 at 15:17
A dictionary stores unique keys and values assigned to keys. You can create dictionary and later refer to it by the key. Here is a nice article about dictionaries in VBA: excelmacromastery.com/vba-dictionary If my reply was helpful please approve the answer :)
– Pawel Czyz
Nov 12 '18 at 15:20
Thanks a lot, I will study it. It seems very interesting.
– A.MNIER
Nov 13 '18 at 11:27
add a comment |
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
});
}
});
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%2f53262826%2fvba-convert-string-to-int-with-string-not-a-number%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Convert them back to the variables.
ValCellY = IIf(Cells(StartNumber, 2).Value = "b", b, d)
ValCellZ = IIf(Cells(StartNumber, 3).Value = "b", b, d)
Hello, I am not sure to understand the purpose of this. Can you explain me? Thanks
– A.MNIER
Nov 12 '18 at 15:04
It makes ValCellY & ValCellZ equal to the variables in your code, did you not try it?
– Davesexcel
Nov 12 '18 at 15:40
Yes I tried and it works! Thanks you so much
– A.MNIER
Nov 13 '18 at 12:14
You need to click this as the answer.
– Davesexcel
Nov 13 '18 at 16:02
add a comment |
Convert them back to the variables.
ValCellY = IIf(Cells(StartNumber, 2).Value = "b", b, d)
ValCellZ = IIf(Cells(StartNumber, 3).Value = "b", b, d)
Hello, I am not sure to understand the purpose of this. Can you explain me? Thanks
– A.MNIER
Nov 12 '18 at 15:04
It makes ValCellY & ValCellZ equal to the variables in your code, did you not try it?
– Davesexcel
Nov 12 '18 at 15:40
Yes I tried and it works! Thanks you so much
– A.MNIER
Nov 13 '18 at 12:14
You need to click this as the answer.
– Davesexcel
Nov 13 '18 at 16:02
add a comment |
Convert them back to the variables.
ValCellY = IIf(Cells(StartNumber, 2).Value = "b", b, d)
ValCellZ = IIf(Cells(StartNumber, 3).Value = "b", b, d)
Convert them back to the variables.
ValCellY = IIf(Cells(StartNumber, 2).Value = "b", b, d)
ValCellZ = IIf(Cells(StartNumber, 3).Value = "b", b, d)
answered Nov 12 '18 at 13:23
DavesexcelDavesexcel
5,07921936
5,07921936
Hello, I am not sure to understand the purpose of this. Can you explain me? Thanks
– A.MNIER
Nov 12 '18 at 15:04
It makes ValCellY & ValCellZ equal to the variables in your code, did you not try it?
– Davesexcel
Nov 12 '18 at 15:40
Yes I tried and it works! Thanks you so much
– A.MNIER
Nov 13 '18 at 12:14
You need to click this as the answer.
– Davesexcel
Nov 13 '18 at 16:02
add a comment |
Hello, I am not sure to understand the purpose of this. Can you explain me? Thanks
– A.MNIER
Nov 12 '18 at 15:04
It makes ValCellY & ValCellZ equal to the variables in your code, did you not try it?
– Davesexcel
Nov 12 '18 at 15:40
Yes I tried and it works! Thanks you so much
– A.MNIER
Nov 13 '18 at 12:14
You need to click this as the answer.
– Davesexcel
Nov 13 '18 at 16:02
Hello, I am not sure to understand the purpose of this. Can you explain me? Thanks
– A.MNIER
Nov 12 '18 at 15:04
Hello, I am not sure to understand the purpose of this. Can you explain me? Thanks
– A.MNIER
Nov 12 '18 at 15:04
It makes ValCellY & ValCellZ equal to the variables in your code, did you not try it?
– Davesexcel
Nov 12 '18 at 15:40
It makes ValCellY & ValCellZ equal to the variables in your code, did you not try it?
– Davesexcel
Nov 12 '18 at 15:40
Yes I tried and it works! Thanks you so much
– A.MNIER
Nov 13 '18 at 12:14
Yes I tried and it works! Thanks you so much
– A.MNIER
Nov 13 '18 at 12:14
You need to click this as the answer.
– Davesexcel
Nov 13 '18 at 16:02
You need to click this as the answer.
– Davesexcel
Nov 13 '18 at 16:02
add a comment |
You are using strings to calculate instead of actual values. You might use if, or select case but if your actual problem is more complex then I suggest using dictionary.
Option Explicit
Sub TestFinal()
Dim Result As Integer
Dim StartNumber As Integer
Dim EndNumber As Integer
Dim b As Variant
Dim d As Variant
Dim ValCellY As String
Dim ValCellZ As String
StartNumber = 2 'First line value of my table
EndNumber = 3 'Last line value of my table
b = 80
d = 20
For StartNumber = 2 To EndNumber 'Begining of my loops
ValCellY = Cells(StartNumber, 2).Value
ValCellZ = Cells(StartNumber, 3).Value
If Cells(StartNumber, 1) = "Yes" Then 'First if condition
Result = 100
End If
If Cells(StartNumber, 1) = "No" Then 'Second if condition
If ValCellY = "b" Then
ValCellY = b
ElseIf ValCellY = "d" Then
ValCellY = d
End If
If ValCellZ = "b" Then
ValCellZ = b
ElseIf ValCellZ = "d" Then
ValCellZ = d
End If
Result = 75 * CInt(ValCellY) + 25 * CInt(ValCellZ) 'I use CInt function to convert my string into a integer
End If
Cells(StartNumber, 4).Value = Result 'I associate the varaible Result to the column D
Next StartNumber 'End of my loops
End Sub
Thanks a lot it is working!
– A.MNIER
Nov 12 '18 at 14:57
Can you explain me what do you mean by dictionary?
– A.MNIER
Nov 12 '18 at 15:17
A dictionary stores unique keys and values assigned to keys. You can create dictionary and later refer to it by the key. Here is a nice article about dictionaries in VBA: excelmacromastery.com/vba-dictionary If my reply was helpful please approve the answer :)
– Pawel Czyz
Nov 12 '18 at 15:20
Thanks a lot, I will study it. It seems very interesting.
– A.MNIER
Nov 13 '18 at 11:27
add a comment |
You are using strings to calculate instead of actual values. You might use if, or select case but if your actual problem is more complex then I suggest using dictionary.
Option Explicit
Sub TestFinal()
Dim Result As Integer
Dim StartNumber As Integer
Dim EndNumber As Integer
Dim b As Variant
Dim d As Variant
Dim ValCellY As String
Dim ValCellZ As String
StartNumber = 2 'First line value of my table
EndNumber = 3 'Last line value of my table
b = 80
d = 20
For StartNumber = 2 To EndNumber 'Begining of my loops
ValCellY = Cells(StartNumber, 2).Value
ValCellZ = Cells(StartNumber, 3).Value
If Cells(StartNumber, 1) = "Yes" Then 'First if condition
Result = 100
End If
If Cells(StartNumber, 1) = "No" Then 'Second if condition
If ValCellY = "b" Then
ValCellY = b
ElseIf ValCellY = "d" Then
ValCellY = d
End If
If ValCellZ = "b" Then
ValCellZ = b
ElseIf ValCellZ = "d" Then
ValCellZ = d
End If
Result = 75 * CInt(ValCellY) + 25 * CInt(ValCellZ) 'I use CInt function to convert my string into a integer
End If
Cells(StartNumber, 4).Value = Result 'I associate the varaible Result to the column D
Next StartNumber 'End of my loops
End Sub
Thanks a lot it is working!
– A.MNIER
Nov 12 '18 at 14:57
Can you explain me what do you mean by dictionary?
– A.MNIER
Nov 12 '18 at 15:17
A dictionary stores unique keys and values assigned to keys. You can create dictionary and later refer to it by the key. Here is a nice article about dictionaries in VBA: excelmacromastery.com/vba-dictionary If my reply was helpful please approve the answer :)
– Pawel Czyz
Nov 12 '18 at 15:20
Thanks a lot, I will study it. It seems very interesting.
– A.MNIER
Nov 13 '18 at 11:27
add a comment |
You are using strings to calculate instead of actual values. You might use if, or select case but if your actual problem is more complex then I suggest using dictionary.
Option Explicit
Sub TestFinal()
Dim Result As Integer
Dim StartNumber As Integer
Dim EndNumber As Integer
Dim b As Variant
Dim d As Variant
Dim ValCellY As String
Dim ValCellZ As String
StartNumber = 2 'First line value of my table
EndNumber = 3 'Last line value of my table
b = 80
d = 20
For StartNumber = 2 To EndNumber 'Begining of my loops
ValCellY = Cells(StartNumber, 2).Value
ValCellZ = Cells(StartNumber, 3).Value
If Cells(StartNumber, 1) = "Yes" Then 'First if condition
Result = 100
End If
If Cells(StartNumber, 1) = "No" Then 'Second if condition
If ValCellY = "b" Then
ValCellY = b
ElseIf ValCellY = "d" Then
ValCellY = d
End If
If ValCellZ = "b" Then
ValCellZ = b
ElseIf ValCellZ = "d" Then
ValCellZ = d
End If
Result = 75 * CInt(ValCellY) + 25 * CInt(ValCellZ) 'I use CInt function to convert my string into a integer
End If
Cells(StartNumber, 4).Value = Result 'I associate the varaible Result to the column D
Next StartNumber 'End of my loops
End Sub
You are using strings to calculate instead of actual values. You might use if, or select case but if your actual problem is more complex then I suggest using dictionary.
Option Explicit
Sub TestFinal()
Dim Result As Integer
Dim StartNumber As Integer
Dim EndNumber As Integer
Dim b As Variant
Dim d As Variant
Dim ValCellY As String
Dim ValCellZ As String
StartNumber = 2 'First line value of my table
EndNumber = 3 'Last line value of my table
b = 80
d = 20
For StartNumber = 2 To EndNumber 'Begining of my loops
ValCellY = Cells(StartNumber, 2).Value
ValCellZ = Cells(StartNumber, 3).Value
If Cells(StartNumber, 1) = "Yes" Then 'First if condition
Result = 100
End If
If Cells(StartNumber, 1) = "No" Then 'Second if condition
If ValCellY = "b" Then
ValCellY = b
ElseIf ValCellY = "d" Then
ValCellY = d
End If
If ValCellZ = "b" Then
ValCellZ = b
ElseIf ValCellZ = "d" Then
ValCellZ = d
End If
Result = 75 * CInt(ValCellY) + 25 * CInt(ValCellZ) 'I use CInt function to convert my string into a integer
End If
Cells(StartNumber, 4).Value = Result 'I associate the varaible Result to the column D
Next StartNumber 'End of my loops
End Sub
answered Nov 12 '18 at 13:25
Pawel CzyzPawel Czyz
6502517
6502517
Thanks a lot it is working!
– A.MNIER
Nov 12 '18 at 14:57
Can you explain me what do you mean by dictionary?
– A.MNIER
Nov 12 '18 at 15:17
A dictionary stores unique keys and values assigned to keys. You can create dictionary and later refer to it by the key. Here is a nice article about dictionaries in VBA: excelmacromastery.com/vba-dictionary If my reply was helpful please approve the answer :)
– Pawel Czyz
Nov 12 '18 at 15:20
Thanks a lot, I will study it. It seems very interesting.
– A.MNIER
Nov 13 '18 at 11:27
add a comment |
Thanks a lot it is working!
– A.MNIER
Nov 12 '18 at 14:57
Can you explain me what do you mean by dictionary?
– A.MNIER
Nov 12 '18 at 15:17
A dictionary stores unique keys and values assigned to keys. You can create dictionary and later refer to it by the key. Here is a nice article about dictionaries in VBA: excelmacromastery.com/vba-dictionary If my reply was helpful please approve the answer :)
– Pawel Czyz
Nov 12 '18 at 15:20
Thanks a lot, I will study it. It seems very interesting.
– A.MNIER
Nov 13 '18 at 11:27
Thanks a lot it is working!
– A.MNIER
Nov 12 '18 at 14:57
Thanks a lot it is working!
– A.MNIER
Nov 12 '18 at 14:57
Can you explain me what do you mean by dictionary?
– A.MNIER
Nov 12 '18 at 15:17
Can you explain me what do you mean by dictionary?
– A.MNIER
Nov 12 '18 at 15:17
A dictionary stores unique keys and values assigned to keys. You can create dictionary and later refer to it by the key. Here is a nice article about dictionaries in VBA: excelmacromastery.com/vba-dictionary If my reply was helpful please approve the answer :)
– Pawel Czyz
Nov 12 '18 at 15:20
A dictionary stores unique keys and values assigned to keys. You can create dictionary and later refer to it by the key. Here is a nice article about dictionaries in VBA: excelmacromastery.com/vba-dictionary If my reply was helpful please approve the answer :)
– Pawel Czyz
Nov 12 '18 at 15:20
Thanks a lot, I will study it. It seems very interesting.
– A.MNIER
Nov 13 '18 at 11:27
Thanks a lot, I will study it. It seems very interesting.
– A.MNIER
Nov 13 '18 at 11:27
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.
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%2f53262826%2fvba-convert-string-to-int-with-string-not-a-number%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
1
You are trying to do calculations on the letter "b".
– SJR
Nov 12 '18 at 13:16