簡體   English   中英

從字符串中提取范圍

[英]Extract range from string

我有一個導出到多個Excel工作表的報告。 每張紙上的第一行與以下內容類似。

僅將數據粘貼在范圍A1:K44中。 數據來自“零件查詢”中的“歷史記錄”轉儲。

我需要從該字符串中提取范圍。 我試圖使用split和find函數,但是范圍的長度發生了變化(即A1:AB53A15:C20等)。

我歡迎任何有關如何提取范圍的意見。

只需使用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

輸出:

A1:K44

如果顯示的文本不太一致,則可以使用正則表達式提取出現在字符串中的第一個范圍:

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