I'm a bit new to VBA and don't understand how do I assign specific values into array, not the range. I'm trying to do something like this:
Dim toDel(8) As Integer
Set toDel() = Array(1, 3, 5, 6, 7, 8, 10, 12)
But it returns me error. So can I somehow insert a hole array into array without looping through each element? Like we do it in python just array = array
Consider:
Option Explicit
Sub whatever()
Dim toDel As Variant
toDel = Array(1, 3, 5, 6, 7, 8, 10, 12)
End Sub
Related
I would like all the numbers from 1 to 1000000 to be put into a set in VBA Access for example, {1, 2, 3, 4, 5, 6, 7, 8, ..., 1000000} How can I do this? I need all the numbers to be in this set format so that I can perform set operations with all of those numbers.
I would like to perform set operations like subtracting a list from a list like:
{0, 1, 2, 3, 4, 6, 7, 8, ..., 1000000}- {1, 2, 3}= {4, 5, 6, 7, 8,..., 1000000}
But I need a clever way of getting all the numbers from one to 1000000 as a set.
Seems like you're looking for an array. You'll need to rebuild it with various reductions as you describe, but it should do what you need.
Sub MakeArray()
Dim myArray(1 To 1000000) As Long
Dim i As Long
For i = LBound(myArray) To UBound(myArray)
myArray(i) = i
Next i
End Sub
Is there a way like in MATLAB to assign many values in an array's row?
Let's say you have a matrix M(5,5) and a Vector V = [1, 2, 3, 4, 5]
In Matlab, If you wanted the first row to be filled up with vector V, you would write:
M(1,:) = V
Is there something similar in Excel VBA instead of looping through each column of the first row and assign the value V(j) every time?
You could try something along these lines:
Sub InitializeMatrixWithVector()
Dim V(), M()
ReDim M(1 To 5, 1 To 5)
V = Array(1, 2, 3, 4, 5)
M = Application.Choose([1+(row(1:5)=1)], M, V)
Range("B1").Resize(UBound(M), UBound(M, 2)) = M
End Sub
The square brackets are equivalent to the Evaluate VBA function.
Application.Choose is a WorksheetFunction method that can take and return variant arrays.
Is it possible to have multiple conditions that can evaluate to true in an if statement? Primarily just shorthand to save some time:
Example:
if value = 1 or value = 9 or value = 3 or value = 17 Then return True
Im wondering if there is something to this effect (could not find in MSDN)
if value = 1,9,3,17 Then return True
Can't do that with an IF statement, but you could use:
SELECT CASE value
CASE 1, 3, 9, 17
' Do something
CASE 2, 4
' Do another thing
CASE ELSE
' Do something else
END SELECT
You can also try like:
If (New List(Of Integer)(New Integer() {1, 3, 5, 7, 9}).Contains(value)) Then
Return TRUE
End If
If it's a return statement, you could make it bit shorter like this;
Return value = 1 Or value = 9 Or value = 3 Or value = 17
Also if the values would be in an array, you could use Linq - Any function;
Dim value As Integer = 1
Dim values = New Integer() {1, 9, 3, 17}
Return values.Any(Function(number) number = value)
You can't do what you want exactly within VB, but you can get around it. Here is one method. Put your values into an Array. You can search the Array using the IndexOf method. If the IndexOf returns a number above -1, then the item was found in the array. There are some gotchas that you will want to read about on the MSDN page.
Dim value As Boolean
Dim myList() As Integer = {1, 9, 3, 17}
If Array.IndexOf(myList, 12) > -1 Then
value = True
Else
value = False
End If
Console.WriteLine("Found: {0}", value)
You can also shorten the test to an inline comparison and assignment, removing the need for the If statement above.:
value = (Array.IndexOf(myList, 12) > -1)
Sub omnama()
ActiveSheet.Range(Cells("$1", "$A"), Cells("$39930", "$Q"))._
RemoveDuplicates Columns:=Array(1, 2, 6, 7, 8, 9), Header:=xlYes
End Sub
This is the code to remove the duplicates I have seen and my question is,
I wanna give a variable rather Than a number
cells("$39930","$Q") should be replaced with the below code
cells("$variable"),"$Q")) but gives the error Type mismatch -- run time error 13
How do I make it work?
If i say
cells($variable,"$Q") ' says that invalid character
So how do i put a variable Value for these above code. Please let me know.Thank you in advance
Try something like this:
Sub omnama()
Dim intRow as integer
intRow = 39930
ActiveSheet.Range(Cells("$1", "$A"), Cells("$" & CStr(intRow), "$Q"))._
RemoveDuplicates Columns:=Array(1, 2, 6, 7, 8, 9), Header:=xlYes
End Sub
I just learn how to create an array literal in VB.NET.
Dim MyArray = New Integer() { 1, 2, 3 }
' Or
Dim MyArray() As Integer = { 1, 2, 3 }
' Or
Dim MyArray() = { 1, 2, 3 }
' Or
Dim MyArray() = { 1, 2, "A", "B" }
Now, I want to use A LITERAL ARRAY in a condition (see pseudo-code)
If 1 exists in {1,2,3,4} Then
MsgBox "Exists!"
End If
but I don't know how, seems like you have to assign it to a variable before you can use it in the condition.
Dim MyArray() As Integer = {3, 2, 3}
If (MyArray.Contains(1)) Then
MsgBox("exists!")
Else
MsgBox("does not exist!")
End If
The above code works, but I'm just wondering is there any way to do this without assigning the array literal to variable first?
Thanks in advance!
Use {1,2,3,4}.Contains(1) for this.