Results 1 to 10 of 10

Thread: Weird Excel VBA "Find" and "Sort" issue.

Threaded View

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2001
    Location
    London UK
    Posts
    671

    Weird Excel VBA "Find" and "Sort" issue.

    I have a strange problem with automating Excel from VB. When using the Find function to cycle through nonblank
    cells in a large range it takes less than a second to run if I haven't used the sort function previously but more
    than 5 minutes if I have!

    (If anyone wants to check this code out just paste the following code into a standard VB module then run it with the sort line commented and uncommented and see!)

    Has anyone any explanation/solution/work around for this behaviour?

    VB Code:
    1. 'Paste this code into a standard vbmodule.
    2. Option Explicit
    3.  
    4. Private Sub Main()
    5.     If Dir("C:\Test.xls") = "" Then
    6.     Open "C:\Test.xls" For Output As #1
    7.     Close #1
    8.     End If
    9. Dim collCells As Object, collComments As Object, objComment As Object
    10. Dim i As Long, objXLApplication As Object, lngNumberOfCellsToFind As Long
    11. Const xlAscending = 1
    12. Const xlYes = 1
    13. Const xlTopToBottom = 1
    14. Set objXLApplication = CreateObject("Excel.Application")
    15. objXLApplication.Visible = False
    16. objXLApplication.Workbooks.Open ("C:\Test.xls")
    17. objXLApplication.ActiveSheet.Cells.ClearContents
    18.  
    19.  
    20. Set collComments = objXLApplication.ActiveSheet.Comments
    21.     For Each objComment In collComments
    22.     objComment.Delete
    23.     Next
    24. Set collComments = Nothing
    25.  
    26.  
    27.  
    28. Set collCells = objXLApplication.Range("A2:U52").Cells
    29.     For i = 1 To collCells.Count
    30.     collCells(i).Value = Int((100 * Rnd) + 1)
    31.     Next
    32. Set collCells = Nothing
    33.  
    34. objXLApplication.Range("B53").Value = "First dodgy cell"
    35. objXLApplication.Range("IV65536").Value = "Second dodgy cell"
    36. lngNumberOfCellsToFind = 2
    37.  
    38. 'THIS IS THE PROBLEM LINE BELOW!!!!!
    39. 'objXLApplication.ActiveSheet.Columns("A:U").Sort objXLApplication.Range("N2"), xlAscending, objXLApplication.Range("S2"), , xlAscending, , , xlYes, 1, False, xlTopToBottom
    40.  
    41. CommentNonBlankCells objXLApplication.Range("A53:IV65536"), lngNumberOfCellsToFind
    42. objXLApplication.DisplayAlerts = False
    43. objXLApplication.ActiveWorkbook.SaveAs FileName:="C:\Test.xls", FileFormat:=-4143, _
    44.         Password:="", WriteResPassword:="", ReadOnlyRecommended:=False, _
    45.         CreateBackup:=False
    46. objXLApplication.ActiveWorkbook.Close True
    47. objXLApplication.Quit
    48. Set objXLApplication = Nothing
    49. End Sub
    50. Private Sub CommentNonBlankCells(ByRef PassedRange As Object, ByRef lngNumberOfCellsToFind As Long)
    51. Dim objFoundCell As Object, strFirstAddressInRange As String, blnExitDo As Boolean
    52. Const xlFormulas = -4123
    53. Const xlPart = 2
    54. Const xlByRows = 1
    55. Const xlNext = 1
    56. Set objFoundCell = PassedRange.Find("*", , xlFormulas, xlPart, xlByRows, xlNext, False)
    57.     If TypeName(objFoundCell) = "Nothing" Then Exit Sub
    58. lngNumberOfCellsToFind = lngNumberOfCellsToFind - 1
    59. strFirstAddressInRange = objFoundCell.Address
    60. objFoundCell.AddComment ("Data outside expected area")
    61.     Do Until blnExitDo Or (lngNumberOfCellsToFind = 0)
    62.     DoEvents
    63.     Debug.Print Now()
    64.     'THIS IS THE LINE THAT CAN TAKE TOO LONG BELOW!!!!!
    65.     Set objFoundCell = PassedRange.FindNext(objFoundCell)
    66.     Debug.Print Now()
    67.         If objFoundCell.Address = strFirstAddressInRange Then
    68.         blnExitDo = True
    69.         Else
    70.         objFoundCell.AddComment ("Data outside expected area")
    71.         lngNumberOfCellsToFind = lngNumberOfCellsToFind - 1
    72.         End If
    73.     Loop
    74. Set objFoundCell = Nothing
    75. End Sub
    Last edited by MartinSmith; Mar 15th, 2003 at 08:55 AM.

Posting Permissions

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



Click Here to Expand Forum to Full Width