Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
How can I get random numbers generated by a log normal distribution in VBA?
I used
Dim upper As Double, lower As Double
' Declare 2 arrays
Dim irr(26) As Double
Dim lirr(26) As Double
' set the upper and lower band
upper = 0.125
lower = 0.02
...
lirr(i ) = WorksheetFunction.LogInv(Rand, (upper - lower), lower) + lower
However I get an error.
Option Base 1
Public Function LOGINV_SAMPLE(Optional n As Integer = 1, Optional md As Variant = 1, Optional stdv As Variant = 0) As Variant
'#Execute this as array formula i.e Cnt+Shft+Enter
Dim x(), i, p As Variant
ReDim Preserve x(n)
For i = 1 To n Step 1
Randomize
p = Rnd()
x(i) = Application.LogNorm_Inv(p, stdv, md)
Next i
LOGINV_SAMPLE = Application.Transpose(x)
End Function
This works.
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
Hi I want to generate n random numbers in VBA so that their sum must be 100 and they must be placed in such a way that 7 numbers come on first row then next 7 on second row and so on. I have tried so many solutions given on internet but nothing works for me. So anyone can suggest a solution or give me a link for the solution.
Thanks in advance.
Say we want to make 10 numbers that sum to 100. In A1 enter:
=RANDBETWEEN(1,50)
and in A2 enter:
=IFERROR(RANDBETWEEN(1,100-SUM($A$1:A1)),0)
copy A2 down through A10:
EDIT#1:
To use a macro, try:
Sub randomality()
Dim ary(1 To 10) As Double, zum As Double
Dim i As Long
Randomize
zum = 0
For i = 1 To 10
ary(i) = Rnd
zum = zum + ary(i)
Next i
For i = 1 To 10
ary(i) = ary(i) / zum
Next i
With Application.WorksheetFunction
For i = 1 To 10
Cells(i, "D").Value = Round(100 * ary(i), 0)
Next i
Cells(10, "D").Value = 100 - .Sum(Range("D1:D9"))
End With
End Sub
This puts the values in D1 through D10
The below code will do what you have asked but you have more questions you need to check over: -
Public Sub Sample()
Dim AryNumbers() As Long
Dim LngCounter As Long
ReDim AryNumbers(0)
Randomize
Do Until LngCounter = 100
AryNumbers(UBound(AryNumbers, 1)) = Int(10 * Rnd + 1)
If (LngCounter + AryNumbers(UBound(AryNumbers, 1))) > 100 Then
AryNumbers(UBound(AryNumbers, 1)) = 100 - LngCounter
Else
LngCounter = LngCounter + AryNumbers(UBound(AryNumbers, 1))
ReDim Preserve AryNumbers(UBound(AryNumbers, 1) + 1)
End If
Loop
End Sub
You didn't specify where the numbers are to be stored or if the are to be whole numbers, I have provided them in whole numbers in an array to answer the question.
You need to consider who random the number is, if you looked on the internet you'll know this is a difficult question
I honoured the extra requirement in the comments section of being between 1 and 10.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
How to create a combination generator where order does not matter but being limited from a specific range of Sum? Using Excel VBA macro. Pls help ive been trying to solve this for years not really good at excel vba.. need actual precise codes..
Here is a very simple example that uses an incrementing binary pattern to generate combinations of a set of items.
The items can be either numbers or text values. I am using column B as a "helper column" to hold the binary pattern, but an array could be substituted.Place your items in column A and run this short macro:
Sub Generate()
Dim i As Long, s As String
Dim j As Long, K As Long, N As Long
Dim wf As WorksheetFunction
Dim answer As String
Set wf = Application.WorksheetFunction
K = 1
N = Cells(Rows.Count, "A").End(xlUp).Row
For i = 1 To (2 ^ N) - 1
s = wf.Dec2Bin(i, N)
For j = 1 To N
Cells(j, 2).Value = Val(Mid(s, j, 1))
Next j
answer = ""
For j = 1 To N
If Cells(j, 2) = 1 Then answer = answer & "," & Cells(j, 1)
Next j
Cells(K, 3) = Mid(answer, 2)
K = K + 1
Next i
End Sub
For example:
Because there are (2^N)-1 combinations for N items, there is a practical limit to the number of items that can be placed in column A.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I have a to sum the values which are adjacent to "Leave" valued cell.These values are like 4.54545454545455 and on.
Since "leave" comes intermittently I use condition and accordingly store values from adjacent cell in redefined Integer array(Redim based on the number of occurrences of "Leave". Now I am summing that array here lies the problem as sum is coming up something like 13.409091E-02 and only ULong(which does not work in my VBA editor) can store values like this as much I researched and know. So, Datatypes like Long, Double are not able to store such a value.Which is giving a sum not asked. Is there any way this could work or Is there any other way such that I can check intermittent adjacent values to"Leave"and sum them.
Dim leavearray() As Long <br/>dim xsum as long<br/>dim counter3 as Integer<br/>dim colum as Integer<br/>dim ro as Integer<br/>dim ctr as Integer<br/>dim change as Integer
For counter3 = colum To ro<br/>
If Cells(counter3, 29) = "Leave" Then<br/>
ctr = ctr + 1<br/>
ReDim leavearray(ctr - 1)<br/>
Else<br/>
End If<br/>
Next counter3<br/><br/>For counter = colum To ro<br/>
If Cells(counter, 29) = "Leave" Then<br/>
change = change + 1<br/>
leavearray(change - 1) = Cells(counter, 30).Value 'stores value like 3.409091E-02 in array as and when "leave" occurs in its adjacent cell.<br/>
Else<br/>
End If<br/>
If counter = ro Then<br/>
xsum = WorksheetFunction.Sum(leavearray)'It Stores 0 where the problem lies.
If you have a number and wants only the integer part of it, you should use the command Int(), if you want it to be rounded, use CInt(), as shown in the code below:
Dim integerResult As Integer
Dim singleVariable As Single
singleVariable = 4.65
integerResult = Int(singleVariable)
MsgBox (integerResult)
integerResult = CInt(singleVariable)
MsgBox (integerResult)
The first MessageBox will show you 4, and the second MessageBox will show you 5.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am confused as how to create interest rate input as an argument
When I try to list Dim Rate as single an error box says duplicate argument
The purpose of this function is to calculate NPV for a set of cash flows (myarray)
Public Function myNPV(rate As Single, r As Range) As Single
Dim myArray(64) As Double
Dim i As Integer
Dim sum As Double
For i = 0 To n
Next i
For Each elem In r
myArray(i) = elem.Value
myArray(i) = myArray(i) / ((1 + rate / 100) ^ i)
i = i + 1
Next elem
End Function
n is zero. The For loop does nothing.
myNPV never gets assigned a value in the UDF
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 8 years ago.
Improve this question
I'm making a program that will give you the result of a sum when the input is unstructured. Please see the following:
Input:
whats 20 + 26
Output:
46
I already know how to search for text in a string (If text.IndexOf("hello") >= 0 Then)...
I have no idea how to use the two integers for an addition equation.
Here's a quick example of how to start doing this with regular expressions. This won't be bulletproof against anything you throw at it, but it should make for a good start.
Be sure to add : Imports System.Text.RegularExpressions to your .vb file
'This string is an example input. It demonstrates that the method below
'will find the sum "2345+ 3256236" but will skip over things like
' if + .04g
' 1.23 + 4
' etc...
Dim input As String = _
"aoe%rdes 2345+ 3256236 if + .04g rcfo 8 3 . 1.23 + 4 the#r whuts"
Dim pattern As String = "\s\d+\s*\+\s*\d+(?=\s|$)"
For Each _match As Match In Regex.Matches(input, pattern)
Dim a = _match.Value.Split("+"c) 'Match extracts "2345+ 3256325"
Dim x As Integer
Dim y As Integer
If Integer.TryParse(a(0), x) AndAlso Integer.TryParse(a(1), y) Then
Console.WriteLine("Found the Sum : " & x & " + " & y)
Console.WriteLine("Sum is : " & x + y)
Else
Console.WriteLine("Match failed to parse")
End If
Next
The regular expression can be broken down as
\s '(definitely) one whitespace
\d+ 'followed by any number of integer digits
\s* 'followed by (possibly) a whitespace
\+ 'followed by (definitely) a "+"
\s* 'followed by (possibly) a whitespace
\d+ 'followed by any number of integer digits
(?=\s|$) 'followed by (definitely) either a whitespace or EOL
Read more here :
Regular Expression Language - Quick Reference
.NET Framework Regular Expressions
Regular Expressions Reference
Parse the two integers into two integer variables - we'll call them x and y. Then add their results.
Dim x as Integer
Dim y as Integer
Dim result as Integer
'parse your integers- you indicate you already know how to do this
'add the results
result = x + y
'now print out the result