How do I fix this code

Macro_Nerd99

Board Regular
Joined
Nov 13, 2021
Messages
61
Office Version
  1. 365
I found this formula online that works great to extract a 6 digit code from a cell string: =MID(A1,MIN(FIND({0,1,2,3,4,5,6,7,8,9},A1&1234567890)),6)

However, I'm trying to use it in VBA code and extract it from a variable instead.
However, I get a syntax error when attempting this. How do I fix it?

Public Function Employee_ID() As String
Employee_ID =MID(thisworkbook.name,MIN(FIND({0,1,2,3,4,5,6,7,8,9},thisworkbook.name&1234567890)),6)
End Function
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
How about
Excel Formula:
Public Function Employee_ID() As String
Employee_ID = Evaluate("MID(" & ThisWorkbook.Name & ",MIN(FIND({0,1,2,3,4,5,6,7,8,9}," & ThisWorkbook.Name & "&1234567890)),6)")
End Function
 
Upvote 0
How about
Excel Formula:
Public Function Employee_ID() As String
Employee_ID = Evaluate("MID(" & ThisWorkbook.Name & ",MIN(FIND({0,1,2,3,4,5,6,7,8,9}," & ThisWorkbook.Name & "&1234567890)),6)")
End Function
I get a "Type mismatch" runtime error
 
Upvote 0
Oops. forgot the extra quotes, try
VBA Code:
Public Function Employee_ID() As String
Employee_ID = Evaluate("MID(""" & ThisWorkbook.Name & """,MIN(FIND({0,1,2,3,4,5,6,7,8,9},""" & ThisWorkbook.Name & """&1234567890)),6)")
End Function
 
Upvote 0
Solution
Oops. forgot the extra quotes, try
VBA Code:
Public Function Employee_ID() As String
Employee_ID = Evaluate("MID(""" & ThisWorkbook.Name & """,MIN(FIND({0,1,2,3,4,5,6,7,8,9},""" & ThisWorkbook.Name & """&1234567890)),6)")
End Function
Now I get an "overflow" error. Thanks for your quick replies.
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,223,237
Messages
6,170,924
Members
452,366
Latest member
TePunaBloke

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top