VBA - 根据另一列在同一行上拉取值

时间:2014-09-11 16:54:09

标签: excel vba excel-vba

我需要一个宏,它将向下滚动列B,直到它的值超过6.然后,在第一个传递的值上,从A列获取该行的值(I' ll引用此值作为X)然后转到同一本书中的另一张纸,并在C列大于或等于X后插入3行。我对vba很新,不知道我在做什么但是,我&#39在时间紧迫的时候,可以使用一些帮助。我不知道这是否会有所帮助,但我写了一些可能能够解释它的超伪代码。 (我不仅需要一个X,而且还需要Y在第二张纸上的另一个位置插入3行)

Survey.cell
    dls = Where(column("J") > 6.0 get.row)
    kop = Where(column("B") meets dls)

Survey.cell
    inc = Where(column("C") >85 get.row)
    lnd = Where(column("B") meets inc)

Journal.cell
    Where cloumn("M") >= kip insert.row & inerst.row & insert.row
    Where column("M") >= lnd insert.row & insert.row & insert.row

我能找到的最接近的是: How to find text in a column and saving the row number where it is first found - Excel VBA 但是,它并不完全是我需要的。我感谢任何提供的帮助。祝你有个美好的一天!

1 个答案:

答案 0 :(得分:2)

如果我的问题是正确的,那就应该这样做。如果工作表中还有非数字值,则会失败。

Sub DoIt()
    Dim i As Long
    Dim x As Double
    Dim Worksheet1 As Worksheet
    Dim Worksheet2 As Worksheet
    Dim ColumnA As Long
    Dim ColumnB As Long
    Dim ColumnC As Long

    Set Worksheet1 = ActiveWorkbook.Worksheets("Sheet1") ' Change name of sheet if necessary
    Set Worksheet2 = ActiveWorkbook.Worksheets("Sheet2") ' Change name of sheet if necessary
    ColumnA = 1  ' Change if you want other columns, 1 = A-column, 2 = B etc
    ColumnB = 2
    ColumnC = 3


    For i = 1 To Worksheet1.UsedRange.Rows.Count
        If Worksheet1.Cells(i, ColumnB).Value > 6 Then
            x = Worksheet1.Cells(i, ColumnA).Value
            Exit For
        End If
    Next

    If x = 0 Then MsgBox "No value greater then 6 found in colum b in sheet 1"

    For i = 1 To Worksheet2.UsedRange.Rows.Count
        If Worksheet2.Cells(i, ColumnC).Value >= x Then
            Worksheet2.Rows(i + 1).Insert
            Worksheet2.Rows(i + 1).Insert
            Worksheet2.Rows(i + 1).Insert
            Exit Sub
        End If
    Next

    MsgBox "No value greater then " & x & " found in column C in sheet 2"
End Sub