Verify a variable contains Valid Range address in excel VBA - vba

I have tried to get a value Range address value in to a variable.
searchWrd = InputBox("Please enter the column name of first word/token's", "Search String", "J1")
I need to verify the variable searchWrd contains a valid address or not. Also what should be the data type for the variable searchWrd

I'd go with
searchWrd = Application.InputBox("lease enter the column name of first word/token's", "Search String", ,,,,,8)
For more information read this:
https://msdn.microsoft.com/en-us/library/office/ff839468.aspx
Basically, the 8 at the end sets the input type to be a range. Note, that this will also allow you to immediately Dim searchWrd as Range.

You could try to verify the Text you get from the Inputbox by using a normal If-Then-construct. To get the correct data type we Need to know what should be possbible to type into the Inputbox. Normally i would try to get the Row/Column Number of the Row/Column the Search string is located at. Or, much better, to ask for the string it self and write the code to look up where it is located. So i only Need to check for spelling Errors and give a message that the string isn't found. It's better then asking for a range i think.

Related

MS Access 2016 vba: Empty value passed from form field (text box)

I'm having a strange situation:
I have a simple form with 2 text boxes.
The second one has the vba code triggered after being updated.
For example: typing value to the first field, ENTER, typing value into the second field, ENTER and the code starts.
The code, initially, takes the values from the text boxes and assign them to the string variables (pre declared as strings), like:
test1 = Me.frmSSN.Value
The problem is, the test1 variable seems to be empty after the line above.
It seems to happen only when I type, for example this string:
073QB8KJ2D00A4X
It works fine, when entering CNB0K2W5JK
The tool is a simple serial number comparison.
Just for test, I've entered this line into the code:
aaa="073QB8KJ2D00A4X"
When running in the step-by-step mode and hovering mouse over the "aaa" I'm getting: aaa= and then nothing.
Not even single "" like aaa="" or so. Just aaa=
After retrying multiple things - I believe it's about the value I enter:
073QB8KJ2D00A4X
Could be, that for access/vba it's some control string or so?
I'm just dumb now...
Thanks in advance for any help
Marek
p.s. Source fields are plain text boxes. And here's the code:
Dim user As String
Dim 1stSN As String
Dim 2ndSN As String
1stSN = Me.frmSSN.Value
2ndSN = Me.frmHPSN.Value
Then the values are being used as a part of SQL query. The problem is - in this situation - query doesn't work, as the sql string looks like:
"Select * From sbo_SerialSource where SN like " and nothing after "like".
Debug shows the correct value (with serial number), but query fails with "syntax error" message. Seems like there are some strange "control characters" are being created/added.
That's it.
And I have to use the serial numbers as these "strange ones", because that's how they come from the vendor.

VBA Inputbox strips decimal comma, converts double to string

I'm trying to create an Inputbox which has a number, with decimals, as the default value. I'm setting the Inputbox to the formula type, because the user might input a formula or reference a cell.
The problem is that the Inputbox seems to strip the comma and coerse the number to a string. I could fix this casting the number as a string with Format, and then going back to a number afterwards, but losing precision. And I'd like to understand what's going on.
The code is:
Sub test()
Dim Defolt As Double
Defolt = 1.1866701960364
Dim InputValue
InputValue = Application.InputBox("Value?", , Defolt, , , , , 0)
'for this example, the user just clicks OK to the default value
Debug.Print InputValue
End Sub
The results are these:
Thanks!
ps: the locale is Spanish. Excel version is Excel 2010 32bits.
Have a look here. The important part is right under the table:
You can use the sum of the allowable values for Type. For example, for an input box that can accept both text and numbers, set Type to 1 + 2.
and a little further down in the remarks:
If Type is 0, InputBox returns the formula in the form of text — for example, "=2*PI()/360". If there are any references in the formula, they are returned as A1-style references. (Use ConvertFormula to convert between reference styles.)
Try setting the type as 1 and see if you can still use a formula and number. The documentation leads me to think that you can (basically you get formula for free). Since you're setting the type to 0, you're getting back the default Text type.

