• problem with VBA code (2002/SP3)

    Author
    Topic
    #454865

    The code that I have put together (attached) looks for the occurances of “CHASIS_1” and returns the value next to “T 12=” in the file called “a_test.txt” (also attached). The problem that I get the “T 12=” values for CHASIS_2 rather than CHASIS_1. When I use the “step in” function in the VBA editor everything seems to works fine, but when I run the procedure “TestProc”, I get this:

    CHASIS_1 0 23
    CHASIS_1 120 99.999
    CHASIS_1 240 99.999

    What I should be gettign is this:
    CHASIS_1 0 23
    CHASIS_1 120 21.765
    CHASIS_1 240 21.765

    Can anyone give me some idea as to why this happens?

    Viewing 1 reply thread
    Author
    Replies
    • #1130240

      Are you sure that you posted the correct files? When I try it, your code produces nothing that remotely resembles either of the results that you posted.

    • #1130241

      I found out why I didn’t get a result resembling yours. If there is more than one text file in the folder, the code overwrites the same cells.

      There is no check for CHASIS_2 anywhere in your code. I think you should add that.

      • #1130252

        Hans, thanks for your help here.

        I still don’t see the problem. I made a duplicate of this file “a_test.txt” to “a2_test.txt” and re-ran the procedure. Note that I added more data to this file, so that now there are four instances of “CHASIS_1” and five instances of “CHASIS_2”. I’ve attached the file so you can see what I mean. Running the program yields:

        CHASIS_1 0 23.555 |
        CHASIS_1 120 99.999 |
        CHASIS_1 240 99.001 | < value for "T 12=" at CHASIS_2, not CHASIS_1 as coded
        CHASIS_1 360 99.011 |

        This outcome doesn't change whether I run both text files or just "a2_test.txt". Also note that the instances for "SUBMODEL NAME = CHASIS_1" are identified correctly by the code. What makes this even more confusing to me is that the code seems to work as planned when I used F8 to step through each line in the code.

        Lastly, this sample file only lists "CHASIS_1" and "CHASIS_2" as the "submodel" criteria. In the actual files that I deal with I have close to 20 different "submodels".

        • #1130255

          I’m afraid I can’t help you – I don’t understand what the code tries to do. Sorry!

        • #1130256

          The problem seems to be that after you get the temp Value at “12=” for Chassis 1, you continue mark it in column C, but keep looking for it since you need the time. Thus you overwrite the value in col C when you find a value for “12=” for Chassis 2 which comes between the chassis1 and the time value.

          What you need to do is stop looking for a temperature after you find it, but continue to look for the time. does the Attached modification work for you?

          I added a variable “bNeedsTemp” which gets changed after a temp is found and reset after a time is found [I also defined some of your variable types. The lines:
          Dim r, d, i As Long
          Dim strName, strNode As String

          Will only set i as long and strNode as a string. The other variables will be set as variants since you don’t explicitly define them. VBA does not have implicit definitions (r and d being long or strName as a string) as you want them to be.

          Steve

          • #1130266

            Steve, the code works as planned. Thanks you very much for your help. 🙂

    Viewing 1 reply thread
    Reply To: problem with VBA code (2002/SP3)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: