Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Visual Basic Code.

I was wondering if someone would help me. I have 22 lines of code and I would like to know what EACH line MEANS. Would anyone be willing to explain each line? Here they are! Thank you, to whoever takes this on! :) Here's the code:

1. Do Until ActiveCell.Value = ""
2. counter = 0
3. tempchr = ""
4. FullName = ActiveCell.Value
5. Do Until tempchr = " "
6. counter = counter + 1
7. tempchr = Mid(FullName, counter, 1)
8. Loop
9. firstname = Left(FullName, counter - 1)
10. ActiveCell.Offset(0, 1).Range("A1").Select
11. ActiveCell.FormulaR1C1 = firstname
12. ActiveCell.Offset(0, 1).Range("A1").Select
13. counter = Len(FullName) + 1
14. tempchr = ""
15. Do Until tempchr = " "
16. counter = counter - 1
17. tempchr = Mid(FullName, counter, 1)
18. Loop
19. lastname = Right(FullName, Len(FullName) - counter)
20. ActiveCell.FormulaR1C1 = lastname
21. ActiveCell.Offset(1, -2).Range("A1").Select
22. Loop



Comments

  • JomacJomac Posts: 231Member
    : I was wondering if someone would help me. I have 22 lines of code and I would like to know what EACH line MEANS. Would anyone be willing to explain each line? Here they are! Thank you, to whoever takes this on! :) Here's the code:
    :
    : 1. Do Until ActiveCell.Value = ""
    : 2. counter = 0
    : 3. tempchr = ""
    : 4. FullName = ActiveCell.Value
    : 5. Do Until tempchr = " "
    : 6. counter = counter + 1
    : 7. tempchr = Mid(FullName, counter, 1)
    : 8. Loop
    : 9. firstname = Left(FullName, counter - 1)
    : 10. ActiveCell.Offset(0, 1).Range("A1").Select
    : 11. ActiveCell.FormulaR1C1 = firstname
    : 12. ActiveCell.Offset(0, 1).Range("A1").Select
    : 13. counter = Len(FullName) + 1
    : 14. tempchr = ""
    : 15. Do Until tempchr = " "
    : 16. counter = counter - 1
    : 17. tempchr = Mid(FullName, counter, 1)
    : 18. Loop
    : 19. lastname = Right(FullName, Len(FullName) - counter)
    : 20. ActiveCell.FormulaR1C1 = lastname
    : 21. ActiveCell.Offset(1, -2).Range("A1").Select
    : 22. Loop
    :
    :

    OK Now what each line does depends on the rest of the program, but I will do my best.

    Line 1: This causes the program to loop through the code until the Active Cell contains nothing. It is paired with the 'Loop' statment on line 22.

    Line2:
    Sets a variable called 'counter' to the value 0 (nothing), at the start of each loop.

    Line3:
    Sets a variable called 'tempchr' to the value "" (nothing), at the start of each loop.

    Line4:
    Sets a variable called 'FullName' to the value of the Active Cell.

    Line5:
    This starts another loop within the one started at line 1 But this one continues until the variable 'tempchr' contains a single space(" "), This is paired with the 'Loop' statment at line 8.

    Line6:
    Adds 1 to the curent value of the variable 'counter'.

    Line7:
    This Line extracts a single character from the variable 'FullName', starting at the charater 'counter' number of spaces from the left and stores it in 'tempchr'.
    Note: If this character was a single space (" "), then this loop will end.

    Line8:
    This is the 'Loop' statment that sends the program back to Line 5

    Line9:
    Gets all the characters to the left of the space (" ") found in the previous loop, and stores it back in the variable 'FullName' (eg. turns "Jomac Inc." into "Jomac")

    Line10 - 12:
    I am unsure of the exact result of these lines but is part of the excel.application object.

    Line13:
    Sets the variable 'counter' to the number of characters in the variable 'FullName', + 1 (eg. if FullName = "Jomac" it would set 'counter' = 6).

    Line14:
    Sets the variable'tempchr' back to "" (nothing).

    Line15 - 18:
    Repeats what is done between lines 5 - 8.

    Line19:
    Does the same as line 9, but this time gets all the characters to the right of the space (" ").

    Line20 - 21:
    Again I am unsure.

    Line22:
    This is the 'Loop' statment that sends the program back to Line 1

    I am afraid this is the best explination I can give you based on the information provided, however looking at the code I can see many better ways of doing the same thing with less code and less bugs.

    Lines 5 to 9 could be replaced with a single line:

    FullName = Split(FullName, " ")(0)

    Lines 15 to 19 could be replaced with a single line:

    FullName = Split(FullName, " ")(1)

    Anyway best of luck.

    Regards,

    Regards,
    Jomac Inc.


Sign In or Register to comment.