The Problem (Q-score 28, ranked #4th of 95 in the VBA Core archive)
The scenario as originally posted in 2013
In VBA, what’s the most straight forward way to test if a string begins with a substring? Java has startsWith. Is there a VBA equivalent?
Why community consensus is tight on this one
Across 95 VBA Core entries in the archive, the accepted answer here holds elite answer (top 10 %%) status — meaning voters are unusually aligned on the right fix.
The Verified Solution — elite answer (top 10 %%) (+53)
4-line VBA Core pattern (copy-ready)
There are several ways to do this:
InStr
You can use the InStr build-in function to test if a String contains a substring. InStr will either return the index of the first match, or 0. So you can test if a String begins with a substring by doing the following:
If InStr(1, "Hello World", "Hello W") = 1 Then
MsgBox "Yep, this string begins with Hello W!"
End If
If InStr returns 1, then the String (“Hello World”), begins with the substring (“Hello W”).
Like
You can also use the like comparison operator along with some basic pattern matching:
If "Hello World" Like "Hello W*" Then
MsgBox "Yep, this string begins with Hello W!"
End If
In this, we use an asterisk (*) to test if the String begins with our substring.
Error-handling details to lift with the snippet
This answer wires error flow through MsgBox / Err.Description. Keep that intact: stripping it to “make it cleaner” removes the signal you’ll need when the macro fails silently on a user machine.
When to Use It — classic (2013–2016)
A top-10 VBA Core pattern — why it still holds up
Ranks #4th of 95 in the VBA Core archive. The only pattern ranked immediately above it is “Detect whether Office is 32bit or 64bit via the registry” — compare both if you’re choosing between approaches.
What changed between 2013 and 2026
The answer is 13 years old. The VBA Core object model has been stable across Office 2013, 2016, 2019, 2021, 365, and 2024/2026 LTSC, so the pattern still compiles. Changes that might affect you: 64-bit API declarations (use PtrSafe), blocked macros in downloaded files (Mark-of-the-Web), and the shift toward Office Scripts for web-first workflows.