Converting A Variant Into A String
May 17, 2006Is 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)
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)
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?
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.
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 RelatedI was wondering if it was possible to do something like this:
x = 15
test = "x"
MsgBox (test)
with the message box saying "15" instead of "x". I guess more specifically what I want is:
x = 15
test = Range("A5").Value 'Range("A5").value = "x + 2"
MsgBox (test) 'I want "17" to be outputted
Is this possible? Somehow convert the string test into an integer and read the variable?
I'm currently trying to copy a dynamic range based on the row number of a selected listbox item. I'm able to get the row number of the selected listbox item from the data tab in my file. I, however, am stuck when it comes to copying a fixed column / varying row range from this tab and copying it over to another tab.
Private Sub CommandButton2_Click()
Dim ID As Long
Dim Row As Long
Dim Cnt As Long
Dim SearchTermsStr As String
Dim SearchTermsRng As Range
[Code] ....
SearchTermsRng is returing "Nothing" but the SearchTermsStr returns "F3:O3"
I have a problem in converting text to columns for a string that has 1000+ entry's. Excel bumps me out after 851 using the Split function.
The data I can only download in a text format has >1000 fields that are seperated by ",". Not a problem so far but when I attempt to convert to columns there is a maximum limitation of 851.
Data1,Data2,Data3,Data4...................Data1000,Data1001,Data1002 etc...
I have tried:
Sub Split_Delimeter()
Dim Words() As String
Words = Split(Cells(2, 4), ",")
MsgBox "String contains " & (UBound(Words) + 1) & " words"
End Sub
I have a set of letters in a single row DISATAGFOORCBORDQFINFCOBBKIEHDSMCNRSKARDFCJSLCQCECFVSBCBOYKPVYKC. I want to distibute them across 5 columns such that each letter occupies one cell. I might want to specify a different column width later on so the solution should be flexible.
View 5 Replies View RelatedI am trying to check my sheet names to see if they convert to a valid date format. If it doesn't, then I want it to check for the next criteria (ex - if it equals "Page2"). With the code below I keep getting an error of type mismatch. Code is below.
[Code] ......
I need to be able to locate a date in a spreadsheet bassed uppon a user input date. I am looking for the week that contains the user's date. However it seems even if I initialize the value as Date, it still reads the user's input as a string.
Is there an easy way make sure the system automaticaly recognizes the user's input as a date, or is there a way to quicly convert the string to a date?
I have a spreadsheet that has 3 different dates in 3 different cells:
B4 - 3/24/2014
B5 - 4/6/2014
B6 - 4/14/2014
From that information I need once cell to display "Mar 24 - Apr 6 paid Apr 14"
Reason being is that I am importing journal entries into an accounting program and I am allowed one cell each row for the description. This would let me change the dates only and my descriptions will fill out before import. It is important that the months are abbreviated to 3 letters as I am limited on how many characters the description can be.
I have a function that will be used across 10K+ records at a time, so I was taking a look at optimizing it a bit. At the start of the routing I have a string with a 9 to 15 digit numeric value in it. I wanted to load each individual digit into a byte array. I started out using:
byVal = StrConv(sVal,vbFromUnicode)
But that actually loads the ASCII Character codes into the array not the digits themselves. From there I considered looping through the array applying CByte(Chr$(byVal(i))) but then I wondered if it would make more sense to just load the array with Mid$(sVal,i,1)... Does anyone have tested knowledge on which performs better? Is there an option C I have missed?
I have hit an issue when tranfering a string date from VBA to excel, excel thinks the string is US format and swaps around the days and months even though the system and excel are setup for UK date format:
Simple example of the problem:
Sub test()
Dim testy As String
testy = Now
'testy will equal 04/10/2007 17:20
Range("A1").Value = testy
'A1 will equal 10/04/2007 17:20
End Sub
Now I know in this example that if I used DateValue(testy) it would work, but our situation is a bit more complex as we are running this is a loop testy is not always a date.
I have an ACD program that gives me a time in "mm:ss" format, or in ":ss" fomat. I have 2-3 problems with charting this data.
1 I am inheriting the production of charts from someone else, with old data being displayed by seconds only I am supposed to keep this display.
2 the data that is in mm:ss format is recognized as being in full date format, for example 2:14 is recognized as 2:14:00 AM instead of 2 minutes, 14 seconds.
3 the data that is in :ss, ie :44, is recognized as a string.
Any thoughts on how I can convert both cases to a integer seconds? I know for example that I could mid() the second condition, but I am stuck thinking about the first condition.
I can't adjust the way the time comes in, unfortunately. It is an auto-generated export of the data from a switch.
I have a userform that reads in a list of names into a combobox. When you choose a name from the combobox it then shows the that persons details from a sheet containing all the names.
I have some text boxes that allows the data to be modified and returns it to the sheet which works fine. What i also want it to do it put the data from the form into a sheet for that individual. For example the name of Ford would go into the Ford worksheet.
How do i read in the value from the combobox on the worksheet and use it as a variable to use in my code. For example
Sheets(Value from the combobox).Cells(NoOfRows, 3).Value = Me.TextBoxChangePosistion.Value
*NoOfROws is the variable to find a blank row
My question is about converting a Literal String to Character Code. I'm using the following coded InputBox. And it prompts the user for what characters to search for in a string.
View 4 Replies View RelatedI have a cell which says November , 2012 in General format
In the next cell i want a date of the following month . Eg : 1-Dec-2012.
I tried =DATE(RIGHT(G4,4),LEFT(FIND(",",SUBSTITUTE(G4,",",",",1))),1)
I recieve a spreadsheet each month with the following column of data:
9 Paddington House
PO Box 73
Whiteleys of Bayswater
9-10 Grove Road
134/138 Drymen Road
44-46 The High Street
1 Midland Road
48 Donegall Place
As you can see, it consists of addresses, each line is a separate address. I need it to be converted so that the data is split into the following 5 fields:
Street number from
street number letter from
street number to
street number letter to
street
If there is no street number , then it should just enter the data into the street field. If there is a range such as 44-46, it should enter the 2 numbers into the from and to fields. Sometimes this is in the format 44/46. Also, sometimes there are letters, eg 44a-44b, in which case each section (ie 44, a, 44, b) needs to be in a spearate field.
I have a string of data coming from a SQL Server data connection into my workbook. The value in the column is a text string that is pipe delimited. I need a macro to parse the data from that column into applicable separate columns. I would typically use text to columns for this and parse it out manually... but the tool I'm creating is one of the automated variety, so that will not suffice for this application. I need it to do this automatically when the data connection refreshes.
U:U AQ AR AS AT AU AV
to
Produce|Fruit|apple|banana|cherry|date Produce Fruit apple banana cherry date
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.
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
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
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 RelatedI 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?
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.
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.
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)
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.
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.
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