Consulting

Results 1 to 9 of 9

Thread: Handling large amount of data in one excel sheet

  1. #1
    VBAX Newbie
    Joined
    Mar 2012
    Posts
    4
    Location

    Handling large amount of data in one excel sheet

    hi,
    I have a large amount of data (168035 * 374) which i want to process. My task is to search some specific record on user input.
    e.g., If user is asked to input some number. Then this number will be matched with all A1 (168035 rows)and if the match occurs in multiple places, it should fetch those records (rows) and save it to another worksheet.
    I have some code, which is not efficient. when i run the query, excel becomes non responsive (halts). Please give me some suggestion
    NOTE: i am using excel-2007
    Here is my code:
    Sub copying()
    Dim i As long, j As long
    Dim strsearch As String, lastline As long, tocopy As long
    strsearch = CStr(InputBox("Please Enter the video ID: "))
    lastline = Range("A1048576").End(xlUp).Row
    j = 1
    For i = 1 To lastline
        For Each c In Range("A" & i)
            If InStr(c.Text, strsearch) Then
                tocopy = 1
            End If
        Next c
         If tocopy = 1 Then
            Rows(i).Copy Destination:=Sheets(2).Rows(j)
            j = j + 1
        End If
    tocopy = 0
    Next i
    MsgBox "All matching data has been copied."
    End Sub

  2. #2
    I'd manually set up an autofilter on that column while recording a macro...
    Regards,

    Jan Karel Pieterse
    Excel MVP jkp-ads.com

  3. #3
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    why not access dbase?
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  4. #4
    VBAX Expert
    Joined
    Sep 2010
    Posts
    604
    Location
    Hi kkj,

    I agree with mancubus that using Access or other database is going to be the best option, but if it needs to be done in Excel, Jan Karel Pieterse advice to use the autofilter is going to be far more efficient than looping.

    Maybe something like this:
    [vba]
    Option Explicit
    Sub CopyFilteredResultsToAnotherSheet()
    Dim strsearch As String, Calc As Integer
    Dim Lastrow As Long, LastCol As Integer
    Dim rng As Range, SourceSheet As Worksheet, DestinationSheet As Worksheet

    Set SourceSheet = Worksheets("Sheet1") 'change to suit
    Set DestinationSheet = Worksheets("Sheet2") 'change to suit
    Application.ScreenUpdating = False
    Application.EnableEvents = False
    Calc = Application.Calculation
    Application.Calculation = xlCalculationManual

    DestinationSheet.UsedRange.ClearContents

    With SourceSheet
    Lastrow = .Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    LastCol = .Cells.Find("*", SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column

    Set rng = .Range(.Cells(1, 1), .Cells(Lastrow, LastCol))

    End With

    If Not rng Is Nothing Then

    strsearch = CStr(InputBox("Please Enter the video ID: "))

    If Trim(strsearch) = "" Then GoTo Finish

    rng.AutoFilter Field:=1, Criteria1:=strsearch

    rng.SpecialCells(xlCellTypeVisible).Copy DestinationSheet.Cells(1, 1)

    End If
    Finish:

    Application.ScreenUpdating = True

    If DestinationSheet.UsedRange.Rows.Count > 1 Then
    DestinationSheet.Activate
    MsgBox DestinationSheet.UsedRange.Rows.Count - 1 & " match's found"
    Else
    MsgBox "No matches"
    End If

    With SourceSheet
    If .AutoFilterMode = True Then
    .AutoFilterMode = False
    .EnableAutoFilter = True
    End If
    End With
    Application.EnableEvents = True
    Application.Calculation = Calc

    End Sub

    [/vba]

  5. #5
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    Cross-post: http://www.mrexcel.com/forum/showthread.php?t=621054.

    Please DO NOT cross post. It's very frustrating for people offering help when they see their efforts for nothing when it's been answered on another forum.

  6. #6
    Use ADO
    Set a referenence to Microsoft ActiveX Data Objects in the VBE then[VBA][/VBA]

    With xlCon
    .Provider = "Microsoft.Jet.OLEDB.4.0"
    .ConnectionString = "Data Source=" & FullFileName & ";" & _
    "Extended Properties=""Excel 8.0;HDR=Yes;"""
    .Open
    End With
    [VBA]

  7. #7
    Sorry sent before complete

    Also
    Dim xlCon as New ADODB.Connectiion
    Dim RS as New ADODB.Recordset

    RS.Open "SELECT * FROM [Sheet1$A1:EZ65000]", _
    conXL, adOpenKeyset, adLockReadOnly

    This will give you a recordset where all ADO facilities are available, in particular:
    RS.Filter = "Col1=" & Cstr(NumberToFind)

    Col1 should be a field name at the top of Column 1 - this will result in a recordset of zero (no matches) to the number of matches found which can then be processed at the time. It is very quick.

  8. #8
    VBAX Newbie
    Joined
    Mar 2012
    Posts
    4
    Location
    Sorry zack,
    I already apologized on that forum. I am a new user and i overlooked that rule mistakenly. Now i will not do that again.
    Thanks for guiding me.
    Last edited by kkj; 03-15-2012 at 07:32 AM.

  9. #9
    VBAX Contributor
    Joined
    Aug 2011
    Posts
    126
    Location

    Row Count / Loop

    1
    Last edited by Rlb53; 03-16-2012 at 05:23 PM. Reason: Posted in Error

Posting Permissions

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