简体   繁体   English

从字符串中提取范围

[英]Extract range from string

I have a report that exports to multiple Excel sheets. 我有一个导出到多个Excel工作表的报告。 The first row on each sheet has a similar statement to the below. 每张纸上的第一行与以下内容类似。

Paste Data in range A1:K44 only. 仅将数据粘贴在范围A1:K44中。 Data to come from "History" dump in Part Inquire. 数据来自“零件查询”中的“历史记录”转储。

I need to extract the range from that string. 我需要从该字符串中提取范围。 I've tried to use the split and find functions, but the length of the range changes (ie A1:AB53 , A15:C20 , etc.). 我试图使用split和find函数,但是范围的长度发生了变化(即A1:AB53A15:C20等)。

I welcome any input as to how to extract the range. 我欢迎任何有关如何提取范围的意见。

Just use Split() to split your string into an array and then grab the 5th element (index = 4): 只需使用Split()将您的字符串拆分为一个数组,然后获取第5个元素(索引= 4):

Const SOME_TEXT As String = "Paste Data in range A1:K44 only. Data to come from ""History"" dump in Part Inquire."

Dim strRange As String
strRange = Split(SOME_TEXT)(4)

Debug.Print strRange

Output: 输出:

A1:K44

If the text that appears isn't quite so consistent, you can use a regex to extract the first range that appears within your string: 如果显示的文本不太一致,则可以使用正则表达式提取出现在字符串中的第一个范围:

Const SOME_TEXT As String = "This string has A1:K44 in it but is inconsistent with the rest."

Dim re
Set re = CreateObject("VBScript.RegExp")
re.Pattern = "\b[A-Z]{1,3}\d+:[A-Z]{1,3}\d+\b"

If re.Test(SOME_TEXT) Then
    Debug.Print re.Execute(SOME_TEXT)(0)
End If

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

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