The value of gettxt = 'ATCH 6: Page 2 of 2
gettxt values are from text formatted cells.
I cannot get the following Like condition to be true.
If LCase(gettxt) Like "*atch #*:" Then ...
Suggestions on how to fix the Like statement are appreciated.
You're just missing one more "*" after the colon.
Your condition states that there shouldn't be anything AFTER the colon, but your "gettxt" value has something in it after that.
So the correct condition should be:
If LCase(gettxt) Like "*atch #*:*" Then
Full working version:
Sub test()
gettxt = "ATCH 6: Page 2 of 2"
If LCase(gettxt) Like "*atch #:*" Then
Debug.Print "Working!"
Else
Debug.Print "Not Working!"
End If
End Sub
Hope this helps!
Related
I have put together the following code so that when I repeatedly press a shortcut, the number in a cell automatically switches formats between:
2020
2020A
2020E
2020A/E
2,020%
2,020x
2,020bps
Here's the code that isn't currently working:
Sub Number_Format_Cycle()
Dim x, myFormats
myFormats = Array("###0;(###0);-", "###0A;(###0)A;-", "###0E;(###0E);-", "###0A/E;(###0A/E);-", "#,##0.0%;(#,##0.0)%;0.0%", "#,##0.0x;(#,##0.0)x;0.0x", "#,##0bps;(#,##0)bps;0bps")
x = Application.Match(ActiveCell.NumberFormat, myFormats, False)
If IsError(x) Then x = 0
Selection.NumberFormat = myFormats((x Mod (UBound(myFormats) + 1)))
End Sub
Any help would be much appreciated
Cheers,
Thomas
You have to use a backslash (\) in order to add character literals to make them showing up exactly as typed.
Changing the myFormats assignment to the following codeline worked for me:
myFormats = Array("###0;(###0);-", "###0\A;(###0)\A;-", "###0\E;(###0\E);-", "###0\A\/\E;(###0\A\/\E);-", "#,##0.0%;(#,##0.0)%;0.0%", "#,##0.0x;(#,##0.0)x;0.0x", "#,##0\bp\s;(#,##0)\bp\s;0\bp\s")
Re-Check each number format you intend to add after a test setting against the array elements.
Caveat: I have to admit that I can't explain the last \bp\s format, nevertheless it works.
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"))
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 would like to limit certain textboxes to accept only [A-Za-z]
I hope, a counterpart to Like exists.
With Like I would have to make a long list of not allowed characters to be able to filter.
Not MyString like [?;!°%/=....]
I can think of a solution in the form of:
For Counter = 1 To Len(MyString)
if Mid(MyString, Counter, 1) Like "*[a-z]*" = false then
MsgBox "String contains bad characters"
exit sub
end if
next
... but is there a more sophisticated 1liner solution ?
Until then, I have created a function to make it "Oneliner":
Function isPureString(myText As String) As Boolean
Dim i As Integer
isPureString = True
For i = 1 To Len(myText)
If Mid(myText, i, 1) Like "*[a-zA-Z_íéáűúőöüóÓÜÖÚŐŰÁÉÍ]*" = False Then
isPureString = False
End If
Next
End Function
If i add 1 more parameter, its also possible to define the allowed characters upon calling the function.
Ok, it seems my question was a bit of a duplicate, even though that did not pop in my search results.
So credits for #QHarr for posting the link.
The solution I can forge from that idea for my "oneliner" is:
If myText Like WorksheetFunction.Rept("[a-zA-Z]", Len(myText))=false then 'do something.
Using .rept is inspiringly clever and elegant in my oppinion.
So what is does: Multiplies the search criteria for each charater instead of looping through the characters.
EDIT:
In an overaboundance of nice and elegant solutions, the most recent leader is:
If not myText Like "*[!A-Za-z]*" then '... do something
Statistics update:
I have tested the last 3 solutions' performance:
I have pasted # in the below text strin at the beginning, at the end or nowhere.
The criteria were: "*[a-zA-Z \S.,]*"
For 100000 repetitions
text = "This will be a very Long text, with one unwanted in the middle, to be able to test the difference in performance of the approaches."
1.) Using the [!...] -> 30ms with error, 80ms if no error
2.) Using .Rept -> around 1800ms for all cases
3.) Using characterLoop+Mid -> around 3000ms if no error / 40-80ms ms if early error
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