Access VBA to Excel, add apostrophe [duplicate] - excel

Having a problem with this Error. I am creating a GA and the loop is to assign my fitness value to an array.
some of the variables
Dim Chromolength as integer
Chromolength = varchromolength * aVariables
Dim i as integer, j as integer, counter as integer
Dim Poparr() As Integer
Dim FitValarr() As Integer
the code:
ReDim Poparr(1 To PopSize, 1 To Chromolength)
For i = 1 To PopSize
For j = 1 To Chromolength
If Rnd < 0.5 Then
Poparr(i, j) = 0
Else
Poparr(i, j) = 1
End If
Next j
Next i
For i = 1 To PopSize
j = 1
counter = Chromolength
Do While counter > 0
FitValarr(i) = FitValarr(i) + Poparr(i, counter) * 2 ^ (j - 1)
j = j + 1
counter = counter - 1
Loop
Next i
I am having problems with:
FitValarr(i) = FitValarr(i) + Poparr(i, counter) * 2 ^ (j - 1)
I apologize, I am fairly new to VBA.

An overflow condition arises when you create an integer expression that evaluates to a value larger than can be expressed in a 16-bit signed integer. Given the expression, either the contents of FitValarr(i), or the expression 2^(j-1) could be overflowing. Suggest all the the variables presently declared as Int be changed to Long. Long integers are 32-bit signed values and provide a correspondingly larger range of possible values.

I had the same run time error 6. After much investigation l discovered that mine was a simple 'divide by zero' error.

I set up an integer value to hold Zip codes, and Error 6 events plagued me - until I realized that a zip code of 85338 exceeded the capacity of an int...
While I didn't think of a zip code as a "value" it was nonetheless certainly interpreted as one. I suspect the same could happen with addresses as well as other "non-numeric" numeric values. Changing the variable to a string resolved the problem.
It just didn't occur to me that a zip code was a "numeric value." Lesson learned.

Related

Need help in calculating IRR using custom function in Excel

I am using an excel sheet to track all my investments. In order to calculate my IRR, I need to have the values entered in a specific way for Excel to calculate. So I decided to create a custom function. I will feed this custom function the following values.
Total Investment
Time Period of investment
Final Value of the investment.
I used the following code for creating a custom function. But I get the #VALUE error
Function ROI(fundInvested, timePeriod, finalValue)
eachValue = fundInvested / timePeriod
Dim cashFlow() As Double
Dim n As Integer
For n = 0 To (timePeriod - 1)
cashFlow(n) = -1 * eachValue
Next n
cashFlow(timePeriod) = finalValue
ROI = IRR(cashFlow)
End Function
Where is my formula wrong?
Since you tagged it formula:
=IRR(CHOOSE(INT((ROW($ZZ$1:INDEX($ZZ:$ZZ,B2+1))-1)/B2)+1,-1*A2/B2,C2))
Depending on one's version this may need to be confirmed with Ctrl-Shift-Enter instead of Enter when exiting edit mode.
So you need to reDim it like this:
Function ROI(fundInvested, timePeriod, finalValue)
eachValue = fundInvested / timePeriod
Dim cashFlow() As Double
Dim n As Integer
ReDim cashFlow(0 to timePeriod)
'ReDim cashFlow(timePeriod) is also correct - see #Chris Nielsen comment.
For n = 0 To (timePeriod - 1)
cashFlow(n) = -1 * eachValue
Next n
cashFlow(timePeriod) = finalValue
ROI = IRR(cashFlow)
End Function
Notes
-In absence of Option Base statement, array subscripts start at zero.
-Putting both lower and upper limits in ReDim statement is recommended, but if the lower limit is omitted and only the upper limit is specified, the lower limit is taken from the Option Base currently in operation, in this case zero so
ReDim cashFlow(0 to timePeriod)
and
ReDim cashFlow(timePeriod)
are equivalent.
Try below:
Function ROI(fundInvested, timePeriod, finalValue)
eachValue = fundInvested / timePeriod
Dim cashFlow() As Double
Dim n As Integer
For n = 0 To (timePeriod - 1)
ReDim Preserve cashFlow(n)
cashFlow(n) = -1 * eachValue
Next n
ReDim Preserve cashFlow(timePeriod)
cashFlow(timePeriod) = finalValue
ROI = WorkSheetFunction.IRR(cashFlow)
End Function

Why am I getting this If statement problem?

