如何根据宏中的条件选择值

时间:2017-03-10 07:21:46

标签: vba excel-vba excel

我想比较数据,所以我必须根据条件选择一个值。我拥有的示例数据如下:

enter image description here

条件是:

我想选择PO NO的值。在文本“PO NO。”之后总是放置2列

我如何获得这个价值?之后,将其复制并粘贴到另一列(例如:A列)

3 个答案:

答案 0 :(得分:0)

这取决于你想如何使用这些值,如果你只想将它们放在当前工作簿的某些连续范围内,那么我认为Filter函数就足够了,如果你想进行一些进一步的计算,你可以想写一些VBA代码:

  1. 在当前工作表中按ALT + F11。
  2. 按ALT + I然后按M.
  3. 按Ctrl + G打开"立即"窗口
  4. 然后写下以下几行:

    Sub myValues()
    
        Dim rCount As Long
        Dim i As Long
    
        Let rCount = ThisWorkbook.ActiveSheet.Cells(Rows.Count, 6).End(xlUp).Row
    
        For i = 1 to rCount
            If WorksheetFunction.Trim(ThisWorkbook.ActiveSheet.Cells(i,6).Text) = "PO No." Then
                Debug.Print ThisWorkbook.ActiveSheet.Cells(i,8).Text
            End If
        Next
    
    End Sub
    
  5. 现在你可以在"立即"中获得所有PO NO值。窗口。

答案 1 :(得分:0)

您可以使用此公式提取所需的值。

  

= INDEX(F44:H49,MATCH(" PO No。",F44:F49,0),3)

有待解决的问题是如何定义范围F44:F49。你的问题没有提示如何做到这一点。也许知道你想要出现的价值会提供一个线索。

答案 2 :(得分:0)

您可以迭代列中的每个单元格并收集您的帖子编号,偏移2列,就像我在评论中提到的那样

Sub Test()
    Dim WS As Worksheet
    Dim ParamRange As Range
    Dim LastRow As Long
    Dim Cell As Range
    Dim i As Long

    Set WS = ActiveSheet                                   'or whatever sheet your want


    With WS
        LastRow = .Cells(.Rows.Count, "F").End(xlUp).Row
        Set ParamRange = .Range("F1:F" & LastRow)
    End With


    For Each Cell In ParamRange                             'iterate over column
        If Cell.Value2 = "PO NO." Then
            i = i + 1
            'Debug.Print to Immediate
            Debug.Print i, CurrentSearch.Offset(ColumnOffset:=2).Value2
            'Paste in "A" column
            CurrentSearch.Offset(ColumnOffset:=-5).Value2 = CurrentSearch.Offset(ColumnOffset:=2).Value2
        End If
    Next
End Sub

所以你只需要收集所有Cell.Offset(ColumnOffset:=2).Value2值。

或者,不对单元格进行迭代(并且更快),但有点复杂:

Sub Test()
    Dim WS As Worksheet
    Dim ParamRange As Range
    Dim CurrentSearch As Range
    Dim FirstSearch As Range
    Dim LastRow As Long
    Dim Cell As Range
    Dim i As Long

    Set WS = ActiveSheet                                   'or whatever sheet your want


    With WS
        LastRow = .Cells(.Rows.Count, "F").End(xlUp).Row
        Set ParamRange = .Range("F1:F" & LastRow)
    End With


    'Get first search
    Set CurrentSearch = ParamRange.Find(What:="PO NO.", LookIn:=xlValues, _
            LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
            MatchCase:=False, SearchFormat:=False)

    If Not CurrentSearch Is Nothing Then
        i = i + 1
        'Debug.Print to Immediate
        Debug.Print i, CurrentSearch.Offset(ColumnOffset:=2).Value2
        'Paste in "A" column
        CurrentSearch.Offset(ColumnOffset:=-5).Value2 = CurrentSearch.Offset(ColumnOffset:=2).Value2
        Set FirstSearch = CurrentSearch

        Do
            'Get next search
            Set CurrentSearch = ParamRange.FindNext(After:=CurrentSearch)

            If Not CurrentSearch Is Nothing Then
                If CurrentSearch.Address = FirstSearch.Address Then Exit Do
                i = i + 1
                'Debug.Print to Immediate
                Debug.Print i, CurrentSearch.Offset(ColumnOffset:=2).Value2
                'Paste in "A" column
                CurrentSearch.Offset(ColumnOffset:=-5).Value2 = CurrentSearch.Offset(ColumnOffset:=2).Value2
            Else
                Exit Do
            End If
        Loop
    End If
End Sub

链接:

Range.Offset

Find last row, column or last cell

.Find and .FindNext in Excel VBA