+ Reply to Thread
Results 1 to 4 of 4

Ranges and Arrays

  1. #1
    Will Brown
    Guest

    Ranges and Arrays

    I have two questions, and before I ask them there is one thing you
    should know: I'm a newbie.

    Question 1: I am making an encryption program for an IT project. I need
    to have the user type in (via a userform) a string, have the program
    take a character from the string and then put that into a sheet, first
    char in A1, second in A2, etc., which does calculations on the character
    to encrypt it. I can figure out how to code all of it besides the put
    the characters into a sheet part. I thought the easiest way would be to
    do this:

    Dim char As String
    Dim text As String
    Dim i As Integer

    While i < len(text)
    char = Mid(text, i, 1)
    (range code here).Value = char
    Wend

    but I have no idea how to make it put it in the right range! Is there a
    way to make it so that the program puts the value of char into a range
    based on i, like Sheet1.Range("A" + i) (that doesn't work, I already
    tried)??

    Question 2: Is there any way to create an array (like a matrix) in excel
    without writing to a sheet?

    Thank you very much for your time, and your help is very much
    appreciated!!!!

    Will Brown

    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!

  2. #2
    JulieD
    Guest

    Re: Ranges and Arrays

    Hi Will

    to put it out to a worksheet you can use the following code


    Dim i As Long
    For i = 1 To Len(TextBox1.Value)
    Sheets("Sheet1").Range("A1").Offset(0, i - 1).Value =
    Mid(TextBox1.Value, i, 1)
    Next

    note, you might like to include some error handling to deal with the
    situation where the person types in more than 256 characters.

    and yes, you can use arrays in excel - therefore an alternative would be:

    Option Base 1
    Private Sub CommandButton1_Click()
    Dim myarray() As String
    i = Len(TextBox1.Value)
    ReDim myarray(i)
    For i = 1 To Len(TextBox1.Value)
    myarray(i) = Mid(TextBox1.Value, i, 1)
    Next
    'write out to a message box to prove it works
    For i = 1 To Len(TextBox1.Value)
    MsgBox myarray(i)
    Next
    End Sub
    ---

    hope this helps
    Cheers
    JulieD

    ---------

    "Will Brown" <[email protected]> wrote in message
    news:[email protected]...
    >I have two questions, and before I ask them there is one thing you
    > should know: I'm a newbie.
    >
    > Question 1: I am making an encryption program for an IT project. I need
    > to have the user type in (via a userform) a string, have the program
    > take a character from the string and then put that into a sheet, first
    > char in A1, second in A2, etc., which does calculations on the character
    > to encrypt it. I can figure out how to code all of it besides the put
    > the characters into a sheet part. I thought the easiest way would be to
    > do this:
    >
    > Dim char As String
    > Dim text As String
    > Dim i As Integer
    >
    > While i < len(text)
    > char = Mid(text, i, 1)
    > (range code here).Value = char
    > Wend
    >
    > but I have no idea how to make it put it in the right range! Is there a
    > way to make it so that the program puts the value of char into a range
    > based on i, like Sheet1.Range("A" + i) (that doesn't work, I already
    > tried)??
    >
    > Question 2: Is there any way to create an array (like a matrix) in excel
    > without writing to a sheet?
    >
    > Thank you very much for your time, and your help is very much
    > appreciated!!!!
    >
    > Will Brown
    >
    > *** Sent via Developersdex http://www.developersdex.com ***
    > Don't just participate in USENET...get rewarded for it!




  3. #3
    Tom Ogilvy
    Guest

    Re: Ranges and Arrays

    Dim char As String
    Dim text As String
    Dim i As Integer

    i = 1
    Do While i <= len(text)
    char = Mid(text, i, 1)
    worksheets("Sheet1").Cells(2,i).value = char
    i = i + 1
    Loop

    or to populate an array

    Dim char As String
    Dim text As String
    Dim i As Integer
    Dim v() as String


    redim v(1 to len(text))
    i = 1
    Do While i <= len(text)
    char = Mid(text, i, 1)
    v(i) = char
    i = i + 1
    Loop

    --
    Regards,
    Tom Ogilvy


    "Will Brown" <[email protected]> wrote in message
    news:[email protected]...
    > I have two questions, and before I ask them there is one thing you
    > should know: I'm a newbie.
    >
    > Question 1: I am making an encryption program for an IT project. I need
    > to have the user type in (via a userform) a string, have the program
    > take a character from the string and then put that into a sheet, first
    > char in A1, second in A2, etc., which does calculations on the character
    > to encrypt it. I can figure out how to code all of it besides the put
    > the characters into a sheet part. I thought the easiest way would be to
    > do this:
    >
    > Dim char As String
    > Dim text As String
    > Dim i As Integer
    >
    > While i < len(text)
    > char = Mid(text, i, 1)
    > (range code here).Value = char
    > Wend
    >
    > but I have no idea how to make it put it in the right range! Is there a
    > way to make it so that the program puts the value of char into a range
    > based on i, like Sheet1.Range("A" + i) (that doesn't work, I already
    > tried)??
    >
    > Question 2: Is there any way to create an array (like a matrix) in excel
    > without writing to a sheet?
    >
    > Thank you very much for your time, and your help is very much
    > appreciated!!!!
    >
    > Will Brown
    >
    > *** Sent via Developersdex http://www.developersdex.com ***
    > Don't just participate in USENET...get rewarded for it!




  4. #4
    Bob Phillips
    Guest

    Re: Ranges and Arrays

    Will,

    I don't understand why you want to put it into a range, or even into a VBA
    array. Why not just work on each character in a similar way to what you have

    For i = 1 To Len(Text)
    val = EncryptChar(Mid(text,i,1))
    'do whatever with val
    Next i

    where EncryptChar is the encrypting function which returns a value that you
    process.

    --

    HTH

    RP
    (remove nothere from the email address if mailing direct)


    "Will Brown" <[email protected]> wrote in message
    news:[email protected]...
    > I have two questions, and before I ask them there is one thing you
    > should know: I'm a newbie.
    >
    > Question 1: I am making an encryption program for an IT project. I need
    > to have the user type in (via a userform) a string, have the program
    > take a character from the string and then put that into a sheet, first
    > char in A1, second in A2, etc., which does calculations on the character
    > to encrypt it. I can figure out how to code all of it besides the put
    > the characters into a sheet part. I thought the easiest way would be to
    > do this:
    >
    > Dim char As String
    > Dim text As String
    > Dim i As Integer
    >
    > While i < len(text)
    > char = Mid(text, i, 1)
    > (range code here).Value = char
    > Wend
    >
    > but I have no idea how to make it put it in the right range! Is there a
    > way to make it so that the program puts the value of char into a range
    > based on i, like Sheet1.Range("A" + i) (that doesn't work, I already
    > tried)??
    >
    > Question 2: Is there any way to create an array (like a matrix) in excel
    > without writing to a sheet?
    >
    > Thank you very much for your time, and your help is very much
    > appreciated!!!!
    >
    > Will Brown
    >
    > *** Sent via Developersdex http://www.developersdex.com ***
    > Don't just participate in USENET...get rewarded for it!




+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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