Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 1 year ago.
Improve this question
I want to hear how others go about this, and why, to see if there is a better method that what I do.
Do you do this:
dim x as long
dim y as long
dim a as string
dim b as integer
dim c as integer
dim d as integer
or this:
dim x as long, y
dim a as string
dim b as integer, c, d
or do you declare them as they are needed within the script? or something else?
I have a few scripts that have 60+ variables, and how to organize them is something I always question
thanks!
dim x as long, y as long
dim a as string
dim b as integer, c as integer, d as integer
You have to specify the type of each variable, otherwise it will be a variant.
Don't forget to set the VBE option to automatically insert OPtion explicit at the top of each module. It will save you time and sweat.
A common practice is to have all your DIMs at the top of the module/function/sub
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 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.
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
hello i search a lot of information in internet but i can not get it work .I tray to add the search results from listbox in a sheet number the sheet name is (Tabelle1 ) listbox name is (lsbWarenausgang) and the CommandButton name is (CommandButton3)
Assuming you want pass the value to the cell C1:
Private Sub CommandButton3_Click()
Worksheets("Tabelle1").Range("C1").Value = lsbWarenausgang.Value
End Sub
For multi-columns ListBox:
Private Sub CommandButton3_Click()
Dim i As Integer
Dim k As Integer
With Me.lsbWarenausgang
For k = 0 To .ListCount - 1
If .Selected(k) = True Then
For i = 1 To 5
Worksheets("Tabelle1").Cells(i, 1) = Me.lsbWarenausgang.List(k, i - 1)
Next i
End If
Next k
End With
End Sub
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I will write something like:
Dim Counter As Integer
Dim a As Integer
Counter = 0
a = 1
Counter-a
But, then VBA appends it to this:
Dim Counter As Integer
Dim a As Integer
Counter = 0
a = 1
Counter a '<---------------This change is what I am confused about
This is my full code so far:
Private Sub SpinButtonM_SpinDown()
Dim Counter As Integer
Dim a As Integer
a = 1
Counter a
UserForm2.Month.Caption = Counter
End Sub
You have to decrement like this:
Counter = Counter - a
If you want to subtract variable a from variable Counter then you need to store the result of that subtraction somewhere. So you might want to introduce another variable like
Dim result as Integer
...
result = Counter - a
msgbox(result)
Or maybe you want to decrease the Counter variable by a, then you need
Counter = Counter - a
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 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 9 years ago.
Improve this question
I am currently creating a chess game a to save me a large amount of if statements I was wondering if there is a way to insert a variable such as x into the label below:
Label1.Text = "MyRedPawns()"
I want it to go in the ( ) similar to what would be used for a Console.Writeline statement.
Edit:
Here is the code:
For x = 1 To 4
If mouseclicklocation.X >= MyRedPawns(x).getposition.X And mouseclicklocation.X <= MyRedPawns(x).getposition.X + 80 Then
If mouseclicklocation.Y >= MyRedPawns(x).getposition.Y And mouseclicklocation.Y <= MyRedPawns(x).getposition.Y + 80 Then
Label1.Text = "MyRedPawns( )"
Piecefound = True
End If
End If
Next x
I want the value of x which is in the for loop to be passed in to the label1.text inside the ()
You can do something like this:
Label1.Text = "MyRedPawns(" & x & ")"
The question is not clear. However, i'll give it a try. I guess you want to insert the value of a variable x into a string. You can use String.Format:
Dim pattern = "MyRedPawns({0})"
Dim xVariable As Int32 = 4711
Dim result = String.Format(pattern, xVariable)
Result: MyRedPawns(4711)
Have a look at the remarks section of MSDN to see how it works.
If we had a variable x = "Hello" to get the text from x into the label we would do:
Label1.Text = x
I'm not sure what MyRedPawns() actually does or returns so I could be wrong but if it is returning a String and takes a parameter then you should be able to do:
Label1.Text = MyRedPawns(x)