+ Reply to Thread
Results 1 to 44 of 44

Repeat code without writing same code

  1. #1
    Registered User
    Join Date
    01-24-2017
    Location
    Kingman, Az
    MS-Off Ver
    2013
    Posts
    30

    Repeat code without writing same code

    Not sure what the title should say. But below, the code is basically an IF statement reading from a table. So far I got it going to row 13. I need it to go down to "maybe" row 30. I say maybe because I need it to continue all the way to what is specified in sheet11 F2. I don't want to have to go in and change the code everytime I want to change how many rows it reads.

    Please Login or Register  to view this content.

  2. #2
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,528

    Re: Repeat code without writing same code

    I can't speak for other people but it might just be easier if you explain what you want to do.
    Of course an example with a before and after is the best.

  3. #3
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,905

    Re: Repeat code without writing same code

    My guess is that you have absolutely no idea what programming is, you probably picked something up and then decide, "I'll just repeat it until I'm done"
    Your code starts fine then the loop and there you absolutely lost it, sorry but that's it, straight forward.
    Why don't you use the c.row instead of writing out every ror? "B4" "C4" "D8" etc. etc
    Replace all the occurrences RANGE("?3") with Range("?" & c.row) you do that for all the ones with the 3 in it and remove all the other ones.

    Hope this makes a little sense to you. Happy coding
    ---
    Hans
    "IT" Always crosses your path!
    May the (vba) code be with you... if it isn't; start debugging!
    If you like my answer, Click the * below to say thank-you

  4. #4
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,905

    Re: Repeat code without writing same code

    Try this version of your code:

    Please Login or Register  to view this content.

  5. #5
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,905

    Re: Repeat code without writing same code

    Minor chnage with the array

    Please Login or Register  to view this content.
    This will redimension the array as you go then you go an the array will be the same dimension as the data in it.

  6. #6
    Registered User
    Join Date
    01-24-2017
    Location
    Kingman, Az
    MS-Off Ver
    2013
    Posts
    30

    Re: Repeat code without writing same code

    I'm A noob. Very much so. This is a small part of lots of code pieced together over several years. Obviously it is not pretty. It worked to just wright every row before.

    Please Login or Register  to view this content.
    But my needs have changed and would like a repeater based on the value in sheet11 F2. As I said I'm a noob. I did not totally understand what you suggested as a change.
    The examples:


    Sheet11
    Group Wgt Min Min Age Max Wgt Max Age Weight Groups
    A 0 0 55 5
    B 0 6 63 7
    B 55.1 0 500 5
    C 0 8 77 9
    C 63.1 6 500 7
    D 77.1 8 500 9
    D 77.1 10 111 11
    E 0 10 77 11
    F 111.1 10 500 11
    F 111.1 12 500 18
    G 0 12 111 18

    Next is what will be set. Sheet 8 Column F (W/A Final).
    1st Name Last Name Weight Age Club W/A Final
    F L 33.0 5 Havasu
    F L 34.5 5 Mohave
    F L 41.2 7 Mohave
    F L 41.9 6 Parker
    F L 42.2 6 Mohave
    F L 60.7 7 Havasu
    F L 62.4 6 Mohave
    F L 63.0 6 Kingman
    F L 65.3 5 Parker
    F L 45.0 9 Havasu
    F L 48.8 8 Mohave
    F L 79.5 7 Mohave
    F L 82.2 7 Mohave
    F L 100.0 6 TOPOCK
    F L 78.0 11 TOPOCK
    F L 78.5 11 TOPOCK
    F L 80.5 10 Mohave
    F L 82.0 9 Kingman
    F L 82.2 8 Havasu

  7. #7
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,905

    Re: Repeat code without writing same code

    Minor change with the array

    Please Login or Register  to view this content.
    This will re-dimension the array as you go then you go an the array will be the same dimension as the data in it.

    I don't know what you're doing but this just about the vba code

  8. #8
    Registered User
    Join Date
    01-24-2017
    Location
    Kingman, Az
    MS-Off Ver
    2013
    Posts
    30

    Re: Repeat code without writing same code

    Keebellah thank you. Will test and see if it works. Again Thank you. I understand what you were trying to say now.

  9. #9
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,905

    Re: Repeat code without writing same code

    My guess is after looking at your coe, all the rows of code you repeated are identical except for the row number so my guess is this will work, the advantage... If you add more rows of data you do NOT have to modigy the VBA code.

    Hope it works for you.

    Noobs can become experts too, look at me LOL

  10. #10
    Registered User
    Join Date
    01-24-2017
    Location
    Kingman, Az
    MS-Off Ver
    2013
    Posts
    30

    Re: Repeat code without writing same code

    Went to test it and got a syntax error

    Please Login or Register  to view this content.

  11. #11
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,905

    Re: Repeat code without writing same code

    Sorry, my mistake, I typed it outside Excel in an Editor

    Please Login or Register  to view this content.
    I forgot the .Row between the ))
    Last edited by Keebellah; 01-25-2017 at 05:37 PM. Reason: my mistake

  12. #12
    Registered User
    Join Date
    01-24-2017
    Location
    Kingman, Az
    MS-Off Ver
    2013
    Posts
    30

    Re: Repeat code without writing same code

    Not to be a pain, but now its saying "expecting end of statement"

  13. #13
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,905

    Re: Repeat code without writing same code

    Loose the last ) after Row

  14. #14
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,905

    Re: Repeat code without writing same code

    I'm just typing it in so no check on-the-fly from Excel's VBA

  15. #15
    Registered User
    Join Date
    01-24-2017
    Location
    Kingman, Az
    MS-Off Ver
    2013
    Posts
    30

    Re: Repeat code without writing same code

    I appreciate all of your help. It isn't doing anything. Gonna see if I can play with it and figure it out. Again TY!

  16. #16
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,905

    Re: Repeat code without writing same code

    Is it possible you attach a sample file with non-sensitive data so that I can see if I can figure out what may be going wrong?
    You could also step through the VBA code by opening the VBA editor, select the beginning of the macro and press F8 that way you can take it line for line

  17. #17
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,905

    Re: Repeat code without writing same code

    I did a search and replace and ALL where replaced with c.Row

    Please Login or Register  to view this content.
    This way should be

    Please Login or Register  to view this content.
    BTW I get all none with the data you put in your post

  18. #18
    Registered User
    Join Date
    01-24-2017
    Location
    Kingman, Az
    MS-Off Ver
    2013
    Posts
    30

    Re: Repeat code without writing same code

    I tried that line before and the top row worked, but the rest said none as you said. Still having an issue with that line of code. There was an issue with a missing S further up in the code. I forgot where. I noticed that this code does not have and end with. I tried adding it and obviously it didn't work. any other ideas?

  19. #19
    Registered User
    Join Date
    01-24-2017
    Location
    Kingman, Az
    MS-Off Ver
    2013
    Posts
    30

    Re: Repeat code without writing same code

    This is what I added the s to. nrofRow
    Please Login or Register  to view this content.

  20. #20
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,905

    Re: Repeat code without writing same code

    You don't need the Edn With becasue I removed the first With, it's unncessary:

    I pasted the data for Sheet 11 and tried it, just to make it clear I edited the VBA code to make sure it checks .Value

    Please Login or Register  to view this content.
    And the result is none and that is true according to the conditions you have laid
    The result for each row in a text file:

    Please Login or Register  to view this content.
    It shows each Row
    The column checked and the value and the end if it's true or fale

    I change column E in the last row to 118 and then I get both TRUE and the G instead of none

    You mention Sheet 8 but this code you hard coded for Sheet 11.

    To make it work for sheet 8 I would suggest a variable for the Worksheet and then set it to the active worksheet

    If you want to play with VBA you must go into it a little more and practice by trial and error will get you very far, I started from scratch (okay somewhere around 1995) but still I started by trying and still do that to learn new techniques

  21. #21
    Registered User
    Join Date
    01-24-2017
    Location
    Kingman, Az
    MS-Off Ver
    2013
    Posts
    30

    Re: Repeat code without writing same code

    Sheet 8 is where I run the marco from. Will try out the new code and get back. If it doesn't work, I will make a partial to upload.

  22. #22
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,905

    Re: Repeat code without writing same code

    This code should NOT be in a Sheet is should be in a module

  23. #23
    Registered User
    Join Date
    01-24-2017
    Location
    Kingman, Az
    MS-Off Ver
    2013
    Posts
    30

    Re: Repeat code without writing same code

    Tried to attach file. Could not accomplish. posting this and than will figure out how.

    Three buttons. My old one. That worked, but obviously not simple or complete. Your code. Doesn't work, but accomplishes what I want. And clear. Clears F, G, and H. G and H are not used in this example. These buttons are also on my workbook. It may seem silly but the overall picture they have a purpose.

  24. #24
    Registered User
    Join Date
    01-24-2017
    Location
    Kingman, Az
    MS-Off Ver
    2013
    Posts
    30

    Re: Repeat code without writing same code

    Edited: uploaded.
    Attached Files Attached Files
    Last edited by wrestlemy69; 01-25-2017 at 08:05 PM.

  25. #25
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,528

    Re: Repeat code without writing same code

    Can you read the first line in post #2 again and answer that?
    Your attachment does not tell me anything at all. But that can be me though.

  26. #26
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,905

    Re: Repeat code without writing same code

    Well, next step, nice to post an xlsx file, this is NOT the file with the macros so please, upload the file WITH the macros, that would be an xlsm file.
    Read how to upload, so just create the file again make sure to save it as an macro embedded file, how hard can it be?

  27. #27
    Registered User
    Join Date
    01-24-2017
    Location
    Kingman, Az
    MS-Off Ver
    2013
    Posts
    30

    Re: Repeat code without writing same code

    I do appreciate your response, but Not sure what I can tell you that has not already been said or shown but will try again. Using my attachment I want a code that does exactly what my "Button" does, but does not stop at a defined row. Where as mine does. Row 13. I want it to stop at the last amount of data on sheet 11.

    What it does. It takes a kid who has a certain weight and age and puts him in a weight/age class. Each class (a, b, c, etc) is defined on sheet11. It than takes the weight class assignment and puts it in sheet8.

    Hope this helped. Any other questions?

    Will try to attach another.

  28. #28
    Registered User
    Join Date
    01-24-2017
    Location
    Kingman, Az
    MS-Off Ver
    2013
    Posts
    30

    Re: Repeat code without writing same code

    Here we go.
    Attached Files Attached Files

  29. #29
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,905

    Re: Repeat code without writing same code

    I'll pick-up the file and see what you mean and also see if i understand it.
    I'll let you know.

  30. #30
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,905

    Re: Repeat code without writing same code

    Let me get to it right away.
    I am suming that it's the Module5 macro sorttest your want me to look at.
    Okay, first and VERY important, you do not refer to the worksheets or even test if you're in the correct worksheet.
    This is a very risky situation.
    Take your ClearSort maro
    you just select the ranges and clearcontents, I imagine that if you're in the correct worksheet that is okay but what if you have a worksheet with data in columns a though z and then you accidentally clear the contents of columns F through H the first 500 rows, I imagine your memberslist database would be very much in trouble or another one which has been painstakingly filled.

    I hope you can imagine and undestand my concern.
    A macro is a stupid and dumb piece of sequential instructions that does exactly what the programmer has put into it and has no intelligence whatsoever.

    Please tell me if my assumptions are correct:
    ClearSort is to clear the contents of columns F-H in Sheet 8 (I would better give this sheet a descriptive name like Weight Group or whatever

    sortest is the macro that checks Sheet 8's values and compares then to the table in Sheet 11 so that column F in Sheet 8 gets filled?

    If this is correct then the MAcro sorttest does NOT in anyway indicate that it requires the data of Sheet 8 to x-check with sheet 11.
    You just mention Sheet 11 but if somebody starts this macro directly from the macro list and has by accident Sheet 3 as active sheet, my guess you're going to loose data in column F of that sheet.

    If they only use the buttons then maybe you can avoid this but the macrolist is always availabe from the menu bar

    Hope my comncerns are clear to you and if I have been able to explain myself correctly

    Like I said, I'll check is out and get back to you later in the morning, I've got some ore things to do right now

  31. #31
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,905

    Re: Repeat code without writing same code

    I had to finish it.
    I did NOT check your code I corrected my code which you named TextBox4_click
    I did fix the Clear button (simplified it)
    I used the Sheet8 and Sheet11 names so if you change the sheet names you will have to do so in my modified code too.
    Here's my file and it looks like it works, I get several G's
    Attached Files Attached Files

  32. #32
    Registered User
    Join Date
    01-24-2017
    Location
    Kingman, Az
    MS-Off Ver
    2013
    Posts
    30

    Re: Repeat code without writing same code

    I appreciate all your help. But here is the problem. Yours still does not work. I included mine, not to check the code, but to see what mine does simply by pressing the button. Your code should get the exact same result as mine. Every single row should be grouped into a weight class. There should be No "None". It seems you changed your code. I am going to try and look at it and see if I can find the issue. But I doubt it, lol.

    I totally get what you mean by not identifying. The clear button was mainly for test purposes. So I could clear the cells with one click and press a button again. I will only be using buttons. The main reason for this is simplicity as people who have never used excel before will be using this.

    It seems you understand the concept of what I am trying to accomplish. Again thank you for your help.

  33. #33
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,905

    Re: Repeat code without writing same code

    The strange thing is only the last rows return a group (G) with my code while the others return none, as far as comparing what your code does and mine I could not detect any differences.

    If it's not problem, could you send your file as you are using it now so that I'm sure we're using the same version?

  34. #34
    Registered User
    Join Date
    01-24-2017
    Location
    Kingman, Az
    MS-Off Ver
    2013
    Posts
    30

    Re: Repeat code without writing same code

    The file I sent was my file just renamed and a bunch of stuff removed. But I will upload the full thing. The one you uploaded, when I press my code button it populates everything. Your button as you said is just doing G. I checked the logic and It seems right.

    Will upload the full file soon.

  35. #35
    Registered User
    Join Date
    01-24-2017
    Location
    Kingman, Az
    MS-Off Ver
    2013
    Posts
    30

    Re: Repeat code without writing same code

    Full file upload
    Attached Files Attached Files

  36. #36
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,905

    Re: Repeat code without writing same code

    Thanks, will take a shot at it again, don't know if I'll be able to get to it tonight else tomorrow

  37. #37
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,528

    Re: Repeat code without writing same code

    Would this work for you?
    Different approach.
    Run while in Sheet8.
    Please Login or Register  to view this content.

  38. #38
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,905

    Re: Repeat code without writing same code

    Okay, here's the working code for TextBox4_Click
    Please Login or Register  to view this content.
    Your approach threw me completely off with the Select Case True
    I modified all that and now it works

  39. #39
    Registered User
    Join Date
    01-24-2017
    Location
    Kingman, Az
    MS-Off Ver
    2013
    Posts
    30

    Re: Repeat code without writing same code

    Thank you. Both of you. Keebellah works perfectly. I threw a couple of test at it and it worked flawlessly. So thank you very much for all your hard work and not giving up.

    jolivanes I had two Issues. Other than that it worked flawlessly. I would change it, But I SO DON'T understand your code. Seemed to work well. Gonna take a longer look at it to see if I can figure it out. The two issues. 1 it landed in column G not F. 2nd if it doesn't meet the criterea I need it to say "none".

    Not sure which I'm going to use. But as I said THANK YOU!!!!!!!!!!

  40. #40
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,528

    Re: Repeat code without writing same code

    Re: it landed in column G not F
    Change
    Please Login or Register  to view this content.
    To
    Please Login or Register  to view this content.
    Re: if it doesn't meet the criteria I need it to say "none".
    Which ones should say "None"?
    I changed the values in the code to -0.1 and +0.1 to have the full numbers included.
    If you say >5 than 5 itself is not included.
    For example, if you want to check if it is more than 5 and less than 10, that would only be 6, 7, 8 and 9.
    If you want to have 5 and 10 included, you can check for 4.9 to 10.1
    That can be changed without problem but first let me know which ones should say "None". (Manually check all)

    If you know you have some in there that should say "None", try this maybe.
    Please Login or Register  to view this content.
    I should have mentioned that this is probably more like it. No need to add or subtract the 0.1.
    Please Login or Register  to view this content.
    Last edited by jolivanes; 01-26-2017 at 10:35 PM. Reason: Add info

  41. #41
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,905

    Re: Repeat code without writing same code

    My mistake was that I forgot the Exit For if the condition was met.
    The Select Case statement is really unnecessary because it is not True or False situation.
    Your first check it to see if the cells is not empty, then a loop to see in which group the member fits and if found set a flag (i used x=1) and exit the loop
    Upon exiting the loop check if the flag was set, if yes ten continue if not then fill in "none"

    This about covers everything so if you decide to add another criteria to the chart in Sheet11 it will be taken into account without having to modify the vba code unless you move around columns

    I hope this explains my approach and it was Jolivane's code that triggered the exit for which I had overlooked

  42. #42
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,528

    Re: Repeat code without writing same code

    Re: I forgot the Exit For
    Maybe it would be wise to refrain from tooting your own horn like you did at:
    "Noobs can become experts too, look at me"

    While we're at it, the following is uncalled for.
    "My guess is that you have absolutely no idea what programming is"
    Right or wrong, we should respect people who at least try. Quite a few don't even do that.

  43. #43
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,905

    Re: Repeat code without writing same code

    @jolivanes: I stand corrected, however my first statement was meant sarcastically and the second, I went too far on that one.

  44. #44
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,528

    Re: Repeat code without writing same code

    No problem (I think)
    Het siert een mens als ze hun fout kunnen toegeven.
    Have a good weekend en thanks for the contributions of course.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. re-writing VBA Code
    By ShakJames in forum Excel General
    Replies: 2
    Last Post: 02-13-2015, 11:05 AM
  2. Writing a Formula using R1C1 code in VBA code
    By e4excel in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 01-15-2014, 11:06 AM
  3. Need Macro to Transfer Data from sheet 2 to sheet 1
    By STU22 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-25-2013, 03:09 PM
  4. Need date wise In and out time for employee ID
    By priyanka.r8523 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-10-2013, 06:30 AM
  5. New to VBA code writing
    By Justair07 in forum Hello..Introduce yourself
    Replies: 1
    Last Post: 01-08-2013, 07:38 PM
  6. Need Help on Writing a Code... Please
    By uu_urfriend in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-10-2012, 09:34 PM
  7. writing VBA code
    By donyc in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 09-14-2010, 03:54 PM

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