+ Reply to Thread
Results 1 to 2 of 2

Thread: Performance issue - Looping through Cells

  1. #1
    Registered User
    Join Date
    09-12-2011
    Location
    San Diego ,CA
    MS-Off Ver
    Excel 2007
    Posts
    1

    Performance issue - Looping through Cells

    I have a failry complex application that involves data transfer between two separate WorkBooks.

    The application works perfectly however, I do have a performance issue that I am hoping to solve. I did extensive debugging and tracing to isolate the portion of the code that was bogging down. I then read extensively on the symtpoms and learned that the cause was likely do to the "For Each ... Next" construct I was using to loop through the cells.

    Dim rCell As Range, rMultipleColumns As Range
    Dim sCellFormat as String
    Dim vCellValue as Variant
    
    '--- I've skipped all the other code in between here
    
    'Loop through each cell in pre-defined multi-column range
    For Each rCell In rMultipleColumns
        If VarType(rCell) <> vbDate Then   'Do not re-format dates ... they process correctly as-is
            vCellValue = rCell.value   'Get value of current cell
            sCellFormat = rCell.NumberFormat
            If Right(sCellFormat, 1) = "%" Then   'Need to re-format cells that are formatted as a percentage
                rCell.value = "'" & Format(vCellValue, sCellFormat)
            Else   'Otherwise, format the cell as text
                rCell.NumberFormat = "@"
            End If
        End If
    Next rCell

    One possible solution I located involved a method whereby the Range "value" is first copied to an array, processed quickly, then copied back to the range. I wasn't able to figure out how to implement that method with the "numberformat" property though which is necessary in my logic above. In case it helps, here is the "copy to an array" method which runs exceedingly fast:

    Dim lCurrentCell as Long
    Dim rSelectedColumn as Range
    Dim vTempRange as Variant
    
    '--- I've skipped all the other code in between here
    
    vTempRange = rSelectedColumn.value   'Copy range to array
        
    For lCurrentCell = LBound(vTempRange) To UBound(vTempRange)
        Select Case Nz(Trim(vTempRange(lCurrentCell, 1)), "")
        Case "N", "Y"   'Value already set as 'N' or 'Y' - leave it alone
        Case Else   'Blank, space or null ... set to default of 'N'
            vTempRange(lCurrentCell, 1) = "N"
        End Select
    Next lCurrentCell
    
    rSelectedColumn.value = cTempRange   'Now, copy the array back to the range

    Thank you for anything you can offer.

    Doug

  2. #2
    Forum Guru jaslake's Avatar
    Join Date
    02-21-2009
    Location
    mineral city, ohio
    MS-Off Ver
    Excel 2007; Excel 2000
    Posts
    4,004

    Re: Performance issue - Looping through Cells

    Hi Doug

    It's a bit difficult without seeing the data but, in reading your code, have you considered filtering the data and making changes to the visible cells?

    If you'll post a sample of the data (before and after) I MAY be able to help.
    John

    If you have issues with Code I've provided, I appreciate your feedback.

    In the event Code provided resolves your issue, please click EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED.

    If you're satisfied by any members response to your issue please use the star icon at the lower left of their post.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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.2.0