簡體   English   中英

使用VBA搜索特定的字符串格式,然后粘貼到新的Excel工作表中

[英]Search for specific string format using VBA and paste to a new excel worksheet

我正在嘗試將一些數據整合到我創建的特定excel模板中。 我的數據標題為PAxxx.xx,其中x可以是0-9之間的任何數字。 有什么辦法可以在當前工作簿中搜索該特定標題“ PAxxx.xx”,並將其填充到我創建的模板字段中。

我目前在VBA中具有以下搜索功能:

Sub CopyPasteCellData()

Dim FirstAddress As String
Dim searchTerms As Variant
Dim Rcount As Long
Dim I As Long
Dim Rng As Range

Dim currentWorkbook As Workbook
Dim newWorkbook As Workbook
Dim currentWorksheet As Worksheet
Dim newWorksheet As Worksheet

Set currentWorkbook = Workbooks("LVX Release 2015 (2).xlsm")
Set currentWorksheet = currentWorkbook.Sheets("PA5179.01")

Set newWorkbook = Workbooks("Test.xlsx")
Set newWorksheet = newWorkbook.Sheets("Sheet1")

'newWorksheet.Range("C2").Value = currentWorksheet.Range("A1").Value

searchTerms = Array("PA")

With currentWorksheet.UsedRange
    Rcount = 0
    For I = LBound(searchTerms) To UBound(searchTerms)
        Set Rng = .Find(What:=searchTerms(I), _
                        After:=.Cells(.Cells.Count), _
                        LookIn:=xlFormulas, _
                        LookAt:=xlPart, _
                        SearchOrder:=xlByRows, _
                        SearchDirection:=xlNext, _
                        MatchCase:=False)
            If Not Rng Is Nothing Then
                FirstAddress = Rng.Address
                Do
                    Rcount = Rcount + 1
                    newWorksheet.Range("A" & Rcount).Value = Rng.Value
                    Set Rng = .FindNext(Rng)
                Loop While Not Rng Is Nothing And Rng.Address <> FirstAddress
            End If
    Next I
End With              
End Sub 

只是不確定如何在工作表中搜索所有數據集PAxxx.xx。

提前致謝 :)

這是一個基本原理,如何遍歷所有工作表並查找PAxxx.xx->如果需要更改驗證,請閱讀Like運算符的說明->

Sub LoopTroughWorkSheetsAndFindPA()
Dim wb As Workbook: Set wb = ThisWorkbook 'anyreference of a workbook you want
Dim ws As Worksheet

For Each ws In wb.Worksheets
    If ws.Name Like "PA###.##" Then
        'do some operations here for example ->
        Debug.Print ws.Name
    End If
Next

End Sub

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM