Convert Variant To Typed Array
May 23, 2007
I am using strongly typed code, but I am also pulling data from worksheets into variants. e.g.
Dim MyArray As Variant
MyArray = [MyNamedRange]
is there any shortcut to convert the variant to a typed array, which doesn't just involve looping throught the variant and using a casting function, e.g.
Dim MyArray As Variant
Dim typedArray() As type
MyArray = [MyNamedRange]
typedArray = shortcut(MyArray)
View 4 Replies
ADVERTISEMENT
Jul 19, 2014
I want to test how many elements are not empty within a variant array.
For example for arrays Arr1 and Arr2 below the answers would be:
- for Arr1 not empty elements=1
- for Arr2 not empty elements=0
Arr1(0)=empty
Arr1(1)=2
Arr1(2)=empty
Arr2(0)=empty
Arr2(1)=empty
Arr2(2)=empty
I've tried with function COUNTA inside VBA but counts even the empty values:
Code:
NotEmptyCells = Application.WorksheetFunction.CountA(Arr1)
Is there another function to count this from an array or alternative way?
View 9 Replies
View Related
Jun 26, 2008
The answer is probably "because I'm stupid", but I really can't get my head around it! I'm playing with variant arrays for the first (and possibly last) time,
The code I have is:
Sub test()
Dim vSheetColours As Variant
Dim iCounter As Integer
vSheetColours = Range("Colours").Interior.ColorIndex
For iCounter = 1 To UBound(vSheetColours, 1)
MsgBox vSheetColours(iCounter, 1)
Next iCounter
End Sub
(Obviously this code doesn't do anything useful - but if I could get it to work, I might have a chance of making my real code work!)
Colours is a range of 8 cells. Each one has some text in, and has a different background colour. I'm trying to store the colours.
If I run this code, I get a runtime error 13 type mismatch, and it highlights
For iCounter = 1 To UBound(vSheetColours, 1)
But if I replace
vSheetColours = Range("Colours").Interior.ColorIndex
with
vSheetColours = Range("Colours").Value
it works fine.
View 9 Replies
View Related
Jul 13, 2006
Is it possible to use an array without looping through it? I have code that has two loops: One that is just for the array and the other that loops through the data. It would be nice if I could get it down to one loop, but I'm not sure if it's possible. For example, if you have
Dim varArea As Variant
varArea = Array(1,2,4,6,8,12)
For x = LBound(varArea) To UBound(VarArea)
Do While Not C Is Nothing
Code here
Is there a way to advance to the next item in the array without using the loop.
View 10 Replies
View Related
Jan 24, 2013
I am trying to develop some code to serve as an 'undo' for several macros that I have. They each take a selected range and perform some changes to that range. I have managed to make some code that will undo the last macro run but would like to make something that can go back several steps. To that end, I have started with the following code but am running into an issue when I need to use a variant array to hold multiple arrays of a custom defined data type:
VB:
Type SaveRange
Val As Variant
Addr As String
[Code] .....
This declares some public variables I'll need, each as an array so that I can iterate through several steps of do/undo using the undoIndex. I then use them to save each range I am about to change by calling the following macro within my actual data-manipulating macros:
VB:
Sub Save_RangeForUndo(rng As Range)
Dim i As Integer
Dim cell As Range
undoIndex = undoIndex + 1
[Code] .....
If you look at the last line here, this is my problem; I can't figure out how to properly use an array to hold each instance of OldSelection() for later referral. Just for completeness, here is my actual undo macro:
VB:
Sub Undo_Operation()
Dim i As Integer
OldWorkbooks(undoIndex).Activate
OldWorksheets(undoIndex).Activate
[Code] .....
I need to get this OldSlctVariant()() array to hold each instance of OldSelection() so that I can restore them for each consecutive undo. I'm not very familiar with the variant type and anything more than the basic use of arrays.
View 7 Replies
View Related
Jun 21, 2014
wondering is there a VBA equivalent of --() in excel that turns trues and falses to 1's and 0's?
View 14 Replies
View Related
Sep 26, 2008
I have a data array which i would like to convert to a list. Sort of the reverse of a Pivot Table
See example below
I would like to turn this
Account Jan Feb Mar April etc..
001 59 30 25 40
002 12 20 32 29
003 5 13 27 39
004 10 11 12 13
Into This
Account Month Amount
001 Jan 59
001 Feb 30
001 Mar 25
001 April 40
002 Jan 12
002 Feb 20
002 Mar 32
002 April 29
003 etc...
There is a sample of the data file attached.
View 3 Replies
View Related
May 9, 2008
I have a list of code that it has been suggested to me will work a lot more efficiently by using an Array.
Sub Auto_Open()
Sheet1.ComboBox1_click
Sheet2.ComboBox1_click
Sheet2.ComboBox2_click
Sheet3.ComboBox1_click
Sheet4.ComboBox1_click
Sheet6.ComboBox1_click
Sheet6.ComboBox2_click
Sheet10.ComboBox1_click
Sheet10.ComboBox2_click
Sheet11.ComboBox1_click
Sheet12.ComboBox1_click
Sheet12.ComboBox2_click
Sheet15.ComboBox1_click
Sheet15.ComboBox2_click
Sheet16.ComboBox1_click
Sheet16.ComboBox2_click
Sheet17.ComboBox1_click
Sheet17.ComboBox2_click
Sheet18.ComboBox1_click
Sheet19.ComboBox1_click
Sheet19.ComboBox2_click
End Sub
All the code does is refresh the selection list in a combo box so it is up-to- date when the workbook is open.
View 7 Replies
View Related
Dec 5, 2011
I want to have a user-defined function where the user sends a range (i.e. "A1:J1") and each cell is viewed as a member of an array. I can do this if the user sends "A1,B1,C1,D1,E1,F1,G1,H1,I1,J1", but this is very cumbersome. I tried the following code:
Code:
Public Function CreateDenom(DenomValues As Range) As Variant
Dim tmpArr() As Variant
Dim c As Range
For Each c In DenomValues
tmpArr(c) = c.Value
Next c
CreateDenom = UBound(tmpArr)
End Function
This won't be the end product of course, but it's a starting point if I can get it to work. However, I get an error stating that "A value used in the formula is of the wrong data type."
View 2 Replies
View Related
Oct 31, 2006
The attached has a mega array formula that really slows down the recalc on this spreadsheet. I would like to use a macro to write the value of the formula to the worksheet "Database" column J.
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Cells.Count > 1 Or IsEmpty(Target) Then Exit Sub
If Not Intersect(Target, Range(Range("rReason").Offset(1, 0), Range("rReason"). _
Offset(UsedRange.Rows.Count + 1, 0))) Is Nothing Then
Target.Offset(0, 1) = Application.VLookup(Target, ValList.Range("ReasonLkUp"), 2, False)
'This formula below needs a VBA equivalent
' Target.Offset(0, 4).FormulaArray = "=IF(MIN(IF(R7C3:R35000C3=RC[-7],IF(R7C8:R35000C8>0,R7C8:R35000C8)))=RC[-2],MIN(IF(R7C3:R35000C3=RC[-7],IF(R7C8:R35000C8>0,R7C8:R35000C8))),0)"
End If
If Not Intersect(Target, Range(Range("rSurname").Offset(1, 0), Range("rSurname"). _
Offset(UsedRange.Rows.Count + 1, 0))) Is Nothing Then
Target.Offset(0, 1) = ActiveCell.Offset(0, -2) & " " & ActiveCell.Offset(0, -1)
End If
End Sub
View 9 Replies
View Related
May 2, 2013
I have a variable that gets set to an address range:
Code:
TAG_RANGE = Sheets(BAL_SHT_TAB_NAME).Range("A1", Sheets(BAL_SHT_TAB_NAME).Cells(Rows.Count, Range("A1").Column).End(xlUp)).Address
In the first loop that executes this command TAG_RANGE gets set to $A$1:$A$39
I want to loop through the values in that range and run tests against them. Is there a function that will take the values in the address range an convert them into an array so that I can use something like this:
Code:
For Counter = LBound(TAG_RANGE_ARRAY) To UBound(TAG_RANGE_ARRAY)
[run tests]
Next
Or is there someother direct way to do this other than creating a loop that fills the array element by element
View 5 Replies
View Related
Feb 15, 2010
{= SUM(IF(({325,481,342,440,425}=ID)*($A37=DateRng)*1, ROUND(Sales,2),0))}
I am currently using this formula to retrieve total sales by day for each team and it works perfectly. Data is stored in columns by Date,Salesman ID,Sales.
I would like to replace the array portion with a vlookup to return the array set so i can use drop-down to select different teams and see the sales for that team.
{=SUM(IF((vlookup(TmName,Teams,2,0)=ID)*($A37=DateRng)*1,ROUND(Sales,2),0))}
This is the function as I thought it would work, but the vlookup returns "325,481,342,440,425" as a string not an array.
View 9 Replies
View Related
Sep 6, 2007
I have a byte array that contains a 4 byte floating point number. How would I convert this byte array to the single typed floating point number it contains?
View 9 Replies
View Related
Jul 1, 2009
I have a button the runs Application.GetOpenFilename And puts the output filepath into a label. I then have another button that opens a inputbox asking to change the file name and then copys the file from above path to my destenation.
when setting the destination i use ThisWorkbook.Path & Application.PathSeparator & "Images" & Application.PathSeparator and the imputbox value as the filename. doing it like this it wont alocate a extention for the file ".jpg,ect". The label.caption has the .ext and i would like to be able to store everythink after the . as a variant.
View 2 Replies
View Related
May 7, 2006
I experienced error "object required" when I tried to pass a argument of type variant into the b/m function call.
Call closeCon(con) 'con is of type Variant
The function is as follows.
Public Sub closeCon(ByVal nwAdoCon As ADODB.Connection)
nwAdoCon.Close
End Sub
View 9 Replies
View Related
May 17, 2006
Is there any way in VBA to convert a Variant into a String? This code doesn't seem to be valid:
Dim vName As Variant
Dim sName As String
vName = "John"
sName = CStr(vName)
View 5 Replies
View Related
May 31, 2007
Is/how possible to read entire ss into a variant "off-scree" without showing the user? I can open the file, I just dont know how to not show the file, select the entire sheet and read the values. I have seen the below but dont know how modify it as needed. Also is there a way to do this dynamically with variable length rows/columns?
Dim vaData As Variant
Dim lRow Long
Dim lCol As Long
vaData=ActiveSheet. Range("A1:B10").Value
from Professional Excel Development S.Bullen et.al. pp614
View 7 Replies
View Related
Jun 2, 2009
I am assigning the result of a vlookup to a variable that is defined as Variant. I then need to use that variable in a calculation.
View 5 Replies
View Related
Jul 5, 2006
I have a multiple select list box which I capture the selected items by using ListBox1. List
I want to use the 'selection' to build up a string that will form a document name I want to add to an email as an attachment (all being done from Excel), however I get a type mis-match error as the ListBox1.List output is a variant.
So I'm aiming to have something like MyFile = firstpartoffilename & ListBox1.List & "*.doc"
Do I need to somehow convert the variant variable or are there some other clever tricks I can use to create my document name.
View 4 Replies
View Related
Aug 10, 2007
I have looked into the maximum length of a variant/string in vba and it appears to be 250 characters. I am running a macro which first lists all excel files in a folder, returning them to a sheet, then using a loop statement opens each one in turn extracting the information to a second summary sheet before closing it. The file path to the folder is ridiculously long and the macro stumbles. I used the =LEN(A1) formula to check if the file names were too long for the string, but the maximum file name length was 226 characters. I've tried both String and Variant to collect the file names but both have the same effect.
View 2 Replies
View Related
Nov 6, 2009
I have about 20 different workbooks and i want to create identical tables and graphs in each one of them using the least number of steps.
The number of the rows changes by workbook but the colums are identical. So the range will vary by workbook.
so for example:
workbook1=
name sales
a 10
b 20
c 30
workbook2=
name sales
a 10
So I want a macro that does the tabling and charting in workbook1 and automatically moves to workbook2 and so on.
View 9 Replies
View Related
Oct 7, 2009
I would like to get the return value for an evaluate statement which is a variant data type, convert it to an integer and do some computation. I have 4 test programs, the first 3 all work, the 4ths does not.
The first sd_test(returns 1) and st_test1(returns 1) demonstrate that my evaluate statement is correct and works,
sd_test2(returns 3) shows that I can convert a variant to an integer
in sd_test3(returns #VALUE!) I try to put both together and it doesn't work?
View 8 Replies
View Related
Feb 11, 2009
I wanted to convert a variant to an integer. The variant data is stored in an array, the variant data was entered from a userform.
Private Sub CB_OilPrice_Click()
Dim xx As Long
UserFormSpecifyDetail.Show 'Enter data
For i = 1 To 120
xx = VarType(UserFormInputArray(i)) 'Returns 8
xx = Int(UserFormInputArray(i)) 'Returns error
Next i
End Sub
The value in UserFormInputArray = "50000"
error = Run-Time error 6, overflow
Excel 2003
View 9 Replies
View Related
Mar 6, 2014
I'm currently building a large spreadsheet which will require numeric codes to be within certain cells. At present these cells are filled with alphabetical codes and the person using them has little knowledge of the numbers, is there a simple way for them to type in the alphabetical code and it to then auto-replace it with the numberical?
View 8 Replies
View Related
Mar 23, 2007
I am trying to make a puzzle and use excel to verify I have done it correctly.
What I want to do is when I type a letter in a specific cell have it put a number value in another cell automatically. For instance, if I type the letter "a" in cell A1 I would like the number 60 to populate automatically in cell B1. I want to give each letter in the alphabet a numeric value so I can type a message in the A column and then auto sum the results of the numbers in the B column.
View 10 Replies
View Related
Feb 2, 2012
Is there a formula that can sum only when i am typing numbers on a cell, but only when i enter new data to double check my quantities.
View 5 Replies
View Related
Jan 26, 2009
I am getting the 'the formula you typed contains an error" ....
View 9 Replies
View Related
May 3, 2014
Is there a way to imitate the search box in access where it highlights any matching text in the entire database. BUT, instead of hitting the next arrow to go to the next matching thing, I want it to simply hide all rows that contain nothing that matches. I don't want it to just hide them though, I want them to be shown from the topmost cell down rather than simply hiding cells and having a ton of gaps to look through. If this can be done in access then that will work as well.
View 1 Replies
View Related
Oct 27, 2008
I was wondering if there would be a way to use vlookup and still be able to type text in the same cell. For example, if the vlookup gives me a result of 123. I want it to also pull from a different cell that I've typed in 321. So the resulting cell would show 321-123 or 321 123. I have attached a sample sheet. On sheet two where the 3 items are, I want it to display 321-123 for item 1, 321-234 for item 2, and 321-345 for item 3. The 321 would be dependant on what I type into B1 on the first sheet.
View 2 Replies
View Related
Jun 12, 2014
I have the following two columns, and would like to obtain for each individual Company, the corresponding Country values excluding duplicates as text in a single cell.
Company 2Country B
Company 2Country C
Company 3Country C
Company 3Country C
Company 5Country A
Company 5Country C
Company 5Country C
For example:
- For Company 2, a cell containing "Country B, Country C"
- For Company 3, a cell containing "Country C"
- For Company 5, a cell containing "Country A, Country C"
I've approached generating an array using an IF statement, as in =IF(INDEX(A1:A8="Company 5",,),INDEX(B1:B8,,)," "), which returns the following array: ={" ";" ";" ";" ";" ";" ";" ";"Country A";"Country C";"Country C";" ";" ";" ";" "}.
The question is: how do I get that array to produce, as text in a cell: "Country A, Country C". Note that the duplicate Country C has been removed.
There are a few "StringConcat" User-defined functions that I've found elsewhere on the internet, but they don't seem to be able to handle to conditionally generated IF Index array, which I would think is key to parsing between Countries corresponding to each Company in the list.
View 3 Replies
View Related