+ Reply to Thread
Results 1 to 11 of 11

Search columns and paste rows

Hybrid View

  1. #1
    Registered User
    Join Date
    02-25-2009
    Location
    London
    MS-Off Ver
    Excel 2007
    Posts
    7

    Search columns and paste rows

    Hi I have an excel spreadsheet with:
    2 sheets,Sheet1 and Sheet2
    4 columns (A,B,C,D)
    100,000s of rows.

    I need to search 3 columns, B,C and D, for particular values.
    Column B needs to be = 0
    Columns C and D need to be "<=0.1"

    When all three criteria in the row are matched I need the value from column A in that row to be pasted to Sheet2 and then for the macro to continue searching the rows.

    I have attached an extract of the data as the files size is too large to upload.
    Attached Files Attached Files
    Last edited by chambone; 03-02-2009 at 10:51 AM.

  2. #2
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Search columns and paste rows

    Welcome to the Board.

    Something along the lines of the below perhaps ?

    Public Sub FilterData()
    Dim rngArea As Range
    Sheets("sheet2").Columns(1).Clear
    Sheets("Sheet1").Select
    With Range(Cells(1, Columns.Count), Cells(Rows.Count, "A").End(xlUp).Offset(, Columns.Count - 1))
        .FormulaR1C1 = "=IF(AND(RC2=0,RC3<=0.1,RC4<=0.1),1,""x"")"
        For Each rngArea In .SpecialCells(xlCellTypeFormulas, xlNumbers).Areas
            Sheets("Sheet2").Cells(Rows.Count, "A").End(xlUp).Offset(1).Resize(rngArea.Rows.Count).Value = rngArea.Offset(, 1 - Columns.Count).Value
        Next rngArea
        .Clear
    End With
    End Sub

  3. #3
    Registered User
    Join Date
    02-25-2009
    Location
    London
    MS-Off Ver
    Excel 2007
    Posts
    7

    Re: Search columns and paste rows

    Thank you very much for your help DonkeyOte, worked perfectly.

    I now have a different but similar problem.

    I need to search Column B for the number 4 and then copy the values in Columns G,H,I and J into sheet 2.

    The attached file is just an extract of all the data. As you can see the columns increse from I to J in row 24. In the actual file this occurs in row 326198.

    I dont know if it makes a difference but in the actual data the number of columns occupied by values decreses
    Attached Files Attached Files

  4. #4
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Search columns and paste rows

    Public Sub FilterData()
    Dim rngArea As Range, xlCalc As XlCalculation
    On Error GoTo Handler
    With Application
        xlCalc = Application.Calculation
        .Calculation = xlCalculationManual
        .Application.ScreenUpdating = False
        .EnableEvents = False
    End With
    Sheets("sheet2").Columns(1).Clear
    Sheets("Sheet1").Select
    With Range(Cells(1, Columns.Count), Cells(Rows.Count, "A").End(xlUp).Offset(, Columns.Count - 1))
        .FormulaR1C1 = "=IF(RC2=4,1,""x"")"
        For Each rngArea In .SpecialCells(xlCellTypeFormulas, xlNumbers).Areas
            Sheets("Sheet2").Cells(Rows.Count, "A").End(xlUp).Offset(1).Resize(rngArea.Rows.Count, 4).Value = rngArea.Offset(, 7 - Columns.Count).Resize(, 4).Value
        Next rngArea
        .Clear
    End With
    ExitPoint:
    With Application
        .Calculation = xlCalc
        .Application.ScreenUpdating = True
        .EnableEvents = True
    End With
    Exit Sub
    
    Handler:
    MsgBox "Error etc...", vbCritical, "Error"
    Resume ExitPoint
    
    End Sub
    Others may suggest Auto Filter... given the volume of data I don't know how feasible that is... I added in some Application events given that fact (re: calculation, screenupdating and events)

  5. #5
    Registered User
    Join Date
    02-25-2009
    Location
    London
    MS-Off Ver
    Excel 2007
    Posts
    7

    Re: Search columns and paste rows

    I get an error when I run it in the full data sheet. The start of the data sheet is the file attached previously
    Last edited by chambone; 02-26-2009 at 12:27 PM.

  6. #6
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Search columns and paste rows

    The code I posted worked for me with your prior sample file
    (I'd not have posted it otherwise)

  7. #7
    Registered User
    Join Date
    02-25-2009
    Location
    London
    MS-Off Ver
    Excel 2007
    Posts
    7

    Re: Search columns and paste rows

    My apologies, my sheets in the main file were listed as Sheet1 and Sheet3 now works fine thanks very much.

  8. #8
    Registered User
    Join Date
    02-25-2009
    Location
    London
    MS-Off Ver
    Excel 2007
    Posts
    7

    Re: Search columns and paste rows

    DonkeyOte similar problem to your last response.

    I need all values selected where:
    Column A=-0.1
    Column B=0.2
    Column B=-0.1
    Column A=0 and Column B is between 0.1 and 0.2
    Column A=0 and Column B is between 0 and -0.1

    It does not matter about the value in Column C.

    Would be great if you could help out

  9. #9
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Search columns and paste rows

    Re: your PM ... I'm not sure I understand the scenarios you posted previously.... are these independent processes ?

    Please provide a sample with "before(s)" & "after(s)" such that I can see the logic in action.

  10. #10
    Registered User
    Join Date
    02-25-2009
    Location
    London
    MS-Off Ver
    Excel 2007
    Posts
    7

    Re: Search columns and paste rows

    Okay if I have the three columns,

    Copy the value of column A into Sheet2 if column B=0 and or
    Copy the value of column A if column C=0.2 or -0.1

  11. #11
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Search columns and paste rows

    I neither have the time nor inclination to create a test file for this so the below is untested, next time please provide a sample file as requested.

    Public Sub FilterData()
    Dim rngArea As Range, xlCalc As XlCalculation
    On Error GoTo Handler
    With Application
        xlCalc = Application.Calculation
        .Calculation = xlCalculationManual
        .Application.ScreenUpdating = False
        .EnableEvents = False
    End With
    Sheets("sheet2").Columns(1).Clear
    Sheets("Sheet1").Select
    With Range(Cells(1, Columns.Count), Cells(Rows.Count, "A").End(xlUp).Offset(, Columns.Count - 1))
        .FormulaR1C1 = "=IF(OR(RC2=0,RC3=0.2,RC3=-0.1),1,""x"")"
        For Each rngArea In .SpecialCells(xlCellTypeFormulas, xlNumbers).Areas
            Sheets("Sheet2").Cells(Rows.Count, "A").End(xlUp).Offset(1).Resize(rngArea.Rows.Count).Value = rngArea.Offset(, 1 - Columns.Count).Value
        Next rngArea
        .Clear
    End With
    ExitPoint:
    With Application
        .Calculation = xlCalc
        .Application.ScreenUpdating = True
        .EnableEvents = True
    End With
    Exit Sub
    
    Handler:
    MsgBox "Error etc...", vbCritical, "Error"
    Resume ExitPoint
    
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1