如何使用字符串的列找到另一片相同的字符串,并粘贴到表1旁边发现价值相应的价值?

问题描述 投票:0回答:1

我的工作产生了一些自动化的报告。要重申的是,我想我的代码来看看在Sheet1列E(忽略空格)每个“串”,搜索在另一个工作表/工作簿中值,并贴上“单身”包含在Sheet1列d(忽略空格,如果E是空白的,d将是空白的),以在搜索中发现较早的字符串的左侧。

接收数据片被广泛地分散并以各种格式,但一个不变的是用于数据输入的每个位置具有在也是在主数据存储表中找到相邻的列的唯一标签。我没有在VBA那么多经验着呢,不知道怎么有些人会在这不会打破添加了新列分钟以动态的方式解决这个问题。

我当前的代码是一个混乱的烂摊子,所以任何指针,想法,一般的策略,将不胜感激。我试图让使用表远()。选择和这样的其他引用,但我不知道该怎么做呢。

Dim location As String
Dim rownum As Integer
Dim cellfinder As Integer

Sheets("Sheet2").Select                          'Ensures that we start on sheet 2

rownum = Range("G2").Value


For cellfinder = 1 To rownum                     'Loop goes for as many non-blank rows exist in column---- need
    'to add code to skip over blank rows with offset function or else loop will not go on long enough.

    Sheets("Sheet2").Select                      'selects Pi tag data sheet

    'hopefully adjusts the active cell relative to the loop count

    If ActiveCell.Value = "" Then                'hopefully detects blank cells and skips to next loop
        GoTo Skip
    End If

    location = ActiveCell.Value                  'calls the location tag string the variable "location"
    ActiveCell.Offset(0, -1).Select              'offsets from location tag to the "current value column"
    ActiveCell.Value.Copy                        'copies the value found in the current value column hopefully not the pi function


    Sheets("Sheet1").Select                      'Selects EOM sheet, can be whatever sheet we wish, or in another worksheet
    Range("A1").Select                           'establishes a starting point for find function

    Cells.Find(What:="location", After:=sht2.cells(1,1), LookIn:= _
               xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _
               xlNext, MatchCase:=False, SearchFormat:=False).Activate
    ActiveCell.Offset(0, -1).Select              'offsets the active cell to the value column in EOM sheet
    ActiveCell.Paste                             'pastes the activecell value copied from sheet 1
    ' find function finds the string saved to the location variable , makes it the active cell.

    Skip:
Next cellfinder
excel vba
1个回答
1
投票

有一点需要注意的是,当你把What:="location"在报价location,你告诉你的查找功能,专门寻找价值"location",而不是您已设置在环路的location的值的变量ActiveCell

另外,我会避免使用Goto除非你退出深度嵌套循环。你可以重写代码以避免使用SelectActiveCellGoto和纠正你的.Find这样的:

Dim location As String
Dim rownum As Long
Dim cellfinder As Long
Dim fRng As Range

With Sheets("Sheet1")

    rownum = .Range("E" & Rows.Count).End(xlUp).Row

    For cellfinder = 1 To rownum

        If .Range("E" & cellfinder) <> "" Then

            location = .Range("E" & cellfinder)

            Set fRng = Sheets("Sheet2").Cells.Find(What:=location, LookIn:=xlFormulas, LookAt:=xlPart) '<-Sheets("Sheet2") can be any sheet you need to perform the .Find against

            If Not fRng Is Nothing Then
                fRng.Offset(0, -1) = .Range("D" & cellfinder)
            Else
                'Do something when .Find doesn't find anything
            End If

        End If

    Next cellfinder

End With

使用With块可以一次指定所需的对象,并通过前缀方法与.以便利用它

With Sheets("Sheet1")
    .Range("A1")
End With

在功能上是一样的

Sheets("Sheet1").Range("A1")

使用.Range("E" & Rows.Count).End(xlUp).Row将返回列E上次使用的行数,那么你可以测试循环中的空白单元格,而不是担心你rownum数不是足够长的时间,当你让它= Range("G2")

规避Goto语句,使用相反的操作者和封装整个随后的执行代码的If语句内将实现而不鼓励面条代码相同的结果。相反,测试,看看是否ActiveCell = ""然后执行Goto当它,看看测试如果没有,只有执行以下代码时,它没有。

初始化fRng作为Range并设置它等于你的.Find函数的结果将让你测试查找功能,看它是否返回任何东西,这样你就不会报错了,当你尝试做一些与fRng时,它的Nothing

© www.soinside.com 2019 - 2024. All rights reserved.