I am getting the error "End If Without Block If" problem in VBA. Followin is my code. Could someone help me find out the mistake I am making?
Function ProjectedProductionPlan(Coverage As Double, Sales As Variant, ProjectedStock As Double) As Double
Dim count As Integer
Dim ResidualBalance As Double
Dim ProjectedPlan As Double
Dim k As Integer
Dim x As Integer
Dim y As Single
Dim s As Single
count = Sales.count
s = 0
ResidualBalance = ProjectedStock
i = 1
If Coverage < 1 Then
ProjectedPlan = (Sales(i) * Coverage) - ResidualBalance
ElseIf Coverage = 1 Then
ProjectedPlan = Sales(i) - ResidualBalance
Else
For k = 1 To count
Do Until k - Coverage > 0
x = k
y = Coverage - x
s = Sales(k) + s
Loop
Exit For
End If
ProjectedPlan = s + (Sales(x + 1) * y)
End Function
As per my comment:
To get rid of the compile error, you'll need to change Exit For into Next to create an actual iteration whereas Exit For is simply a statement within the loop to Exit the loop. Here is MS-documentation on For...Next loops.
However, you also make use of a Do Until...Loop. As I see your code, repairing the above would immediately throw your code in an infinite loop since no variable (neither k nor coverage) get's adjusted. So my suggestion would be to include an IF statement inside your For...Next loop instead. For example like this:
For k = 1 To count
If k - Coverage > 0 Then
Exit For
Else
x = k
y = Coverage - x
s = Sales(k) + s
End If
Next
I've not checked the rest of your code to see if implementing this is actually what you needed.
Small sidenote: using Integer data type variables is only going to bite you at one point. Use Long instead.

Combines multiple recursive generator vba [duplicate]

I have run into an overflow error in Excel VBA and cannot find my way around it. While Microsoft's documentation indicates that the range for doubles should reach ~1.8E308, I am receiving an overflow error for numbers significantly lower than that threshold. My code is as follows:
Public Function Fixed_Sample_Nums(ByVal n As Long, seed As Long) As Double()
Dim x() As Double, y() As Double, i As Long
ReDim y(1 To n)
ReDim x(1 To n)
x(1) = (CDbl(48271) * seed) Mod CDbl(2 ^ 31 - 1)
For i = 2 To n
x(i) = (CDbl(48271) * CDbl(x(i - 1))) Mod (CDbl(2 ^ 31 - 1))
y(i) = CDbl(x(i)) / CDbl(2 ^ 31 - 1)
Next i
Fixed_Sample_Nums = y
End Function
'I receive the error in the first iteration of the for loop with
'seed equal to any value >= 1 (i.e. w/ seed = 1):
Debug.Print((CDbl(48271) * CDbl(48271)) Mod (CDbl(2 ^ 31 - 1)))
'results in an overflow error
I am attempting to create a pseudo-random number generator that can take in any 'seed' value up to and including 2 ^ 31 - 1. The for loop should be able to iterate at least 9,999 times (i.e. n = 10000). If the overflow error is not encountered within the first few iterations, it most likely will not be encountered for any subsequent iteration.
As you can see, I am converting each integer to a double before any calculation. I am aware of the fact that arrays substantially increase the byte size of the calculation, but that does not appear to be the current issue as I directly copied the example calculation above into the immediate window and still received the overflow error. My attempts to find a solution online have resulted in no avail, so I would really appreciate any input. Thanks in advance!
Try using Chip Pearson's XMod function:
x(i) = XMod((CDbl(48271) * seed), CDbl(2 ^ 31 - 1))
As he notes:
You can also get overflow errors in VBA using the Mod operator with
very large numbers. For example,
Dim Number As Double
Dim Divisor As Double
Dim Result As Double
Number = 2 ^ 31
Divisor = 7
Result = Number Mod Divisor ' Overflow error here.
Code for the function:
Function XMod(ByVal Number As Double, ByVal Divisor As Double) As Double
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' XMod
' Performs the same function as Mod but will not overflow
' with very large numbers. Both Mod and integer division ( \ )
' will overflow with very large numbers. XMod will not.
' Existing code like:
' Result = Number Mod Divisor
' should be changed to:
' Result = XMod(Number, Divisor)
' Input values that are not integers are truncated to integers. Negative
' numbers are converted to postive numbers.
' This can be used in VBA code and can be called directly from
' a worksheet cell.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Number = Int(Abs(Number))
Divisor = Int(Abs(Divisor))
XMod = Number - (Int(Number / Divisor) * Divisor)
End Function
Additional details:
http://www.cpearson.com/excel/ModFunction.aspx

