dcsimg
Page 1 of 2 12 LastLast
Results 1 to 40 of 43

Thread: [RESOLVED] VBA Run time error 9

  1. #1

    Thread Starter
    Member
    Join Date
    Aug 2019
    Posts
    54

    Resolved [RESOLVED] VBA Run time error 9

    Hello,

    I am facing Run time error 9 in this line vSafeArray(i, j) = CLng(Fer_Year(i, j)). I changed the Dim as Double but it is not fixing. Any ideas? Thanks in advance

    To give some more clues

    Code:
    Dim IRR95(0 To 1000, 0 To 1000) As Long
    'Dim WUE_Year(0 To 1000, 0 To 1000) As Long
    Dim VWC_Year(0 To 1000, 0 To 1000) As Long
    Dim CWP_Year(0 To 1000, 0 To 1000) As Long
    Dim Irr_Year(0 To 1000, 0 To 1000) As Long
    Dim Fer_Year(0 To 1000, 0 To 1000) As Long
    Dim VPD_Year(0 To 1000, 0 To 1000) As Long
    Dim HI_Year(0 To 1000, 0 To 1000) As Long
    Dim SR_Year(0 To 1000, 0 To 1000) As Long
    Dim PER_Year(0 To 1000, 0 To 1000) As Long
    Dim YLNG_Year(0 To 1000, 0 To 1000) As Long
    Dim X1_Year(0 To 1000, 0 To 1000) As Long
    Dim X2_Year(0 To 1000, 0 To 1000) As Long
    Dim X3_Year(0 To 1000, 0 To 1000) As Long
    Dim Bio_Year(0 To 1000, 0 To 1000) As Long
    and some lines below the code continues as
    
    Irr_Year(intCol(41), intRow(35)) = CLng(IRR)
    Fer_Year(intCol(41), intRow(35)) = CLng(FER)
    VPD_Year(intCol(41), intRow(35)) = CLng(VPD * 1000)
    HI_Year(intCol(41), intRow(35)) = CLng(HI * 1000)
    SR_Year(intCol(41), intRow(35)) = CLng(sr)
    YLNG_Year(intCol(41), intRow(35)) = CLng(YLNG)
    X1_Year(intCol(41), intRow(35)) = CLng(X1)
    X2_Year(intCol(41), intRow(35)) = CLng(X2)
    X3_Year(intCol(41), intRow(35)) = CLng(X3)
    ET_Year(intCol(41), intRow(35)) = CLng(GSET) 'mm
    Last edited by RobDog888; Oct 16th, 2019 at 02:26 PM. Reason: I had mistakes, added [code] tags for easier reading

  2. #2

    Thread Starter
    Member
    Join Date
    Aug 2019
    Posts
    54

    Re: VBA Run time error 9

    To give some more clues

    Code:
    Dim IRR95(0 To 1000, 0 To 1000) As Long
        'Dim WUE_Year(0 To 1000, 0 To 1000) As Long
        Dim VWC_Year(0 To 1000, 0 To 1000) As Long
        Dim CWP_Year(0 To 1000, 0 To 1000) As Long
        Dim Irr_Year(0 To 1000, 0 To 1000) As Long
        Dim Fer_Year(0 To 1000, 0 To 1000) As Long
        Dim VPD_Year(0 To 1000, 0 To 1000) As Long
        Dim HI_Year(0 To 1000, 0 To 1000) As Long
        Dim SR_Year(0 To 1000, 0 To 1000) As Long
        Dim PER_Year(0 To 1000, 0 To 1000) As Long
        Dim YLNG_Year(0 To 1000, 0 To 1000) As Long
        Dim X1_Year(0 To 1000, 0 To 1000) As Long
        Dim X2_Year(0 To 1000, 0 To 1000) As Long
        Dim X3_Year(0 To 1000, 0 To 1000) As Long
        Dim Bio_Year(0 To 1000, 0 To 1000) As Long
    and some lines below the code continues as
    
     Irr_Year(intCol(41), intRow(35)) = CLng(IRR)
                Fer_Year(intCol(41), intRow(35)) = CLng(FER)
                VPD_Year(intCol(41), intRow(35)) = CLng(VPD * 1000)
                HI_Year(intCol(41), intRow(35)) = CLng(HI * 1000)
                SR_Year(intCol(41), intRow(35)) = CLng(sr)
                YLNG_Year(intCol(41), intRow(35)) = CLng(YLNG)
                X1_Year(intCol(41), intRow(35)) = CLng(X1)
                X2_Year(intCol(41), intRow(35)) = CLng(X2)
                X3_Year(intCol(41), intRow(35)) = CLng(X3)
                ET_Year(intCol(41), intRow(35)) = CLng(GSET) 'mm
    Last edited by RobDog888; Oct 16th, 2019 at 02:26 PM. Reason: added [code] tags

  3. #3
    Frenzied Member jdc2000's Avatar
    Join Date
    Oct 2001
    Location
    Idaho Falls, Idaho USA
    Posts
    1,577

    Re: VBA Run time error 9

    I don't see a Dim for vSafeArray anywhere. Triple check what that is set to. Also, what is the value of CLng(Fer_Year(i, j)) when you get the error?

  4. #4

    Thread Starter
    Member
    Join Date
    Aug 2019
    Posts
    54

    Re: VBA Run time error 9

    Thank you .

    Dim vSafeArray As Variant
    vSafeArray = pBlock.PixelDataByRef(0) . If I understand well the question, the value is 120

  5. #5

    Thread Starter
    Member
    Join Date
    Aug 2019
    Posts
    54

    Re: VBA Run time error 9

    Thank you . Apologize but I am new in VBA

    Dim vSafeArray As Variant
    vSafeArray = pBlock.PixelDataByRef(0). In CLng(Fer_Year(i, j)) The i=0 and j= 1001. And the error is CLng(Fer_Year(i, j))= <Subscript out of range>

  6. #6
    Frenzied Member jdc2000's Avatar
    Join Date
    Oct 2001
    Location
    Idaho Falls, Idaho USA
    Posts
    1,577

    Re: VBA Run time error 9

    Fer_Year(i, j) has a maximum j value of 1000, so a j = 1001 will cause this error.

  7. #7

    Thread Starter
    Member
    Join Date
    Aug 2019
    Posts
    54

    Re: VBA Run time error 9

    Thank you, but how this can be corrected?
    I found only this

    For j = 0 To pRasProps.Height - 1

  8. #8
    Frenzied Member jdc2000's Avatar
    Join Date
    Oct 2001
    Location
    Idaho Falls, Idaho USA
    Posts
    1,577

    Re: VBA Run time error 9

    What is the value of pRasProps.Height - 1? If it is not too large, dimension your Fer_Year() array large enough to handle it:

    Example:
    If pRasProps.Height - 1 = 2000, then:
    Dim Fer_Year(0 To 1000, 0 To 2000) As Long

  9. #9
    Super Moderator RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: VBA Run time error 9

    If you put Option Explicit at the top of your code classes/modules it will help catch undeclared variables like how vSafeArray wasnt declared.

    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  10. #10

    Thread Starter
    Member
    Join Date
    Aug 2019
    Posts
    54

    Re: VBA Run time error 9

    Thank you for your answers

    jdc2000 unfortunately this option does not seem to work.
    RobDog888 I wrote this command

    Option Explicit ' Force explicit variable declaration.
    Dim MyVar ' Declare variable.
    MyInt = ' Undeclared variable generates error.
    MyVar = ' Declared variable does not generate error. but I dot Know what number to use. I have tried a lot of combinations of number but nothing seems to work

  11. #11
    Frenzied Member jdc2000's Avatar
    Join Date
    Oct 2001
    Location
    Idaho Falls, Idaho USA
    Posts
    1,577

    Re: VBA Run time error 9

    Your Dim statements should have an 'As (something)' to define the type of variable you are creating (Integer, Long, Double, Variant, etc.). If the variable is an array, it should have the maximum size specified also.)

    https://docs.microsoft.com/en-us/off.../dim-statement

    Code:
    Dim Fer_Year(1000, 2000) As Long

  12. #12

    Thread Starter
    Member
    Join Date
    Aug 2019
    Posts
    54

    Re: VBA Run time error 9

    Thank you. I used Dim Fer_Year(1000, 2000) As Long but In CLng(Fer_Year(i, j)) The i=0 and j= 2001.

    I have also tried this Dim Fer_Year(0 To 1000, 0 To 2000) As Long but the result in CLng(Fer_Year(i, j)) was the same i=0 and j= 2001.
    Last edited by getziatz; Oct 17th, 2019 at 03:18 AM.

  13. #13
    PowerPoster
    Join Date
    Dec 2004
    Posts
    24,762

    Re: VBA Run time error 9

    you need to make sure your array is beg enough to hold all the values being assigned to it, else you need to make sure you do not try to address values outside the range of the array

    as you do not show any of the code used to address the array, unless you just have a mammoth code to address every array index individually, we can not specifically why the error is occurring

    For j = 0 To pRasProps.Height - 1
    it would appear that your arrays should be dynamic, then redimmed to prasprops.height - 1

    Code:
    dim somearray() as long
    then somewhere in the code
    redim somearray(1000, pRasProps.Height - 1)
    you may also need to make the i value match some other object property possibly the width, the same probably is required for all the arrays


    Dim Fer_Year(1000, 2000) As Long
    and
    Dim Fer_Year(0 To 1000, 0 To 2000) As Long
    are basically the same, if no lower value is given, zero is assumed, though there is an option on that

    Option Explicit ' Force explicit variable declaration.
    Dim MyVar ' Declare variable.
    MyInt = ' Undeclared variable generates error.
    MyVar = ' Declared variable does not generate error. but I dot Know what number to use. I have tried a lot of combinations of number but nothing seems to work
    if no type is given, then the variable is a variant, a variant can hold any other type of variable
    you can detect the content type of a variant by looking in the locals window and drilling down the list, then you can decide to change the variable to that type, if desired
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  14. #14

    Thread Starter
    Member
    Join Date
    Aug 2019
    Posts
    54

    Re: VBA Run time error 9

    Unfortunately, I have a mammoth code . The error appears in the module where I have the following dim:


    Dim X, Y, Yield, WUE, GSET, EP, IRR, FER, ET, VPD, HI, sr, YLNG, X1, X2, X3 As Single
    Dim V17, V18, V19, V20 As Single
    Dim V21, V22, V23, V24, V25, V26, V27, V28, V29, V30 As Single
    Dim V31, V32, V33, V34, V35, V36, V37, V38, V39, V40 As Single
    Dim V41, V42, V43, V44, V45, V46, V47, V48, V49, V50 As Single
    Dim V51, V52, V53, V54, V55, V56, V57, V58, V59, V60 As Single
    Dim V61, V62 As Single

    Dim XLOG(0 To 1000, 0 To 1000) As Single
    Dim YLAT(0 To 1000, 0 To 1000) As Single
    Dim yield_Year(0 To 1000, 0 To 1000) As Long
    Dim ET_Year(0 To 1000, 0 To 1000) As Long
    Dim EP_Year(0 To 1000, 0 To 1000) As Long
    Dim Ann_ET(0 To 1000, 0 To 1000) As Long
    Dim wind_ero(0 To 1000, 0 To 1000) As Long
    Dim water_ero(0 To 1000, 0 To 1000) As Long

    'Dim IRR95(0 To 1000, 0 To 1000) As Long
    'Dim WUE_Year(0 To 1000, 0 To 1000) As Long
    Dim VWC_Year(0 To 1000, 0 To 1000) As Long
    Dim CWP_Year(0 To 1000, 0 To 1000) As Long
    Dim Irr_Year(0 To 1000, 0 To 1000) As Long
    Dim Fer_Year(0 To 1000, 0 To 2000) As Long
    Dim VPD_Year(0 To 1000, 0 To 1000) As Long
    Dim HI_Year(0 To 1000, 0 To 1000) As Long
    Dim SR_Year(0 To 1000, 0 To 1000) As Long
    Dim PER_Year(0 To 1000, 0 To 1000) As Long
    Dim YLNG_Year(0 To 1000, 0 To 1000) As Long
    Dim X1_Year(0 To 1000, 0 To 1000) As Long
    Dim X2_Year(0 To 1000, 0 To 1000) As Long
    Dim X3_Year(0 To 1000, 0 To 1000) As Long
    Dim Bio_Year(0 To 1000, 0 To 1000) As Long

    Dim intCol(0 To 200000) As Long
    Dim intRow(0 To 200000) As Long
    Dim intSerialNo(0 To 200000) As Single

    Dim i As Long, j As Long
    For i = 0 To pRasProps.Width - 1
    For j = 0 To pRasProps.Height - 1 Thank you for your answer. Any idea is welcomed

  15. #15
    PowerPoster
    Join Date
    Dec 2004
    Posts
    24,762

    Re: VBA Run time error 9

    i think my previous post covers your immediate error problem, you have to make sure the array(s) is big enough to hold all the data
    you need to redim the arrays to the size of the width and height of the object, so dim all the arrays as dynamic then redim them when the size can be determined, then you know that the data will fit

    it would appear that your arrays should be dynamic, then redimmed to prasprops.height - 1

    Code:

    dim somearray() as long
    then somewhere in the code
    redim somearray(1000, pRasProps.Height - 1)

    you may also need to make the i value match some other object property possibly the width, the same probably is required for all the arrays
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  16. #16

    Thread Starter
    Member
    Join Date
    Aug 2019
    Posts
    54

    Re: VBA Run time error 9

    Hello

    thank you very much for your time and for your help. I have this information Dim someSafeArray(5000, 4000) As Long in the main body of the code. I am a little bit confused about dim. In the main body of the code I do redim or in the error module? My object has a height of 35 and width of 41.

  17. #17
    PowerPoster
    Join Date
    Dec 2004
    Posts
    24,762

    Re: VBA Run time error 9

    once you dimension the array with a fixed size (like 1000,3000), it is a static array and can not be redimmed

    redim the array just before you want to assign values to it

    For j = 0 To pRasProps.Height - 1
    if your object has a size of 41x35, why on earth should the loop get to 1000 + on the height, that makes no sense, you need to inspect the properties of the prasprops object

    what application are you working in?
    can you post some sample?
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  18. #18

    Thread Starter
    Member
    Join Date
    Aug 2019
    Posts
    54

    Re: VBA Run time error 9

    Thank you very much for your prompt response. I tried this

    Dim Fer_Year(0 To 41, 0 To 35) As Long
    some lines below
    Dim i As Long, j As Long
    For i = 41 To pRasProps.Width - 1
    For j = 35 To pRasProps.Height - 1

    but in this vSafeArray(i, j) = CLng(Fer_Year(i, j)). I get as a result i=41 and j=36 and error 9 subscript out of range error is appeared I think due to j=36 instead of j=35. The software is called GEPIC. Once again, thank you

    All the variables in the main body of the code have this:

    Dim vnameSafeArray(5000, 4000) As Long but I go to the specific module and I try to change the array, there
    Last edited by getziatz; Oct 20th, 2019 at 04:17 PM.

  19. #19
    PowerPoster
    Join Date
    Dec 2004
    Posts
    24,762

    Re: VBA Run time error 9

    Dim vnameSafeArray(5000, 4000) As Long
    this is now a static array and, as pointed out above, can not be redimmed later, should cause an error

    vSafeArray(i, j) = CLng(Fer_Year(i, j)).
    this is working with 2 arrays so either could be causing the error

    Dim Fer_Year(0 To 41, 0 To 35) As Long
    some lines below
    Dim i As Long, j As Long
    For i = 41 To pRasProps.Width - 1
    For j = 35 To pRasProps.Height - 1
    as i and j now start at the ubound of the array every iteration of the loop after the first has to error

    you need to look at the properties of the prasprops object to find the low (presumably 0) and high range of the height and width, everything else is just guessing
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  20. #20
    PowerPoster
    Join Date
    Dec 2004
    Posts
    24,762

    Re: VBA Run time error 9

    Dim vnameSafeArray(5000, 4000) As Long
    this is now a static array and, as pointed out above, can not be redimmed later, should cause an error

    vSafeArray(i, j) = CLng(Fer_Year(i, j)).
    this is working with 2 arrays so either could be causing the error

    Dim Fer_Year(0 To 41, 0 To 35) As Long
    some lines below
    Dim i As Long, j As Long
    For i = 41 To pRasProps.Width - 1
    For j = 35 To pRasProps.Height - 1
    as i and j now start at the ubound of the array every iteration of the loop after the first has to error

    you need to look at the properties of the prasprops object to find the low (presumably 0) and high range of the height and width, everything else is just guessing
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  21. #21

    Thread Starter
    Member
    Join Date
    Aug 2019
    Posts
    54

    Re: VBA Run time error 9

    Thank you very much for your response. I think you speak about set raster properties, am I right? But I cannnot find something like this; until now I have find only this

    QI raster properties
    Dim pRasProps As IRasterProps
    Set pRasProps = pRasB
    Create a default raster from input raster dataset
    Dim pInputRaster As IRaster
    Set pInputRaster = pInRasDS.CreateDefaultRaster

    Where can I look for and change the raster properties? Thank you very much for your help

  22. #22
    PowerPoster
    Join Date
    Dec 2004
    Posts
    24,762

    Re: VBA Run time error 9

    Where can I look for and change the raster properties?
    if you put a break point in your code,, or during debugging an error you can look in the locals window MENU > VIEW > Locals window
    find the object you want and drill down through all the properties, to see what values they contain
    you can not change the vales here, but you will be able to see what the vales are for height and width, to find why you are having a problem and then fix the code

    i have never heard of gepic, i presume it something for mapping or similar, so i can not offer to test /fix the code

    you need to fix all the arrays to dynamic, then redim to the required dimensions, rather than guessing arbitrary dimensions

    for testing, you can change
    vSafeArray(i, j) = CLng(Fer_Year(i, j)).
    to
    Code:
    dim temp as variant
    temp = CLng(Fer_Year(i, j))
    vSafeArray(i, j) = temp
    this will determine which array is out of bounds
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  23. #23

    Thread Starter
    Member
    Join Date
    Aug 2019
    Posts
    54

    Re: VBA Run time error 9

    Thank you very much for your answer. I look the object I want and it is Long(0 to 35) and I have this as Long(0 to 41, 0 to 35). Apologize, but I did not understand very much, where I have to fix all the arrays to dynamic and to redim them since vSafeArray(i, j) is appeared many times in the code. Should a change in the read data section? Thank you very much for your help.

    If I understand well in one of your previouw post you suggested me that Dim vnameSafeArray(5000, 4000) As Long is a static array and cannot be redimmed later so I have to look for the module where give me the permission to change the array to dynamic but the module has a lot of if fuctions such as
    If Content = "Nitrogen" Then
    vSafeArray(i, j) = CLng(Fer_Year(i, j))
    End If

    can i change the array to dynamic and redimmed it in this part ?


    P.S GEPIC is a compile loope program. EPIC is written in Fotran does the calculation and the mapping results in GIS is achieved by a code in VBA. Unfortunately, there is no technical support.
    Last edited by getziatz; Oct 25th, 2019 at 01:12 PM.

  24. #24
    PowerPoster
    Join Date
    Dec 2004
    Posts
    24,762

    Re: VBA Run time error 9

    can i change the array to dynamic and redimmed it in this part ?
    you can not later change an array to dynamic, it must be dimensioned that way to start with
    Code:
    dim somearray(300, 4000) as long    ' static array
    dim arrrayx() as long     ' dynamic array must be rediimmed before use, can be redimmed multiple times
    using redim preserve will allow keeping the values in the array, while changing dimension, though there are limitations on this with 2dimensional arrays, you will need to research if you want to use

    Dim the array in the same place and redim prior to assigning values
    as it is you are assigning a small sett of values to fairly large array, while this should not cause an error it is very inefficient and likely to cause some problem later in the code



    mapping results in GIS is achieved by a code in VBA. Unfortunately, there is no technical support.
    whilst the GIS objects won't be supported, the generic vba code can be copied and pasted into an office application like excel, the you should be able to use F1 help, to get more information about arrays and other functions and statements


    For i = 0 To pRasProps.Width - 1
    For j = 0 To pRasProps.Height - 1
    in this code, the value i should never exceed 35, and j should never exceed 41 (or the other way around), so how it can exceed the the large value of the array dimension seems to be the issue, do you at any point within the loop change the value of either i or j?

    on looking back the original error appeared to be
    In CLng(Fer_Year(i, j)) The i=0 and j= 1001 And the error is CLng(Fer_Year(i, j))= <Subscript out of range>
    which of course is not going to be fixed by any change to your code for prasprops, but you have not shown any code for a fer_year loop where the error is occurring
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  25. #25

    Thread Starter
    Member
    Join Date
    Aug 2019
    Posts
    54

    Re: VBA Run time error 9

    Thank you very much for your reply. If I understand well you would like to see something like that

    For i = 41 To pRasProps.Width - 1
    pProDlg.Description = i & " of " & (pRasProps.Width - 1)
    pStepPro.Message = i / (pRasProps.Width - 1) * 100 & "%"
    blnCont = ptrkCan.Continue
    If Not blnCont Then
    Exit For
    End If
    pStepPro.Step

    For j = 35 To pRasProps.Height - 1

    If Content = "Nitrogen" Then
    vSafeArray(i, j) = CLng(Fer_Year(i, j))
    End If

    Now the i=41 and the j =1001

    I will make my effort to redim my array but I would appeciate very much since you are available to check my code via teamviewer or anydesk. Thank you in advance
    Last edited by getziatz; Oct 26th, 2019 at 04:12 PM.

  26. #26
    PowerPoster
    Join Date
    Dec 2004
    Posts
    24,762

    Re: VBA Run time error 9

    For i = 41 To pRasProps.Width - 1
    as the width is 41, you are make a loop like for i = 41 to 40, do you think that will do anything?
    depending on the lower boundary it should be
    Code:
    for i = 0 To pRasProps.Width - 1
    as you had originally, read again post #20, especially the second part to determine which array is causing error

    if i understand it correctly that was not the part that was originally causing error, some confusion occurred
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  27. #27

    Thread Starter
    Member
    Join Date
    Aug 2019
    Posts
    54

    Re: VBA Run time error 9

    Thank you very much. Perhaps, you may be confused but it is totally my mistake. For one more time, I am sorry

    in the post #20
    Dim vnameSafeArray(5000, 4000) As Long
    is referred to read data section
    while
    Dim Fer_Year(0 To 1000, 0 To 1000) As Long
    is referred to the "createmaps" results module.

    I will try to redim and change my code and I will let you know my progress giving feedback. Thank you
    Last edited by getziatz; Oct 26th, 2019 at 05:11 PM.

  28. #28
    PowerPoster
    Join Date
    Dec 2004
    Posts
    24,762

    Re: VBA Run time error 9

    as they are in different modules, one or both may need to be public, but that does not appear to be the error you had
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  29. #29
    PowerPoster
    Join Date
    Dec 2004
    Posts
    24,762

    Re: VBA Run time error 9

    further to the last post, you should research on the scope of variables
    a variable dimensioned within a procedure (sub or function) is only valid (in scope), within that procedure
    variables dimensioned at the top of the module (in the general section) are valid for any procedure within that module
    if you want to access variables from other modules, then the should be dimensioned as public
    Code:
    public fer_year as long
    i can not really see from the code posted where your variables are dimensioned, but none shown are public

    to resolve this and some other errors you should put option explicit at the very top of every code module, this is always good practice, anywhere a variable is used that is not dimensioned, or if it is not within scope, it will give an error variable not defined, so if you get this error you know some variable needs to be fixed

    hope this gives some pointers towards your problem
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  30. #30

    Thread Starter
    Member
    Join Date
    Aug 2019
    Posts
    54

    Re: VBA Run time error 9

    I think that the variables are dimensioned in this command

    Read Fertilizer data
    Dim sFerInPath, sFerInRasDSName As String
    Dim vFerSafeArray(5000, 4000) As Long
    sFerInPath = InputForm.TextBox11.Text
    sFerInRasDSName = InputForm.TextBox12.Text
    Call ReadGridData(sFerInPath, sFerInRasDSName, vFerSafeArray)

    Thank you for your response

  31. #31
    PowerPoster
    Join Date
    Dec 2004
    Posts
    24,762

    Re: VBA Run time error 9

    it is beyond me to identify your variables or where they are dimensioned from the small code samples you have posted, or which variables may not be in scope during any of the code

    as i said put option explicit at the top of each module, any variable that errors are either not dimensioned or not in scope, so if you believe it is dimensioned you need to look at the scope where the dimensioning takes place
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  32. #32

    Thread Starter
    Member
    Join Date
    Aug 2019
    Posts
    54

    Re: VBA Run time error 9

    Thank you very much for your response. All the code which I have modified is attachedThisDocument.cls.

    Please let me know if you find this helpful or not. Thank you very much for your help
    Attached Files Attached Files

  33. #33
    PowerPoster
    Join Date
    Dec 2004
    Posts
    24,762

    Re: VBA Run time error 9

    i have downloaded all the modules, will look at them later

    which procedure was the error occurring?
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  34. #34

    Thread Starter
    Member
    Join Date
    Aug 2019
    Posts
    54

    Re: VBA Run time error 9

    Thank you, thank you very much. In the Createmaps procedure.

  35. #35
    PowerPoster
    Join Date
    Dec 2004
    Posts
    24,762

    Re: VBA Run time error 9

    Dim vSafeArray As Variant
    vSafeArray = pBlock.PixelDataByRef(0)

    'Design Progress Dialog
    Dim pProDlgFact As IProgressDialogFactory
    Dim pStepPro As IStepProgressor
    Dim pProDlg As IProgressDialog2
    Dim ptrkCan As ITrackCancel
    Set ptrkCan = New CancelTracker
    Set pProDlgFact = New ProgressDialogFactory
    Set pProDlg = pProDlgFact.Create(ptrkCan, Application.Hwnd)
    pProDlg.CancelEnabled = True
    pProDlg.Title = "Generating Maps ..."
    pProDlg.Animation = esriProgressGlobe
    Set pStepPro = pProDlg

    pStepPro.MinRange = 0
    pStepPro.MaxRange = (pRasProps.Width - 1)
    pStepPro.StepValue = 1

    'Loop through the safearray and change all the pixel values
    Dim i As Long, j As Long
    For i = 0 To pRasProps.Width - 1
    pProDlg.Description = i & " of " & (pRasProps.Width - 1)
    pStepPro.Message = i / (pRasProps.Width - 1) * 100 & "%"
    blnCont = ptrkCan.Continue
    If Not blnCont Then
    Exit For
    End If
    pStepPro.Step

    For j = 0 To pRasProps.Height - 1
    'If yield_Year(i, j) > 0.6 Then
    'If Content = "Yield" Then vSafeArray(i, j) = CLng(yield_Year(i, j) / 0.86) 'kg/ha
    If Content = "Yield" Then vSafeArray(i, j) = CLng(yield_Year(i, j)) 'kg/ha dry yield
    If Content = "ET" Then vSafeArray(i, j) = CLng(ET_Year(i, j)) 'mm
    If Content = "VWC" Then
    'If yield_Year(i, j) > 0 Then vSafeArray(i, j) = CLng(0.86 * VWC_Year(i, j)) 'm3/ton
    If yield_Year(i, j) > 0 Then vSafeArray(i, j) = CLng(VWC_Year(i, j)) 'm3/ton dry yield

    End If
    If Content = "CWP" Then
    'If yield_Year(i, j) > 0 Then vSafeArray(i, j) = CLng(CWP_Year(i, j) / 0.86) 'm3/ton
    If yield_Year(i, j) > 0 Then vSafeArray(i, j) = CLng(CWP_Year(i, j)) 'm3/ton
    End If
    If Content = "Irrigation" Then
    vSafeArray(i, j) = CLng(Irr_Year(i, j))
    End If

    If Content = "Nitrogen" Then
    vSafeArray(i, j) = CLng(Fer_Year(i, j))
    End If
    in this excerpt vsafearray is dimensioned as a variant, that assigned an objects pixeldata, so the dimension of that array is then the size of the pixel data, whether that is as big as the other arrays, i can not tell, but that appears where the problem occurs

    another area of concern is
    Irr_Year(intCol(41), intRow(35)) = CLng(IRR)
    Fer_Year(intCol(41), intRow(35)) = CLng(FER)
    VPD_Year(intCol(41), intRow(35)) = CLng(VPD * 1000)
    HI_Year(intCol(41), intRow(35)) = CLng(HI * 1000)
    these are some lines that appear in a loop while reading the file, as far as i can see they appear to overwrite previous values, but i may be mistaken on this



    FYI
    Dim X, Y, Yield, WUE, GSET, EP, IRR, FER, ET, VPD, HI, sr, YLNG, X1, X2, X3 As Single
    Dim V17, V18, V19, V20 As Single
    Dim V21, V22, V23, V24, V25, V26, V27, V28, V29, V30 As Single
    Dim V31, V32, V33, V34, V35, V36, V37, V38, V39, V40 As Single
    Dim V41, V42, V43, V44, V45, V46, V47, V48, V49, V50 As Single
    Dim V51, V52, V53, V54, V55, V56, V57, V58, V59, V60 As Single
    Dim V61, V62 As Single
    in all these lines, only the last variable is dimensioned as single, the rest are all variant


    as i can not test the code at all, i can not even confirm that my findings are even correct, but should give you same things to check out
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  36. #36

    Thread Starter
    Member
    Join Date
    Aug 2019
    Posts
    54

    Re: VBA Run time error 9

    Thank you for your help. I think that you are right in all your assumptions. Is there anything else I can help you with?

  37. #37

    Thread Starter
    Member
    Join Date
    Aug 2019
    Posts
    54

    Re: VBA Run time error 9

    Hello, I changed the variable to variant in the code part you mention but the error 9 continuous to exists. I would like to ask you if I have to change it in any other part; apart from [Dim X, Y, Yield.......... Dim V61, V62 As Single]. Thank you in advance.

  38. #38
    Fanatic Member
    Join Date
    Nov 2017
    Posts
    933

    Re: VBA Run time error 9

    Its your data. Do some googling for information about VBA variable types, what type of data they hold, what the minimum and maximum values they can hold, and go from there.

    Here's a good place to start:

    https://docs.microsoft.com/en-us/off...a-type-summary

    Also, don't do this:

    Code:
    Dim var1, var2, var3 as Single
    Do this:

    Code:
    Dim var1 As Single
    Dim var2 As Single
    Dim var3 As Single
    Good luck.


    Edit: If, as you say, you are still getting a runtime error 9, that means you are still attempting to store data into a "slot" that doesn't exist.

    Example:

    Code:
    Dim var1(4) As Single
    I can store a value in var1(0), var1(1), var1(2), var1(3), and var1(4). If I try to store a value in var1(5) I will get a runtime error 9. Somewhere in your code (probably numerous places) you are trying to store a value into a slot that doesn't exist.

    You admit yourself that you have a "mammoth" code. That makes it even more unlikely that someone here is going to voluntarily go through it and try to track down where the issue is out of the kindness of their heart. You are the only one here who has a vested interest in getting this solved. We're all volunteers here. You've been pointed in the direction of where the problem is.

    Good luck.
    Last edited by OptionBase1; Nov 3rd, 2019 at 01:17 PM.

  39. #39

    Thread Starter
    Member
    Join Date
    Aug 2019
    Posts
    54

    Re: VBA Run time error 9

    Thank you for your answer. I tried your suggestion but it does not seem to correct the error. As far as my data are concerned I have tried a lot of data type such as single, long, double but nothing seems to work. Any new ideas or suggestions are welcome. Moreover, all the variable in this part variable(intCol(number), intRow(number)) = CLng(variable); the result is: CLng(variable)= 0 or CLng(variable)= EMPTY . Thank you in advance.

  40. #40
    Fanatic Member
    Join Date
    Nov 2017
    Posts
    933

    Re: VBA Run time error 9

    Where did this code come from in the first place?

    As far as I can tell, your code reads data from one or more external files, and you haven't provided any sample data from those files that I've seen. That being the case, how can anyone here tell you what type of variables to declare?

    You have the data in front of you. You have the URL that I provided that explains what each type of variable holds what type of data. Time to buckle down and get cracking on it. Saying that you've tried setting them to various data types means you either don't know what the code is doing, or you don't understand what those data types do; in either case, that's on you to rectify.

    You can't plop hundreds of lines of barely documented code on a website full of volunteers and expect them to sort out the coding issues for you. Well, you can, because you did, but don't set your expectations too high.

    If you are in a position where you need something done that you are incapable of doing yourself, you should probably do what you would do if you needed your car worked on, your hair cut, your sink fixed, a tooth pulled, etc. You are going likely have to pay someone who is willing to look at the code and then to fix your code for you. And from my brief glance at the code you posted earlier, I get the feeling that even when the runtime error 9 issue is resolved and the code (potentially) runs without throwing an error, I doubt that the results generated by that code will be correct.

    I'm not able to assist further, good luck.

Page 1 of 2 12 LastLast

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width