查找,选择和复制多个随机单元格VBA宏

时间:2014-08-13 19:59:53

标签: excel vba excel-vba find

我遇到麻烦问题,我无法弄清楚如何做到这一点,我需要一个可以扫描工作簿的宏,查找3个值,“数据:”,“N°”和“ Rodovia:“,从它们偏移1列并选择该值,然后将其粘贴到另一张表中:

数据:| 2014年10月3日

N°:| L02.020.22C

Rodovida:| GO-020

所以必须选择“10/03/2014”,“L02.020.22C”和“GO-020”

我可以使用find和Resize我知道,但它们不在同一行或列中,它们位于随机行和列中,这就是问题,我尝试使用Range()。选择多个.Find()。offset()里面却没有用

之后我需要它来处理工作簿中的所有案例,所以我需要它来给我那个

Case1Data | Case1N°| Case1Rodovia

Case2Data | Case2N°| Case2Rodovia

Case3Data | Case3N°| Case3Rodovia

更新,代码到目前为止:

Sub Gather_Values()
    Dim Rng As Range
    With Sheets("01").Range("A:AJ")
        Set Rng = .Find(What:="Data: ", _
                    After:=.Cells(.Cells.Count), _
                    LookIn:=xlValues, _
                    LookAt:=xlWhole, _
                    SearchOrder:=xlByRows, _
                    SearchDirection:=xlNext, _
                    MatchCase:=False).Offset(0, 1)
        Selection.Copy
        Sheets.Add.Name = "New"
        Worksheets("New").Activate
        Range("A1").Select
                    Selection.PasteSpecial Paste:=xlPasteValues, _
                    Operation:=xlNone, SkipBlanks:=False, Transpose:=False
                    Application.CutCopyMode = False
    End With
End Sub

1 个答案:

答案 0 :(得分:0)

此子程序将整理名为“01”的单个工作表的3个值,搜索该工作表的UsedRange:

Sub Gather_Values()
    Dim Rng As Range
    Dim Sht As Worksheet

    'create new worksheet, name it "New"
    Set Sht = Sheets.Add
    Sht.Name = "New"
    'set column titles in the new sheet
    Sht.Range("B1").Value = "Data"
    Sht.Range("C1").Value = "N°"
    Sht.Range("D1").Value = "Rodovia"

    'search the entire UsedRange of sheet 01
    With Sheets("01").UsedRange
        Set Rng = .Find(What:="Data: ", _
                    After:=.Cells(.Cells.Count), _
                    LookIn:=xlValues, _
                    LookAt:=xlWhole, _
                    SearchOrder:=xlByRows, _
                    SearchDirection:=xlNext, _
                    MatchCase:=False).Offset(0, 1)
        Sht.Range("B2").Value = Rng.Value 'put value from the Find into B column of new sheet

        Set Rng = .Find(What:="N°", _
                    After:=.Cells(.Cells.Count), _
                    LookIn:=xlValues, _
                    LookAt:=xlWhole, _
                    SearchOrder:=xlByRows, _
                    SearchDirection:=xlNext, _
                    MatchCase:=False).Offset(0, 1)
        Sht.Range("C2").Value = Rng.Value 'put value from the Find into C column of new sheet

        Set Rng = .Find(What:="Rodovia:", _
                    After:=.Cells(.Cells.Count), _
                    LookIn:=xlValues, _
                    LookAt:=xlWhole, _
                    SearchOrder:=xlByRows, _
                    SearchDirection:=xlNext, _
                    MatchCase:=False).Offset(0, 1)
        Sht.Range("D2").Value = Rng.Value 'put value from the Find into D column of new sheet
    End With
End Sub

如果值在单张纸上出现多次,则需要确定哪些单元格区域将始终包含单独的记录,或者提供不同的解析数据的方式以确保您按顺序找到的值为相同的“案例”或“记录”。

查看正在解析的原始数据的抽样以查看是否有比使用Find()更好的整理方法将会很有帮助。最好看一个原始数据中存在多个“case”的实例。