简体   繁体   English

在VBA中,如何从文本中提取数字前的字符串

[英]In VBA, how to extract the string before a number from the text

From ActiveWorkbook.name , I would like to extract the strings that are before (left side of ) the numbers.ActiveWorkbook.name中,我想提取数字之前(左侧)的字符串。 Since I want to use the same code in multiple workbooks, the file names would be variable, but every file name has date info in the middle (yyyymmdd).由于我想在多个工作簿中使用相同的代码,因此文件名将是可变的,但每个文件名的中间都有日期信息(yyyymmdd)。

In case of excel file, I can use the below formula, but can I apply the same kind of method in VBA?对于 excel 文件,我可以使用下面的公式,但我可以在 VBA 中应用相同的方法吗?

=LEFT(A1,MIN(FIND({0,1,2,3,4,5,6,7,8,9},ASC(A1)&1234567890))-1)

Example: MyExcelWorkbook_Management_20200602_MyName.xlsm示例:MyExcelWorkbook_Management_20200602_MyName.xlsm

In above case, I want to extract "MyExcelWorkbook_Management_".在上述情况下,我想提取“MyExcelWorkbook_Management_”。

You could use Regular Expressions to extract any letters / underscores before the number as well您也可以使用正则表达式来提取数字之前的任何字母/下划线

Dim str As String

str = "MyExcelWorkbook_Management_20200602_MyName.xlsm"

With CreateObject("vbscript.regexp")
    .Pattern = "^\D*"
    .Global = True

    MsgBox .Execute(str)(0)
End With

Gives:给出:

MyExcelWorkbook_Management_ MyExcelWorkbook_Management_

The most basic thing you could do is to replicate something that worked for you in Excel through Evaluate :您可以做的最基本的事情是通过Evaluate在 Excel 中复制对您有用的东西:

Sub Test()
    Dim str As String: str = "MyExcelWorkbook_Management_20200602_MyName.xlsm"
    Debug.Print Evaluate(Replace("=LEFT(""X"",MIN(FIND({0,1,2,3,4,5,6,7,8,9},ASC(""X"")&1234567890))-1)", "X", str))
End Sub

Pretty?漂亮的? Not really, but it does the job and got it's limitations.不是真的,但它完成了工作并得到了它的局限性。

So basically you want to use the Mid function to look for the first numerical character in your input string, and then cut your input string to that position.所以基本上你想使用Mid function 来查找输入字符串中的第一个数字字符,然后将输入字符串剪切为 position。 That means we need to loop through the string from left to right, look at one character at a time and see if it is a digit or not.这意味着我们需要从左到右遍历字符串,一次查看一个字符,看看它是否是数字。 This code does exactly that:这段代码正是这样做的:

Option Explicit

Sub extratLeftText()

Dim someString As String
Dim result As String
someString = "Hello World1234"

Dim i As Long
Dim c As String 'one character of your string

For i = 1 To Len(someString)
    c = Mid(someString, i, 1)
    If IsNumeric(c) = True Then 'should write  "If IsNumeric(c) = True AND i>1 Then" to avoid an "out of bounds" error
        result = Left(someString, i - 1)
        Exit For
    End If
Next i

MsgBox result
End Sub

Last thing you need to do is to load in some workbook name into your VBA function.您需要做的最后一件事是将一些工作簿名称加载到您的 VBA function 中。 Generally this is done with the .Name method of the workbook object:通常这是使用workbook object 的.Name方法完成的:

Sub workbookName()

Dim wb As Workbook
Set wb = ActiveWorkbook

MsgBox wb.Name

End Sub

Of course you would need to find some way to replace the Set wb = ActiveWorkbook line with code that suits your purpose.当然,您需要找到某种方法来用适合您目的的代码替换Set wb = ActiveWorkbook行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM