+ Reply to Thread
Results 1 to 10 of 10

VBA Loops + Arrays

  1. #1
    Registered User
    Join Date
    09-26-2010
    Location
    Virginia
    MS-Off Ver
    Excel 2007
    Posts
    6

    VBA Loops + Arrays

    Hey guys, I am a student and am taking a class in VBA modeling. I've had one assignment prior to this one where I had to use a Do loop, but other than that I am not skilled in VBA whatsoever. I was hoping I could post the description of the project and get someone to lead me in the right direction. This assignment requires the use of Arrays, and I do not understand them at all as well as other parts of this project. Thanks in advance for any help.

    The finished product should have two boxes, one for the encrypted data and one for the un encrypted, and also two buttons, one to encrypt and the other to un-encrypt.
    Suppose we want to encrypt data for security reasons. Assuming the “data” entered is always a 4 digit integer, write a program to encrypt the data as follows: replace each digit by (the sum of that digit plus 7) modulus 10, then swap the first digit with the third and swap the second digit with the fourth. Print the encrypted integer in the box (Hint: to build a sting of integers, use the concatenate symbol &). For this assignment, you must use an array to store the four values, and put it within a loop for extracting the values using the MID function. Next, write a routine to decrypt a number back to its original value.
    Last edited by Kengh; 09-26-2010 at 06:44 PM.

  2. #2
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,330

    Re: Help with on of my first VBA assignments

    Hi Kengh,
    Welcome to the forum.
    Please take a few minutes to read the forum rules, and then amend your thread title accordingly
    If the solution helped please donate to RSPCA

    Site worth visiting: Rabbitohs

  3. #3
    Registered User
    Join Date
    09-26-2010
    Location
    Virginia
    MS-Off Ver
    Excel 2007
    Posts
    6

    Re: My first VBA assignment

    So far I have come up with:

    Please Login or Register  to view this content.
    Last edited by Kengh; 09-26-2010 at 06:44 PM.

  4. #4
    Forum Expert
    Join Date
    07-16-2010
    Location
    Northumberland, UK
    MS-Off Ver
    Excel 2007 (home), Excel 2010 (work)
    Posts
    3,054

    Re: My first VBA assignment

    I still don't think you're there with your title - try something that describes the problem, "Help understanding arrays", perhaps.

    To answer your main question an array is a variable which has multiple elements, referenced by an index number (or numbers, but let's not go there yet).

    So, for example, if I wanted to write a VBA program to ask for three numbers I could code this...

    Please Login or Register  to view this content.
    That would work, but if I wanted to get 10 numbers, or 100, or even more it would get very, very long-winded and be hard to work with, so I could define an array, like so...

    Please Login or Register  to view this content.

    Functionally these two programs are identical, but the second one can be expanded to allow for the input of any number of numbers, just by changing the value assigned to HowManyNumbers.

    All of the numbers input are stored in the array GetNumber, but each one is stored in a unique element of that array, referenced by number, so if I wanted to output the first number entered I could say

    Please Login or Register  to view this content.

    More importantly I can deal with the numbers one at a time by putting them inside a For...Next loop, so I could output the numbers in the order they were entered with the code...

    Please Login or Register  to view this content.

    Arrays are absolutely essential to efficient coding, because they allow you to deal with sets of values, rather than having to reference each one individually. I don't mean to be rude, but if you're taking a VBA course shouldn't they be teaching you this sort of thing?

  5. #5
    Forum Expert teylyn's Avatar
    Join Date
    10-28-2008
    Location
    New Zealand
    MS-Off Ver
    Excel 365 Insider Fast
    Posts
    11,372

    Re: My first VBA assignment

    Kengh, that title is in no way better. It does not describe the issue and would be useless in a Google search. Also, make sure to wrap your code in code tags.

    You have accepted the forum rules when you joined. Read them!

  6. #6
    Forum Expert
    Join Date
    07-16-2010
    Location
    Northumberland, UK
    MS-Off Ver
    Excel 2007 (home), Excel 2010 (work)
    Posts
    3,054

    Re: My first VBA assignment

    You need to put your code in code tags, or you'll be in more trouble.

    As this is an assignment I think it would be wrong for me to just give you the answer, but I've put a few comments in your code...

    Please Login or Register  to view this content.

  7. #7
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,330

    Re: My first VBA assignment

    hi Kengh
    If you are having problems changing the title or adding code tags let me know and I can help

  8. #8
    Registered User
    Join Date
    09-26-2010
    Location
    Virginia
    MS-Off Ver
    Excel 2007
    Posts
    6

    Re: VBA Loops + Arrays

    Changed my thread title and depicted the code properly. I am having trouble understand what loop to make for this and how to make it Mod10 as it says in the project description. basically the encrypted number is supposed to be 4 digits just like the encrypted one, so adding 7 to a number higher than 3 gives you a two digit number and that will not work.

  9. #9
    Registered User
    Join Date
    09-26-2010
    Location
    Virginia
    MS-Off Ver
    Excel 2007
    Posts
    6

    Re: VBA Loops + Arrays

    Gahhh this may be close, but still wrong. Any help is greatly appreciated!
    Please Login or Register  to view this content.
    What do I need to change

  10. #10
    Registered User
    Join Date
    09-26-2010
    Location
    Virginia
    MS-Off Ver
    Excel 2007
    Posts
    6

    Re: VBA Loops + Arrays

    Update: Got it to work thanks to Andrew-r! Thank you to everyone that helped!
    Last edited by Kengh; 09-26-2010 at 08:02 PM.

+ 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