Need integer value from string

revcanon

Board Regular
Joined
Mar 26, 2015
Messages
173
I am using this code in a very large vb project, but I isolated the problem area:

Code:
Sub Test()

    Dim CondCount1 As Integer
    Dim CondCount2 As Integer
    Dim vCondCount As String
    Dim AssignCount As Integer
    
    CondCount1 = 26
    CondCount2 = 30

    For i = 1 To Worksheets.Count - 1 Step 1

    With Range("C" & 5 + i)
        .NumberFormat = General
        .Value = "Condition " & i
    End With
    
    vCondCount = "CondCount" & i
    AssignCount = Val(vCondCount)
   
    With Range("D" & 5 + i)
        .NumberFormat = 0
        .Value = AssignCount
    End With
    Next i
    
End Sub

I am able to easily define CondCount1 early on, but when I try to do a loop later in the process to look through many different "CondCount"s I can not seem to get the code to find the value of the "CondCount & i".

In this case, I get "0" for each value of AssignCount as it passes through the loop even though the "vCondCount" is = "CondCount1" and using Val() function to find value of "vCondCount"

Any ideas how to make that "AssignCount" = value of "CondCount1" and then it loop to "CondCount2"....etc.?
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
You've defined vCondCount as a string so it has a zero value. Why not put your conditions in an array something like this:
Code:
Sub Test()
    Dim CondCount() As Integer
    ReDim CondCount(1 To 2)
    CondCount(1) = 26
    CondCount(2) = 30
    For i = 1 To Worksheets.Count - 1 Step 1
        With Range("C" & 5 + i)
            .NumberFormat = General
            .Value = CondCount(i)
        End With
        With Range("D" & 5 + i)
            .NumberFormat = 0
            .Value = CondCount(i)
        End With
    Next i
End Sub
 
Upvote 0
The Val of the string CondCount1 is 0 because it starts with text. The Val function is used to evaluate numbers that may be stored as text. It will only return a number if the string starts with a number.

For example, the string "1 dollar" would return 1, but "Dollars: 1" would return 0.

So what exactly do you mean by the value of "CondCount & i"??
 
Upvote 0
uh... is "because I don't actually know what I'm doing" a good answer to your question?

LOL.

That code is perfect for what I needed. Thank you, Joe, for helping me understand the issue and now I know more than I did an hour ago.

Have a great day/night... wherever you are.
 
Upvote 0
uh... is "because I don't actually know what I'm doing" a good answer to your question?

LOL.

That code is perfect for what I needed. Thank you, Joe, for helping me understand the issue and now I know more than I did an hour ago.

Have a great day/night... wherever you are.
You are welcome - thanks for the reply.
 
Upvote 0

Forum statistics

Threads
1,223,723
Messages
6,174,113
Members
452,544
Latest member
aush

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top