+ Reply to Thread
Results 1 to 13 of 13

Converting Array to Integers

Hybrid View

  1. #1
    Registered User
    Join Date
    01-21-2010
    Location
    Akron Ohio
    MS-Off Ver
    Excel 2007
    Posts
    16

    Converting Array to Integers

    So basically i have an Array called my Array declared as fallowing ...

    Public MyArray(1 - 5) As Integer
    The Array Is populated as fallowing ...

    
    MyArray(1)=1 
    MyArray(2)=2
    MyArray(3)=3
    MyArray(4)=4
    MyArray(5)=5
    What i want it to find a way to turn the Array of Integers into a Single Integer



    If
    MyArray(1)=1
    MyArray(2)=2
    MyArray(3)=3
    MyArray(4)=4
    MyArray(5)=5
    then

    Public ConvertedArray as Integer = 12345
    Thank you

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Converting Array to Integers

        Dim ai(1 To 5) As Integer
        Dim i As Long
        
        ai(1) = 1
        ai(2) = 2
        ai(3) = 3
        ai(4) = 4
        ai(5) = 5
        
        i = WorksheetFunction.SumProduct(ai, Array(10000, 1000, 100, 10, 1))
        MsgBox i
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Converting Array to Integers

    Hello cporter5,

    Here is another way...
    Sub ConvertArrayToInteger()
    
      Dim MyArray(1 To 5) As Variant
      Dim N As Integer
    
        MyArray(1) = 1
        MyArray(2) = 2
        MyArray(3) = 3
        MyArray(4) = 4
        MyArray(5) = 5
    
        N = CInt(Val(Join(MyArray, "")))
      
    End Sub
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  4. #4
    Registered User
    Join Date
    01-21-2010
    Location
    Akron Ohio
    MS-Off Ver
    Excel 2007
    Posts
    16

    Re: Converting Array to Integers

    okay im having trouble with this stupid error .... Runtime error 9

    my code looks like this ...

    Dim Password(1 to 5) As Variant
    Dim N As Integer
    
    Public Sub Worksheet_Activate()
    N = 1
    End Sub
    
    
    
    Sub Activate()
    Dim i As Integer
    i = CInt(Val(Join(Password, "")))
    MsgBox i
    
    End Sub
    
    
    
    Sub Button1()
    
     Password(N) = 1
     N = N + 1
        
    Dim shpTemp As Shape
        
        Set shpTemp = ActiveSheet.Shapes(Application.Caller)
        With shpTemp
            If .ThreeD.BevelTopType = msoBevelRelaxedInset Then
                .ThreeD.BevelTopType = msoBevelCircle
            Else
                .ThreeD.BevelTopType = msoBevelRelaxedInset
            End If
        End With
         
    End Sub
    The line Password(N)=1 comes up with the out of bounds error '9' ... i dont understand because N = 1 ... and 1 is between 1 and 5 .... this code should store the number 1 Password(1) and then move it so that Password(2) should be ready to accept an input.

  5. #5
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Converting Array to Integers

    Hello cporter5,

    It appears you defined a Worksheet event inside a standard VBA module...
    Public Sub Worksheet_Activate()
      N = 1
    End Sub

    N will remain zero because this code will never be run. Cut this code from the module and following the directions below.

    How to Save a Worksheet Event Macro
    1. Copy the macro using CTRL+C keys.
    2. Open your Workbook and Right Click on the Worksheet's Name Tab for the Worksheet the macro will run on.
    3. Left Click on View Code in the pop up menu.
    4. Paste the macro code using CTRL+V
    5. Make any custom changes to the macro if needed at this time.
    6. Save the macro in your Workbook using CTRL+S

  6. #6
    Registered User
    Join Date
    01-21-2010
    Location
    Akron Ohio
    MS-Off Ver
    Excel 2007
    Posts
    16

    Re: Converting Array to Integers

    it is a module ... that line of code i added in attempt to make it work .... i think the reason that it doesnt work is because N is never given the initial value of 1 ... and i dont know where to put it ... ideal i would like to have N set to 1 somewhere in a Module scope, because i have several procedures that use the N variable ...

  7. #7
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Converting Array to Integers

    Hello cporter5,

    It may be easier to find the problem if you post your workbook for review.

  8. #8
    Registered User
    Join Date
    01-21-2010
    Location
    Akron Ohio
    MS-Off Ver
    Excel 2007
    Posts
    16

    Re: Converting Array to Integers

    Here is my Excel 2007 workbook ... Before the comments start ... i got bored waiting for response so i went a bit over bored on the graphic element ...
    Attached Files Attached Files

  9. #9
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Converting Array to Integers

    Hello cporter5,

    You are correct about the variable not be initialized. The easiest way is to initialize the variable when the workbook first opens. Copy this code into the ThisWorkbook module.
    Private Sub Workbook_Open()
      N = 1
    End Sub

  10. #10
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Converting Array to Integers

    Hello cporter5,

    You should declare your variables in Module3 as below:
    Public Password(1 To 5) As Variant
    Public N As Integer

  11. #11
    Registered User
    Join Date
    01-21-2010
    Location
    Akron Ohio
    MS-Off Ver
    Excel 2007
    Posts
    16

    Re: Converting Array to Integers

    i made both of your changes ... but it didnt seem to fix the problem ... when i compile it still comes up with error number 9.... on that same line of code ...

  12. #12
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Converting Array to Integers

    Hello cporter5,

    I don't have Excel 2007 yet, so this workbook is a 2003 version of yours. I get don't error the subscript out range error. I get a error with the Shape properties. Try this out and see how it works.
    Attached Files Attached Files

  13. #13
    Registered User
    Join Date
    01-21-2010
    Location
    Akron Ohio
    MS-Off Ver
    Excel 2007
    Posts
    16

    Re: Converting Array to Integers

    thanks its working now ... is there a line of code that i can add that will reset the macro ... right now the sequence can only be done once ... i would like as soon as its done once ... to be able to do the whole sequence 3 times ... so i would get 3 sets of a 5 digit number ...

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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