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
Related
I can't quite explain it, but this is what I'm trying to do.
Array1 = {1, 5, 7, 4}
0+1 = 1
1+5 = 6
6+7 = 13
13+4 = 17
Array2 = {1, 6, 13, 17}
I still cant figure out how to write the code.
Okay my bad, I was just being stupid. I found the solution to my answer and it turned out to be something really simple.
Thanks to the folks in the comment section.
Dim Array1 As Integer() = {1, 5, 7, 4}
Dim Array2 As Integer() = {0}
For i As Integer = 0 To Array1.Length - 1
'.Add() isnt real, so i made an extension
Array2.Add(Array2(i) + Array1(i))
Next
'to remove the 0 at the start
Array2 = Array2.Skip(1).ToArray
Honestly, I don't know how this did not arrive in my head.
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
I am trying to use "If" logic in Excel VBA but the script is always selecting the 1st option for two areas, even if the statement is not correct.
The point of the code is if the word "Rate" appears at one location on the screen, then certain output should be created. If it finds it in a different location, then a different output should be created. Currently, it is ready the first "If" statement and the pulling that output even though the first statement is not correct.
Using the example below, The word "Rate" appears at row, column (4,20) on the screen but the statement is creating output like it is at (4,16) and therefore pulling the output from (4,27) instead of (4,31). I have switched the order around and it always uses the first option regardless of if it is a correct statement.
I abbreviated the code as much as possible. I had both Dim'd as a string but changed RateName to Variant to see if that would fix the issue but it did not.
Dim RateName As Variant
Dim Curr As String
If Session.FindText("Rate", 4, 16, 4) Then
RateName = Trim(Session.GetDisplayText(4, 27, 20)) 'R name for Amt Add, Amt Off, Buy/Get
ElseIf Session.FindText("Rate", 4, 20, 4) Then
RateName = Trim(Session.GetDisplayText(4, 31, 20)) 'R name for Flat Amount, Pct Off
End If
If Session.FindText("Rate", 4, 16, 4) Then
Curr = Trim(Session.GetDisplayText(4, 58, 3))
ElseIf Session.FindText("Rate", 4, 20, 4) Then
Curr = Trim(Session.GetDisplayText(4, 62, 3))
End If
Not sure what I am doing wrong since I have similar logic in place elsewhere and it is working.
In the case of VBA, returning something different to 0 is not TRUE. You get true when you use some operator
If Session.FindText("Rate", 4, 16, 4) = "some text/value" Then
If Session.FindText("Rate", 4, 16, 4) <> "some text/value" Then
If Session.FindText("Rate", 4, 16, 4) >= "some text/value" Then
If Session.FindText("Rate", 4, 16, 4) <= "some text/value" Then
Also, if the text returned is "True" or "False" for the IF in VBA is just a text, not TRUE or FALSE
Edit #1 because of your comment...
And you can use this:
If Session.FindText("Rate", 4, 16, 4) = Empty Then
'some code if the logical test is true
else
'some code if the logical test is false
end if
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.
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