You are here:

Excel/copy row if value macro


I am using Excel 2010
I am looking to get a Macro for:
I have 2 worksheets named- (SearchPage and InputLotoSheet)
On the Search Page sheet i have a drop down with (Tag#, Seq#, Panel#, CB Loc, Emp #) and a cell value in V1
and and on the InputLotoPage I have Column with (Tag# in B, Seq# in D, Panel # in H, CB Loc in I and Emp # in L)
What I am trying to do is if Drop down has example Tag# selected and the value in cell V1 is Equal to Tag# on B12:B411 on InputLotoSheet that it will copy all Rows that Match V1 from Columns (B,D,E,F,G,H,I,J,K,L,M,N,O,P,Q)from InputLotoSheet and Paste Values(not formula) to SearchPage starting at C13.

Then if Value V1 is removed all pasted data on SearchPage goes away until another dropdown box is selected and value is entered in V1

I would normally do this with an array filter (which is formula based) but you specifically asked for a macro solution.  do you want the macro to run when one of the cells is changed, or is to run via a button?  

I've ASSUMED that it should be when V1 is changed - at which point this macro should do what you want

Private Sub Worksheet_Change(ByVal Target As Range)
On Error GoTo err_trap
If Target.Address = "$V$1" Then
Dim looper As Long, matcher As Long, outvar As Long
Dim Matchvar As String
outvar = 13
Select Case Range("DropDown").Value
Case "Tag#"
matcher = 2
Case "Seq#"
matcher = 4
Case "Panel#"
matcher = 8
Case "CB Loc"
matcher = 9
Case "Emp#"
matcher = 12
End Select
   Matchvar = Target.Value
   If Target.Value <> "" Then
       'fill the range with matching data
       With Sheets("InputLotoSheet")
         For looper = 12 To 411
         If .Cells(looper, matcher).Value = Matchvar Then
         Sheets("SearchPage").Range("C" & outvar).Value = .Cells(looper, 2).Value
         Sheets("SearchPage").Range("D" & outvar).Value = .Cells(looper, 4).Value
         Sheets("SearchPage").Range("E" & outvar).Value = .Cells(looper, 5).Value
         Sheets("SearchPage").Range("F" & outvar).Value = .Cells(looper, 6).Value
         Sheets("SearchPage").Range("G" & outvar).Value = .Cells(looper, 7).Value
         Sheets("SearchPage").Range("H" & outvar).Value = .Cells(looper, 8).Value
         Sheets("SearchPage").Range("I" & outvar).Value = .Cells(looper, 9).Value
         Sheets("SearchPage").Range("J" & outvar).Value = .Cells(looper, 10).Value
         Sheets("SearchPage").Range("K" & outvar).Value = .Cells(looper, 11).Value
         Sheets("SearchPage").Range("L" & outvar).Value = .Cells(looper, 12).Value
         Sheets("SearchPage").Range("M" & outvar).Value = .Cells(looper, 13).Value
         Sheets("SearchPage").Range("N" & outvar).Value = .Cells(looper, 14).Value
         Sheets("SearchPage").Range("O" & outvar).Value = .Cells(looper, 15).Value
         Sheets("SearchPage").Range("P" & outvar).Value = .Cells(looper, 16).Value
         Sheets("SearchPage").Range("Q" & outvar).Value = .Cells(looper, 17).Value
         outvar = outvar + 1
         End If
       End With
   End If
End If
Exit Sub
MsgBox "An error has occurred - please check your inputs"
Exit Sub
End Sub  
About Excel
This topic answers questions related to Microsoft Excel spreadsheet (or workbook) stand-alone or Mircrosoft Office Excel including Excel 2003, Excel 2007, Office 2000, and Office XP. You can get Excel help on Excel formulas(or functions), Excell macros, charting in Excel, advanced features, and the general use of Excel. This does not provide a general Excel tutorial nor the basics of using a spreadsheet. It provides specific answers to using Microsoft Excel only. If you do not see your Excel question answered in this area then please ask an Excel question here


All Answers

Answers by Expert:

Ask Experts


Aidan Heritage


I have provided first hand support since `95 for Microsoft Office majoring in Word and Excel - support for all versions from 2 onwards. I'm based in the UK, so please allow for time differences when asking me questions from other parts of the world!


My background is in the insurance industry and call centre areas, but have been called upon to provide many varied solutions.

I'm educated to UK A level standard, but as I left school some 30 years ago that is rather irrelevent - university of life has provided more of a background!

©2017 All rights reserved.