How to properly read a single Excel cell

I've been looking up in Google in these past days but I still can't find a good answer to this one.
Currently, this is how I do it:
For Each cell In ws.Range(fromCol, toCol)
If IsNothing(cell.Value) Then Exit For
valueList.Push(cell.Value.ToString())
Next
But when it reads a cell whose assumed data type is Time, it returns a Double value. I try to parse that value but it's not the same as expected.
How can I properly read a single Excel cell with an assumed type of Time?
As per the comment suggesting the article,
.Text is a bad idea in the sense that it will give you just the displayed string in that cell. If you use TypeName on the cell's content, for example, it will always return string regardless of any datatypes the content might be. However, if you use .Value, it will return the actual datatype name of the content of that cell.
This can prove useful to you if you're using TypeName in a comparison for instance. It saves you using conversion functions.
Take this example:
Sub Test()
Range("A1") = "True"
Debug.print(TypeName(Range("A1").Value))
Debug.print(TypeName(Range("A1").Text))
End Sub
This Output is:
Boolean
String

Validate entered cell value according to a given data type

I use an Excel macro to create new workbooks filled with data of given csv files. One column is open to user input of certain attribute values. These values must fit a datatype and a certain formatting to be accepted by another processing system, here are some examples:
Value A: String value formatted like "#"
Value B: Integer Value formatted "#"
Value C: Float value formatted "#.##0,0###"
Value D: Decimal value formatted "#.##0,0#"
Now i want to add a validation to check if a typed input value is ok for processing or has to be changed, examples:
a String value may not be entered in a Decimal field
negative values are not accepted for all number formatted fields
How do I set up this validation? I am quite new to VBA not shure about the best way to go. The possibilities I found so far:
Add a validation via Worksheet.Range.Validation, but I don't have any clue how to set up the Formula1 to check for correct values.
Go by Worksheet_Change, the problem here is that i create new Workbooks which apparently not contain the Worksheet_Change I wrote in my program code because it is a new workbook.
So what's the best way to go?
You can do quite a bit with data validation on the workbook side, without VBA. For example, for Value C, you could put in the formatting (Float value formatted "#.##0,0###") in the custom number format for that cell, then apply data validation to make sure only numbers are entered in a range you'd like.
Using Worksheet.Range.Validation is not very save because it is not triggered if user pastes data from clipboard.
Go for Workbook_SheetChange. Implement the validation there and save the workbook as your template.
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
Debug.Assert (Format(Target.Value, "#.##0,0#") = Target.Value)
End Sub
Make sure all cells are formatted as text if you use this approch. Otherwise Target.Value will give you an interpolated value.
Then later open the template and import the cvs into it:
wb = Application.Workbooks.Add("path-to-template.xslm")
Application.EnableEvents = false ' You don't want to trigger change events during import
myimport(wb)
Application.EnableEvents = true

Excel Stored Procedure with Excel

I have stored proc Im pulling in to excel but to get it to Run I have to enter my text exec roc 'Name' I need it to be able to have someone else thats running it be able to enter a name as they refresh the data. I dont know VBA at all and am looking for help.
I don't completely understand your question but if my assumption is correct... you want to have a cell where someone can enter some sort of name, where you can then use their input to do some other operations.
To start, you would want to grab a string variable (by grab I mean create)
Example:
Dim strName As String
Then you would want to be able to read a specific cell.... In the next example the cell will be A2
Example:
strName = Range("Sheet1!A2").Value
If you named the first sheet something different use that name. If the user inputs a name into that cell you can then use it later to do calculations or anything you want.
If you are not using VBA code then what are you using?
If using MS Query, try changing the SQL text to
{CALL roc(?)}
and IIRC this should prompt the user to enter text for the parameter value.