Easy question here.
I am currently using this in my program :
If LCase(inp_rng.Offset(1, 0).Value) = "street" Or LCase(inp_rng.Offset(1, 0)) = "ave." Then
score = score - 50
End If
It is obviously not clean but I can find a way to put it in one sentence only. What is the programming way of writing something like this:
If LCase(inp_rng.Offset(1,0).Value = ("street", "ave.", "road", "...", etc.) Then
'do something
End If
Thanks in advance!
You can use Select Case statement instead:
i = LCase(inp_rng.Offset(1,0).Value
Select Case i
Case "street", "ave.", "road"
'do something
Case Else
'do something
End Select
Alternatively you can populate all possible answers in an array and search the array for a match.
You may use Filter() array function
http://msdn.microsoft.com/en-us/library/office/aa164525(v=office.10).aspx
Related
For various reasons, I need to concatenate a text of the form [NAME].Value by changing the value of NAME to an inputbox entry.
Something like this:
Sub example()
data_in = InputBox("Give me something: ")
mystring1 = "[" & data_in & "].Value"
a = Evaluate(mystring1) 'I know this is wrong, but I don't know how to do so.
End Sub
I know it can be done in other ways, and the example in which I want to use this code is not exactly this one, and while it can be done in several ways here, in the original code it can only be done this way.
I want, based on the input in the imputbox, to concatenate the string in whatever way, and subsequently cast that string as code to store the value in another variable, to be used later in the code.
I am not able to get VBA to read the string text as code. I have seen that there is a way that consists of creating a macro from this first macro, execute it, and then delete the recently created macro. The problem with this solution is that doing that I can't save the variable when returning to the initial macro (I don't want to use global variables).
Surely there must be a way?
Thank you very much.
EDIT: The code above returns Error 2015
In order to use a string as if it was code, you can use the evaluate function (exists in most languages)
The official documentation mentions this example:
[a1].Value = 25
Evaluate("A1").Value = 25
trigVariable = [SIN(45)]
trigVariable = Evaluate("SIN(45)")
Set firstCellInSheet = Workbooks("BOOK1.XLS").Sheets(4).[A1]
Set firstCellInSheet = _
Workbooks("BOOK1.XLS").Sheets(4).Evaluate("A1")
I have figured out the easiest way to do it, sorry for posting the question so soon.
Thanks to #Andreas for the solution. I'll write it here in case than could be useful to someone.
Sub example()
data_in = InputBox("Give me something: ")
a = Range(data_in).Value
Debug.Print a
End Sub
In the end the simplest thing is the last thing you try...
I have an userform where people have to fill in with data. If the data already exists, when they put the information in the DocumentTitleBox, other textboxes should automatically fill in.
My code works with letters, but not with numbers.
For example, when I put "aaa", it returns the vlookup values. But if I put "123", it won't do anything, even though there is a vlookup for it.
I cannot figure it out why. This is part of my code:
Private Sub DocumentTitleBox_Change()
On Error Resume Next
Result = WorksheetFunction.VLookup(DocumentTitleBox.Value, Worksheets("example").Range("D:E"), 2, False)
FIND = WorksheetFunction.VLookup(DocumentTitleBox.Value, Worksheets("example").Range("D:E"), 1, False)
On Error GoTo 0
If FIND = DocumentTitleBox.Value Then
NameBox.Value = Result
End If
Thank you in advance!
I always use this kind of thing. Could be cleaned up and stuff but I like the flexibility and I change stuff all the time so this works for me.
Private Sub DocumentTitleBox_Change()
If IsNumeric(DocumentTitleBox.Value) Then
ReturnRow = Application.IfError(Application.Match(DocumentTitleBox.Value + 0, Worksheets("example").Columns(4), 0), "Not Found")
Find = Application.IfError(Application.Index(Worksheets("example").Columns(5), ReturnRow), "Not Present")
Else
ReturnRow = Application.IfError(Application.Match(DocumentTitleBox.Value, Worksheets("example").Columns(4), 0), "Not Found")
Find = Application.IfError(Application.Index(Worksheets("example").Columns(5), ReturnRow), "Not Present")
End If
If Not Find Like "Not Present" Then
NameBox.Value = Find
Else
NameBox.Value = ""
End If
End Sub
PS: I donĀ“t know how to avoid the match functions odd behaviour with strings/numbers so I just go with the +0 and IsNumeric trick. One thing to note is case sensitivity, adjust that as needed, right now its not.
If DocumentTitleBox is a text box, try using DocumentTitleBox.Text instead of DocumentTitleBox.Value.
I have a collection that I'm attempting to iterate through, which I am able to do no problem. What I would like to achieve is seeing the next object in the collection, but I am unable to find anything on this.
I've tried to look ahead using a (+ 1) in the if statement, but this doesn't seem to work.
For each a in CollBlank
if CollBlank(a + 1) <> "some value" then
'do code
end if
Next
Ideally, I'd like to be able to look ahead.
Access-vba & excel-vba are tagged since collections are used in both access and excel, I'm personally using it in Access right now, but most tutorials are through Excel.
Rather than using for each, use a for loop with an index variable, for example:
Dim i As Integer
For i = 0 to CollBlank.Count - 2
If CollBlank(i + 1) <> "some value" Then
' Do stuff
End If
Next i
How can I make a function with a formatted date that will work in every language? I want to make the following function:
=CONCATENATE(TEXT(C8;"TT.MM.JJJJ");"/";G8)
The problem here is, that I use the english client but because I'm a german, excel forces me to use T for day and J for year. I think this will cause problem on a PC located in england (for example).
I think [$-409] won't work because I still have to use T for day and J for year. Is there a good solution for this (function wise)?
If you pass the value of a formula in "" then it cannot be changed based on the localisation settings.
A good way to do it is to use a custom function with VBA, returning "TT.MM.JJJJ" if you are in Germany and "DD.MM.YYYY" if you are in England.
Public Function CorrectString() As String
Select Case Application.International(XlApplicationInternational.xlCountryCode)
Case 1
CorrectString = "DD.MM.YYYY"
Case 49
CorrectString = "TT.MM.JJJJ"
Case Else
CorrectString = "ERROR"
End Select
End Function
Would allow you to call the function like this:
=CONCATENATE(TEXT(C8;CorrectString());"/";G8)
And depending on the excel language, it would give either the German or the English versions.
To simplify the formula, try calling only:
=TEXT(21322;CorrectString())
This should return 17.05.1958.
Source for the regional languages, mentioned by #Dan at the comments:
https://bettersolutions.com/vba/macros/region-language.htm
Or run this to see the corresponding number of your current Excel:
MsgBox xlApplicationInternational.xlCountryCode
Just dropping in another imho elegant (VBA free) alternative taken from: Stackoverflow answer by #Taosique
=IF(TEXT(1,"mmmm")="January",[some logic for English system],[some logic for non-English system])
I've had the same problem and solved it with similar VBA Function. My function accepts input in international format, and outputs the local version for user.
Please see code below:
Function DateFormater(sFI As String)
Dim aFI() As String
aFI = split(StrConv(sFI, vbUnicode), Chr$(0))
ReDim Preserve aFI(UBound(aFI) - 1)
For i = 0 To UBound(aFI)
Select Case (aFI(i))
Case "m", "M"
DateFormater = DateFormater & Application.International(xlMonthCode)
Case "y", "Y"
DateFormater = DateFormater & Application.International(xlYearCode)
Case "d", "D"
DateFormater = DateFormater & Application.International(xlDayCode)
Case Else
DateFormater = DateFormater & aFI(i)
End Select
Next i
End Function
A simpler solution is to use generic Excel functions.
=CONCATENATE(TEXT(DAY(C8;"00");".";TEXT(MONTH(C8);"00");".";YEAR(C8);"/";G8)
Is there a way to create about 100 IF statements like the one below in bulk rather than individually typing each one out with a different value in place of "string". I sometimes use excel to create code but I don't know how to do a line break in a function.
Will IF statements work on a single line, because then excel could be a possibility.
Any help will be much appreciated!
if dbread("column").ToString = "string" then
...
else
...
end if
if dbread("column").ToString = "string2" then
...
else
...
end if
best do a select case.
select dbread("column").tostring
case "string"
''do stuff
case "string2"
''do stuff
case else
''do your else code here
end select