Overflow Error in Excel VBA with Type Double

I have run into an overflow error in Excel VBA and cannot find my way around it. While Microsoft's documentation indicates that the range for doubles should reach ~1.8E308, I am receiving an overflow error for numbers significantly lower than that threshold. My code is as follows:
Public Function Fixed_Sample_Nums(ByVal n As Long, seed As Long) As Double()
Dim x() As Double, y() As Double, i As Long
ReDim y(1 To n)
ReDim x(1 To n)
x(1) = (CDbl(48271) * seed) Mod CDbl(2 ^ 31 - 1)
For i = 2 To n
x(i) = (CDbl(48271) * CDbl(x(i - 1))) Mod (CDbl(2 ^ 31 - 1))
y(i) = CDbl(x(i)) / CDbl(2 ^ 31 - 1)
Next i
Fixed_Sample_Nums = y
End Function
'I receive the error in the first iteration of the for loop with
'seed equal to any value >= 1 (i.e. w/ seed = 1):
Debug.Print((CDbl(48271) * CDbl(48271)) Mod (CDbl(2 ^ 31 - 1)))
'results in an overflow error
I am attempting to create a pseudo-random number generator that can take in any 'seed' value up to and including 2 ^ 31 - 1. The for loop should be able to iterate at least 9,999 times (i.e. n = 10000). If the overflow error is not encountered within the first few iterations, it most likely will not be encountered for any subsequent iteration.
As you can see, I am converting each integer to a double before any calculation. I am aware of the fact that arrays substantially increase the byte size of the calculation, but that does not appear to be the current issue as I directly copied the example calculation above into the immediate window and still received the overflow error. My attempts to find a solution online have resulted in no avail, so I would really appreciate any input. Thanks in advance!
Try using Chip Pearson's XMod function:
x(i) = XMod((CDbl(48271) * seed), CDbl(2 ^ 31 - 1))
As he notes:
You can also get overflow errors in VBA using the Mod operator with
very large numbers. For example,
Dim Number As Double
Dim Divisor As Double
Dim Result As Double
Number = 2 ^ 31
Divisor = 7
Result = Number Mod Divisor ' Overflow error here.
Code for the function:
Function XMod(ByVal Number As Double, ByVal Divisor As Double) As Double
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' XMod
' Performs the same function as Mod but will not overflow
' with very large numbers. Both Mod and integer division ( \ )
' will overflow with very large numbers. XMod will not.
' Existing code like:
' Result = Number Mod Divisor
' should be changed to:
' Result = XMod(Number, Divisor)
' Input values that are not integers are truncated to integers. Negative
' numbers are converted to postive numbers.
' This can be used in VBA code and can be called directly from
' a worksheet cell.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Number = Int(Abs(Number))
Divisor = Int(Abs(Divisor))
XMod = Number - (Int(Number / Divisor) * Divisor)
End Function
Additional details:
http://www.cpearson.com/excel/ModFunction.aspx

Issues with VBA - Run-time error '6': Overflow

I am stuck with this issue on VBA...
I need to create combinations but when N>22 and K=5, VBA is returning Run-time error '6': Overflow.
I've tried to fix this by saving the file with .xlxs but the problem still remains.
So I've tried to switch the variables to 32-bit but I honestly don't know how to modify the code.
Please find it here below:
Public col(100), r, n, nr As Integer
Function comb(k)
col(k) = col(k - 1)
While col(k) < n - r + k
col(k) = col(k) + 1
If k < r Then
comb (k + 1)
Else
nr = nr + 1
For i = 1 To r
Cells(nr, i) = col(i)
Next
End If
Wend
End Function
Public col(100), r, n, nr As Integer
You must be assigning a higher value than an Integer can handle. Use Single, or for debugging reasons, declare them individually inside the function that way if the error persist it will highlight the variable with the error.
Do not use double as the output may require format, double = decimals, etc (as stated by enderland in the comments).
The numbers are becoming too big for the int or long to handle. for small numbers you can use the direct formula, and for larger numbers you can use either Stirling's approximation or Ramanujan's formula for factorial and store it in a double
http://www.johndcook.com/blog/2012/09/25/ramanujans-factorial-approximation/

Resources