vba convert string to int with string not a number












0














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:



enter image description here



-



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










share|improve this question




















  • 1




    You are trying to do calculations on the letter "b".
    – SJR
    Nov 12 '18 at 13:16
















0














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:



enter image description here



-



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










share|improve this question




















  • 1




    You are trying to do calculations on the letter "b".
    – SJR
    Nov 12 '18 at 13:16














0












0








0







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:



enter image description here



-



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










share|improve this question















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:



enter image description here



-



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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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














  • 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












2 Answers
2






active

oldest

votes


















0














Convert them back to the variables.



    ValCellY = IIf(Cells(StartNumber, 2).Value = "b", b, d)
ValCellZ = IIf(Cells(StartNumber, 3).Value = "b", b, d)





share|improve this answer





















  • 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



















0














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





share|improve this answer





















  • 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











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%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









0














Convert them back to the variables.



    ValCellY = IIf(Cells(StartNumber, 2).Value = "b", b, d)
ValCellZ = IIf(Cells(StartNumber, 3).Value = "b", b, d)





share|improve this answer





















  • 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
















0














Convert them back to the variables.



    ValCellY = IIf(Cells(StartNumber, 2).Value = "b", b, d)
ValCellZ = IIf(Cells(StartNumber, 3).Value = "b", b, d)





share|improve this answer





















  • 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














0












0








0






Convert them back to the variables.



    ValCellY = IIf(Cells(StartNumber, 2).Value = "b", b, d)
ValCellZ = IIf(Cells(StartNumber, 3).Value = "b", b, d)





share|improve this answer












Convert them back to the variables.



    ValCellY = IIf(Cells(StartNumber, 2).Value = "b", b, d)
ValCellZ = IIf(Cells(StartNumber, 3).Value = "b", b, d)






share|improve this answer












share|improve this answer



share|improve this answer










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


















  • 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













0














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





share|improve this answer





















  • 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
















0














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





share|improve this answer





















  • 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














0












0








0






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





share|improve this answer












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






share|improve this answer












share|improve this answer



share|improve this answer










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


















  • 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


















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%2f53262826%2fvba-convert-string-to-int-with-string-not-a-number%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

Full-time equivalent

Bicuculline

さくらももこ