Results 1 to 8 of 8

Thread: Weird VBA behavior

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2006
    Posts
    6

    Weird VBA behavior

    Im using VBA with excel to do some routine totaling of large amounts of data that come in every month in more or less the same format...

    While im writing the code im also testing it to make sure it runs smoothly, but, once i write a function and test it it goes REALLY slowly. One time I got kinda....bored...and ctrl+alt+deleted to end task. When i reloaded the SAME file (had saved just before testing and ending) it goes signifcantlly faster, like ~30x faster.

    My question is, why does it do this and how do i make it do that WITHOUT having to EndTask and come back.

    NOTE: saving and closing out the file doesn't work either.

    Thanks,
    -Andrew

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Weird VBA behavior

    To know why it does that, we'll almost certainly need to see the code.

  3. #3

  4. #4

    Thread Starter
    New Member
    Join Date
    Jul 2006
    Posts
    6

    Re: Weird VBA behavior

    K, what parts, i've made a few functions and stuff....heres one of the main functions....

    VB Code:
    1. Function totalPGEPump()
    2.  
    3.     Dim colName As String
    4.     Dim pltName(1 To 17) As String
    5.     Dim pltType As String
    6.     Dim nDay As Long
    7.     Dim day As Long
    8.     Dim nHour As Long
    9.     Dim hour As Long
    10.     Dim i As Long
    11.     Dim pltRow As Long
    12.     Dim subRow As Long
    13.     Dim totalNameCell As String
    14.     Dim functionName As String
    15.     Dim colNameNum As Long
    16.     Dim hourlyTotalArray(30, 24) As Double
    17.     Dim totalPlants As Long
    18.     'pltType is the type of the plants that are being totaled
    19.     pltType = "Pump"
    20.     'functionName is the name that will appear as the blocks title in the worksheet
    21.     functionName = "PG&E Area " & pltType
    22.     'totalNameCell is the cell in which the name will appear
    23.     totalNameCell = "AP1893"
    24.     Cells((detExlRow(totalNameCell)), detExlCol(totalNameCell)) = functionName
    25.  
    26.    
    27.     'the column in which the names of the plants are listed.
    28.     'use the first cell in that column.
    29.     colName = "AQ1"
    30.     colNameNum = detExlCol(colName)
    31.     'Names of the plants in no order.  Add more by increasing the 'dim' above _
    32.     'and also by increasing the while loop below.
    33.    
    34.     pltName(1) = "Badger Hill"
    35.     pltName(2) = "Banks"
    36.     pltName(3) = "Barker Slough"
    37.     pltName(4) = "Bluestone"
    38.     pltName(5) = "Buena Vista"
    39.    
    40.     pltName(6) = "Chrisman"
    41.     pltName(7) = "Cordelia (new)"
    42.     pltName(8) = "Del Valle"
    43.     pltName(9) = "Devils Den"
    44.     pltName(10) = "Dos Amigos"
    45.  
    46.     pltName(11) = "Gianelli"
    47.     pltName(12) = "Hyatt"
    48.     pltName(13) = "Las Perillas"
    49.     pltName(14) = "Polonio"
    50.     pltName(15) = "South Bay"
    51.    
    52.     pltName(16) = "Teerink"
    53.     pltName(17) = "Therm"
    54.    
    55.     i = 1
    56.     subRow = detExlRow(totalNameCell)
    57.    
    58.     totalPlants = 17
    59.    
    60.     nDay = 30 - 1
    61.     day = 0
    62.     nHour = 23
    63.     hour = 0
    64.    
    65.     While i <= totalPlants
    66.         pltRow = searchForPlantName(colName, pltName(i), pltType)
    67.    
    68.         While day <= nDay
    69.             While hour <= nHour
    70.                 hourlyTotalArray(day, hour) = hourlyTotalArray(day, hour) + Cells(pltRow + day, colNameNum + 1 + hour)
    71.                 hour = hour + 1
    72.             Wend
    73.             day = day + 1
    74.             hour = 0
    75.         Wend
    76.         day = 0
    77.         i = i + 1
    78.     Wend
    79.    
    80.     While day <= nDay
    81.         While hour <= nHour
    82.             Cells(subRow + day, colNameNum + 1 + hour) = hourlyTotalArray(day, hour)
    83.             hour = hour + 1
    84.         Wend
    85.         day = day + 1
    86.         hour = 0
    87.     Wend
    88. End Function



    Lemme know if you want some more, or certian functions from this peice.


    Sorry bout the wrong forum thing...


    EDIT: Lost all my formating, sorry.
    Last edited by si_the_geek; Jul 14th, 2006 at 02:27 PM. Reason: added vbcode tags

  5. #5
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: Weird VBA behavior

    application deployment?????

  6. #6
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Weird VBA behavior

    Hmm.. strange choice Marty! Moved to Office Development


    EDIT: Lost all my formating, sorry.
    To keep formatting you need to use VBCode tags - either using the button in the post editor screen, or by putting them in manually, like this:
    [vbcode] 'code here [/vbcode]
    (I have added them to your post above).


    I'm afraid there is nothing obvious in your code that could make it slow down when being re-run, but then we can't see much of the code you are using (in the other functions, like detExlCol) or the sheet itself.

    If you can post the file (put it in a zip file, and click on "post reply" then "manage attachments") then we can probably work out any issues.
    (if you cant show us the data, it would help if you could give us "fake" data instead)

  7. #7
    Addicted Member malik641's Avatar
    Join Date
    Sep 2005
    Location
    South Florida :-)
    Posts
    221

    Re: Weird VBA behavior

    So what you are saying is that when you EDIT your existing code (or adding more to it) and then test it...it runs slower unless you force the program to close and re-open? Or is it when you run the code more than once?




    If you find any of my posts of good help, please rate it

  8. #8

    Thread Starter
    New Member
    Join Date
    Jul 2006
    Posts
    6

    Re: Weird VBA behavior

    For some reason I couldn't find the VBCode button the first time around (I was looking for it), I see it now though.

    The problem:

    (It got weirder over the weekend, I swear this is true)

    When I open the file for the first time the code runs (measured by how fast the individual cells fill in) at "1x". If I Ctrl+Alt+Delete while the code is running the re-open the same file and run it again it runs much faster ~"10x" at least.

    Now, I have discovered while doing the Ctrl+Alt+Delete, that if I just hold Alt down while the code is running I get almost the same results without the impromptu ending for the program. Here is the bulk of my code.

    Don't hesitate to criticize this code! I'm a newbie at programming so I’m sure it’s not as "optimized" as it can be. Also, there are probably many functions I have forgotten about that were either for test or are for some other parts of the program.
    Attached Files Attached Files

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