Ryan
Ryan

Reputation: 77

VBA - Pulling a value on the same row based on another column

I need a macro that will scroll down column B until it's values pass 6. Then, on the first value that passes take the value on that row from column A (I'll refer to this value as X) Then go to another sheet in the same book and insert 3 rows after column C has become greater than or equal to X. I am very new to vba and have no idea what I'm doing but, I'm in a time crunch and could use some help. I don't know if this will help but I wrote out some super pseudo code that might be able to explain it. (not only do I need an X but I also need a Y to insert 3 rows in another spot on the second sheet)

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

The closest thing I could find to this is here: How to find text in a column and saving the row number where it is first found - Excel VBA but, it's not exactly what I need. I appreciate any help offered. Have a good day!

Upvotes: 0

Views: 8710

Answers (1)

Roemer
Roemer

Reputation: 1261

If I got your question right, this should do it. If there are also non-numeric values in the sheet, this will fail.

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

Upvotes: 2

Related Questions