+ Reply to Thread
Results 1 to 6 of 6

Looping through columns

Hybrid View

  1. #1
    Registered User
    Join Date
    07-16-2014
    Location
    melbourne australia
    MS-Off Ver
    office 365
    Posts
    41

    Looping through columns

    Hi all

    So got some code where i essentially do the same thing to each column between columns between cells G5 and P5

    Just wondering if theres a way i can rewrite this into a loop. I know how to loop through rows pretty easily, gust replace "C1" with "C"&x and have x=x+1 at the end of each loop. but am confused how to do it with columns. any advice would be greatly appreciated

    tbh dont really "need" it as the code will work fine as is, but it is more so i learn how to do it properly (as well as more cleanly and so i know how its done in the future if i ever need to accomplish something similar)

    code below
    Sheets("temp").Range("G5").Value = rando1
        rando1 = Int((Worksheets("vars").Range("C1").Value - Worksheets("vars").Range("B1").Value + 1) * Rnd + Worksheets("vars").Range("B1").Value)
        Sheets("temp").Range("H5").Value = rando1
        rando1 = Int((Worksheets("vars").Range("C1").Value - Worksheets("vars").Range("B1").Value + 1) * Rnd + Worksheets("vars").Range("B1").Value)
        Sheets("temp").Range("I5").Value = rando1
        rando1 = Int((Worksheets("vars").Range("C1").Value - Worksheets("vars").Range("B1").Value + 1) * Rnd + Worksheets("vars").Range("B1").Value)
        Sheets("temp").Range("J5").Value = rando1
        rando1 = Int((Worksheets("vars").Range("C1").Value - Worksheets("vars").Range("B1").Value + 1) * Rnd + Worksheets("vars").Range("B1").Value)
        Sheets("temp").Range("K5").Value = rando1
        rando1 = Int((Worksheets("vars").Range("C1").Value - Worksheets("vars").Range("B1").Value + 1) * Rnd + Worksheets("vars").Range("B1").Value)
        Sheets("temp").Range("L5").Value = rando1
        rando1 = Int((Worksheets("vars").Range("C1").Value - Worksheets("vars").Range("B1").Value + 1) * Rnd + Worksheets("vars").Range("B1").Value)
        Sheets("temp").Range("M5").Value = rando1
        rando1 = Int((Worksheets("vars").Range("C1").Value - Worksheets("vars").Range("B1").Value + 1) * Rnd + Worksheets("vars").Range("B1").Value)
        Sheets("temp").Range("N5").Value = rando1
        rando1 = Int((Worksheets("vars").Range("C1").Value - Worksheets("vars").Range("B1").Value + 1) * Rnd + Worksheets("vars").Range("B1").Value)
        Sheets("temp").Range("O5").Value = rando1
        rando1 = Int((Worksheets("vars").Range("C1").Value - Worksheets("vars").Range("B1").Value + 1) * Rnd + Worksheets("vars").Range("B1").Value)
        Sheets("temp").Range("P5").Value = rando1
    Last edited by AliGW; 04-15-2019 at 07:14 AM.

  2. #2
    Forum Moderator jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    Cibolo, TX
    MS-Off Ver
    Office 365
    Posts
    10,327

    Re: Looping through columns

    Untested, but this should work.

    Sub LoopThis()
        Dim i As Long
        For i = 7 To 16
            Sheets("temp").Cells(5, i).Value = rando1
            rando1 = Int((Worksheets("vars").Range("C1").Value - Worksheets("vars").Range("B1").Value + 1) * Rnd + Worksheets("vars").Range("B1").Value)
        Next i
    End Sub
    HTH
    Regards, Jeff

  3. #3
    Registered User
    Join Date
    07-16-2014
    Location
    melbourne australia
    MS-Off Ver
    office 365
    Posts
    41

    Re: Looping through columns

    Thanks so much Jeffrey!!!

    This method worked perfectly, will be sure to use it for reference in the future, cheers

  4. #4
    Forum Moderator jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    Cibolo, TX
    MS-Off Ver
    Office 365
    Posts
    10,327

    Re: [SOLVED] Looping through columns

    You are very welcome and thanks for the feedback.

  5. #5
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (both in England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2504 (Windows 11 Home 24H2 64-bit)
    Posts
    90,659

    Re: [SOLVED] Looping through columns

    Beba420 - I explained in your previous thread how to correctly mark a thread as solved. It is not the way you are doing it.

    In future, please select Thread Tools from the menu link above and mark this thread as SOLVED. I have changed it for you again this time.
    Ali


    Enthusiastic self-taught user of MS Excel who's always learning!
    Don't forget to say "thank you" in your thread to anyone who has offered you help. It's a universal courtesy.
    You can reward them by clicking on * Add Reputation below their user name on the left, if you wish.

    NB:
    as a Moderator, I never accept friendship requests.
    Forum Rules (updated August 2023): please read them here.

  6. #6
    Registered User
    Join Date
    07-16-2014
    Location
    melbourne australia
    MS-Off Ver
    office 365
    Posts
    41

    Re: [SOLVED] Looping through columns

    Hi Ali

    Yes i noticed that, in my defense ya did explain several hours after i marked both threads as solved the incorrect way (in fact im relatively certain i marked this one solved first). Rest assured the lesson is learned and ill start to do it the proper way. thank you for your help

+ 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. [SOLVED] Looping thru columns for value
    By Quint6778 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 11-09-2017, 04:59 PM
  2. [SOLVED] Looping through .Columns
    By efarkouh in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 04-21-2017, 02:01 PM
  3. [SOLVED] Looping through columns
    By Peterhmg in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 06-09-2016, 08:34 AM
  4. Looping through columns
    By figuraluk in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 10-20-2013, 01:06 PM
  5. Looping through two columns
    By pablowilks in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-17-2013, 02:46 AM
  6. Do Until Looping (not looping through all other columns)
    By orle8050 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 02-11-2013, 10:37 AM
  7. Looping over columns
    By LATrojan in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-27-2010, 08:12 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