Dim tc As Cells = newWorksheet.Cells
Dim tccell As Cell
tccell = tc.Find("PT9", Nothing, findOptions)
Note: The “tccell” object declared above As “type Cell” contains a string “J1” that I want to retrieve.
Using VS2010 when I inspect “tccell” it contains the following:
{Aspose.Cells.cell [J1; ValueType: IsString; Value: ABCD]}
How can I extract the value “J1” from “tccell”?
If I try to display tccell:
MsgBox(tccell) -->Argument 'Prompt' cannot be converted to type 'String'.
How can I use the result from the Find Method as shown above?
Thanks
The Cell class has Row and Column properties. If you want those properties in the alphanumeric format, use one of the CellsHelper methods to get that:
string cellname = CellsHelper.CellIndexToName(row, col)
You might also find that the alphanumeric location of the cell already resides in its Name property.
Related
How do I change a string / remove the quotations to make it a existing object value instead of string? I've got a constant object that is used to input what the "currentcreature" is which's value is inputted into byVal tables but whenever I try inputting it as the result of a SQL table search is a string I do not know how to put the string result turning into an object.
Dim Placeholder As New List(Of Object)
Sub with stuff
another sub that ends with this
Placeholder.Add(SearchResults.GetString(0))
CurrentCreature = Placeholder(0)
LoadCreature(CurrentCreature, Player)
End sub
Object of the Constant CurrentCreature immediately gets an error with the preceding code
Value of Creature should be Object rather than String
Creature "Frog" Object {String}
What should happen:
CurrentCreature sure be able to apply with the object as its input than string (this works normally if I put it equal to any object so I mainly want to figure out how to change the values of strings into datatype object/gain the values of it initially stored as objects)
According to this answer one should always use Variant when assigning values in a cell to a variable in the code. Is this correct? I seem to recall reading elsewhere that using Variant indiscriminately is not a good practice.
You can read a cell value into any type you want, VBA will (try to) implicitly convert it to that type for you.
There are dozens of questions on this site involving run-time errors raised from reading cell values into a specific data type - perhaps you've seen this error message before?
Type mismatch
That's the error you get when you try to read a cell containing an error value (e.g. #REF!) into anything other than a Variant.
So if you read a cell value into, say, a Double, everything will work fine as long as you're reading something that VBA can coerce into that data type. The problem is that, well, data is never 100% clean, worksheets do break down, users delete columns and break formulas, lookups fail and the person that wrote the formula didn't bother wrapping it with IFERROR, etc.
That's why you read cell values into a Variant.
That doesn't mean you work with a Variant.
Dim cellValue As Variant
cellValue = someRange.Value
If IsError(cellValue) Then Exit Sub 'bail out before we blow up
Dim workingValue As String
workingValue = CStr(cellValue)
By assigning to another data type, you effectively cast the Variant to that more specific type - here a String. And because you like explicit type conversions, you use VBA's conversion functions to make the conversion explicit - here CStr.
Now, in real code, you probably wouldn't even bother reading it into a Variant - you can use IsError to test the cell value:
If IsError(someRange.Value) Then Exit Sub 'bail out before we blow up
Dim cellValue As String
cellValue = someRange.Value ' or cellValue = CStr(someRange.Value)
The flipside here is that you're accessing the cell twice. Whether or not that's better that reading it into a Variant is for you to decide; performance-wise, it's usually best to avoid accessing ranges as much as possible though.
The value you get from a cell (which is a Range) is a Variant according to the documentation:
Range.Value Property (Excel)
Returns or sets a Variant value that represents the value of the specified range.
Since a Variant can represent different data types, you could loose information if you would assign a cell's value to -- for instance -- a variable of type String.
The mere fact that there is data type information in a Variant already means you lose that type of information. If for instance the original type was numeric and you store it in a String variable, there is no way to know from that string value what the original data type was. You could also lose precision (on Date milliseconds for instance).
Furthermore, a Variant type value cannot always be cast to the data type of your variable, and so you could get a Type mismatch error. In practice this often happens with the Error sub data type.
Only when you know beforehand what the data type is of a certain cell's value, it would be good to define your receiving variable in that data type.
Not strictly answering your question, but thought I'd add this for reference anyway.
With native Excel functions you can usually provide either a range object or a value directly to a function. For example, you can either write =AVERAGE(A1,A2,A3) or =AVERAGE(10,20,30). If you want to do something similar for any user defined functions, you will need to check the type of object passed to your function:
Function test(input As Variant)
Dim var As Variant
If TypeName(input) = "Range" Then
var = input.Value
Else
var = input
End If
You may also want to check for other objects if your function can accept them, but doing this will make your functions behave more like users expect them to.
I have a named range called tempPrintArea
it refers to ="A1:J59"
I created it with VBA and the scope became local. I don't know if the scope matters.
I used this line to create the named range:
wks.Names.Add Name:="tempPrintArea", RefersTo:="A1:J59"
Now I want to set the value of a string to "A1:J59". I've tried the following:
Dim test As String
test = Range("tempPrintArea").RefersTo
but I get the error message Method 'Range' out of object '_Gloabl' failed
What can I change in these code lines to get this working?
When you're using
wks.Names.Add Name:="tempPrintArea", RefersTo:="A1:J59"
code doesn't create named range that refers to A1:J59, instead code creates named range with text "A1:J59".
For creating named range use this one instead:
wks.Names.Add Name:="tempPrintArea", RefersTo:=wks.Range("A1:J59")
and then
Dim test As String
test = wks.Range("tempPrintArea").Address(False, False) ' returns A1:J59
I have a Problem with validating a String in vb.net.
I want to check if the inputString is in a valid standard numeric Format according to http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx except "E" and "X"
So i have the Format specifier e.g. "d4" and my inputString like "1999". But the Specifier is unknown at design time.
Is there a way to validate the inputString if it is valid with the given Format specifier
greetz gangfish
UPDATE:
I am writing an Extension for a working programm.
I am adding massprocessing for selected rows from a gridview.
If a row is selected, the values are shown in my inputFields.
The inputFields are from Telerik UI for WinForms(RadMaskedEditBox).
If multiple rows are selected, i have to remove the Masking, because we have to add an Editor-Pattern for alle Entries (Form: {1..5}test).
So i have to validate if the given input is valid for the masking assigned by the main application. So i have no clue what format specifier is given at design time.
But i know that all standard numeric formats except "E" and "X" are supported by the RadMaskedEditBox
I would like to have something like this:
Dim inputValue = "1999"
Dim formatSpecifier = "d4"
Try
ValidateValue(inputValue, formatSpecifier)
Catch ex As Exception
' Validation Failed. Handle it
End Try
A format specifier is just a string. You can substitute it with any string value at runtime, provided that string value is a valid format specifier. (Otherwise I imagine you'll get an error, so you'll want to have some error handling around all of this.)
For example, given the sample code on the link you provided:
decimal value = 123.456m;
Console.WriteLine(value.ToString("C2"));
If you have a format specifier obtained from somewhere else and stored in a string variable, you can just use that variable:
string formatSpecifier = GetFormatSpecifier();
// ...
decimal value = 123.456m;
Console.WriteLine(value.ToString(formatSpecifier));
Or if it's part of a larger string, you can just concatenate it into that string. So where you would normally have this:
decimal value = 123.456m;
Console.WriteLine("Your account balance is {0:C2}.", value);
You might instead have this:
string formatSpecifier = GetFormatSpecifier();
// ...
decimal value = 123.456m;
Console.WriteLine("Your account balance is " + formatSpecifier + ".", value);
(Or you can construct the string with a StringBuilder. You might also use string.Format() to assemble your format specifier, but I imagine that could introduce confusion when supporting that code.)
I have this code which gives an error:
'declaration
Dim strFieldValues As String
'split
strFieldValues = strRecord.Split(",") 'field are separated by commas
Well, the error seems to be pretty self-explanatory to me. You've declared a variable of type String - i.e. it can hold a value of a single String reference:
Dim strFieldValues As String
You've then tried to assign a value to it returned from String.Split():
strFieldValues = strRecord.Split(",")
Now String.Split() returns a String array, not a single string value.
So you have two courses of action open to you:
Change strFieldValues to an array variable
Change the value you assign to it
My guess is that you want the first, but we don't know what you're trying to achieve. The simplest approach would be to combine declaration and initialization:
Dim strFieldValues = strRecord.Split(",")
You may also need to change the arguments to Split - I don't know how VB will sort out that call.
If all you want to do is just retrieve either side of the resulting string array, you could just invoke the left or right part like this:
strFieldValues = strRecord.Split(",")(0) ' Text to the left of the delimiter character
Or
strFieldValues = strRecord.Split(",")(1) ' Text to the right of the delimiter character
Of course, this assumes that the delimiter character does exist, so you should take the necessary precautions to ensure that you won't run into a runtime exception if said character is not found on the string you are splitting.