繁体   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