Quick way to write multiple if statements - excel

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

Related

Syntax problem to check if boxed are filled with information

i think i have a syntax problem.
I just want to check with vba if theres every field filled for next steps.
I need like 15 more cells to check
Maybe someone can help me
my code work, just the first line give error because of this "and ("C24:C22")"
For Each Zellen_auf_Inhalt_prüfen In Range("B7:J7") **and ("C24:C22")**
If Zellen_auf_Inhalt_prüfen.Value = "" Then
MsgBox "Bitte alle Zellen ausfüllen."
GoTo MacroEnde
End If
Next Zellen_auf_Inhalt_prüfen
"Zellen auf inhalt prüfen" means, to check cells if theres anything in
i google but i dont find any solution.
I think you are looking for the union statement:
For Each Zellen_auf_Inhalt_prüfen In Union(Range("B7:J7"), Range("C24:C22"))

Userform textbox fills in if Vlookup finds the information related to the numbers inserted

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.

Case Statement in VBA

Case statement below is not working when the condition met.
Dim TemplatePick As String
Select Case TemplatePick
Case OptCreate.Value = True
Call WebFormInfo
Case OptModify = True
Call ModifyTemplate
' many more case statement to come
End Select
Select case is used to test the one value in this case TemplatePick then the Case would be Case "A" which would fire when TemplatePick = "A"
So for this to work:
Select Case True
Case OptCreate.Value
Call WebFormInfo
Case OptModify
Call ModifyTemplate
' many more case statement to come
End Select
Now one caveat with Select Case, once it finds a match it ignores all others. In other words if OptCreate.Value is True then it will stop and not test whether OptModify is True.
Your case test expression (TemplatePick) is not the same as your expresion list (OptCreate.Value, OptMOdify). I have a hard time even understanding what your are trying to do. Properly structured it would look something like this:
Dim TemplatePick As String
Select Case TemplatePick
Case "Template 1"
Call WebFormInfo
Case "Template 2"
Call ModifyTemplate
...
case Else
'Do default behavior
End Select
More resources https://msdn.microsoft.com/en-us/library/cy37t14y.aspx

VBA in Excel v2010 - Select Case command within another Select Case

I have a Excel v2010 Project I'm trying to complete, and I'm trying to use the "Select Case" command within another "Select Case" .... at the moment it doesn't seem to work... so my question is.. can it actually work and I'm doing it wrong or should I replace it with "if-else-end if" ??
Select Case LCase(Cells(i, "B").Value)
Case LCase("ABC")
Select Case LCase(Cells(i, "C").Value)
Case "DEF"
x = x + 1
Thanks Guys :)
Sure you can. Read this article.
Actually, your code doesn't work, because LCase returns lower case, but your Case "DEF" is in upper case, so you need to slightly modify your code:
Select Case LCase(Cells(i, "C").Value)
Case "def"

Excel - VBA : Simplify this If statement comparing cell to words

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

Resources