Results 1 to 8 of 8

Improve code performance for Sudoku

Threaded View

  1. #1
    Registered User
    Join Date
    05-27-2024
    Location
    southeast Michigan
    MS-Off Ver
    2010
    Posts
    30

    Question Improve code performance for Sudoku

    I wrote a spreadsheet to help me with Killer Sudoku. The spreadsheet has nine rows which are used to input the numbers seen in (up to) nine cells and a cell for the total value. The VBA macro starts by reading everything in - the numbers are read into the two-dimensional array vals, where the first index is the row number, the second index is the nth value in the row, and the array cnt holds the number of numbers in the row. The values are added and if the sum equals the total value, the numbers in the cells are stored. Following is the 'complex' code that does this:
        For x1 = 0 To cnt(1)
            For x2 = 0 To cnt(2)
                For x3 = 0 To cnt(3)
                    For x4 = 0 To cnt(4)
                        For x5 = 0 To cnt(5)
                            For x6 = 0 To cnt(6)
                                For x7 = 0 To cnt(7)
                                    For x8 = 0 To cnt(8)
                                        For x9 = 0 To cnt(9)
                                            If vals(1, x1) + vals(2, x2) + vals(3, x3) + vals(4, x4) + vals(5, x5) + vals(6, x6) + vals(7, x7) + vals(8, x8) + vals(9, x9) = total Then
                                                answers(cntA, 1) = vals(1, x1)
                                                answers(cntA, 2) = vals(2, x2)
                                                answers(cntA, 3) = vals(3, x3)
                                                answers(cntA, 4) = vals(4, x4)
                                                answers(cntA, 5) = vals(5, x5)
                                                answers(cntA, 6) = vals(6, x6)
                                                answers(cntA, 7) = vals(7, x7)
                                                answers(cntA, 8) = vals(8, x8)
                                                answers(cntA, 9) = vals(9, x9)
                                                cntA = cntA + 1
                                            End If
                                        Next x9
                                    Next x8
                                Next x7
                            Next x6
                        Next x5
                    Next x4
                Next x3
            Next x2
        Next x1
    The program works without issue, but it can become excruciating slow. I just did this experiment to quantify the problem. I started with two rows of values and worked my way up. Each row had all nine numbers and the total was 5*number of rows. I then timed how long it took the macros to complete. Here are the results:
    • 2 cells, sum 10: < 1 sec
    • 3 cells, sum 15: < 1 sec
    • 4 cells, sum 20: 1 sec
    • 5 cells, sum 25: 2 sec
    • 6 cells, sum 30: 45 sec

    I didn't bother going to 7 rows because I would still be waiting for things to complete. Why on earth is this taking so long? I can attach the actual spreadsheet is anyone cares.
    Last edited by 6StringJazzer; 05-31-2024 at 06:47 PM.

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Replies: 2
    Last Post: 07-31-2020, 07:38 PM
  2. Slow macro, slow pc or wrong code?
    By corky81 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 09-24-2018, 05:58 AM
  3. Replies: 0
    Last Post: 06-06-2018, 03:54 AM
  4. Slow saving in Excel 2003 format from Excel 2010
    By simonc2 in forum Excel General
    Replies: 0
    Last Post: 10-18-2013, 08:40 AM
  5. Macro is running real slow and makes navigating the worksheet really slow after execution.
    By MichWolverines in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-03-2013, 04:29 PM
  6. [SOLVED] slow computer (slow clipboard) breaks my macro
    By twilsonco in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-05-2013, 09:16 PM
  7. Excel 2003 macro working very slow in Excel 2007 (xlsm format)
    By kapil in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 12-17-2010, 11:26 AM

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.6.0 RC 1