+ Reply to Thread
Results 1 to 8 of 8

How do you loop through columns

  1. #1
    Registered User
    Join Date
    01-29-2013
    Location
    nevada
    MS-Off Ver
    Excel 2012
    Posts
    9

    How do you loop through columns

    I'm new to to vbs script in excel, and i'm stuck on looping through column function. I have the function below it does what I want but it stop after one iteration. I want it to loop through entire range A1 until the cell is blank. Range A1 contain given numbers, and functions below that does the sum until it hit 100; once it get 100 total. The paste of functions right next to the cell of 100 continue until the end of range A1. The number of sum equal 100 is varies between 4 column and 6 column.

    once the sum of cell is 100, the adjacent cell is blank even with formula exist. So, I'm checking for blank cell on A2 range to repeat the process of pasting, rather than the value 100.

    Please Login or Register  to view this content.
    ============================================
    A B C D E F G H I J K
    1 10 10 10 40 30 10 20 30 40 10 15
    2 10 20 30 70 100 10 30 60 100 10 25
    3 # # # # # #
    4 # # # # # #
    5 # # # # # #
    6
    7
    8
    9 Reference Range of Formula to copy and paste
    10 10 10 10 40 30
    11 10 20 30 70 100
    12 # # # # # #
    13 # # # # # #
    14
    15

    Thanks,
    Fred

    Moderators Note:
    • Please follow Forum Rule #3 and use code tags.
    • Added for you this time, but please use them in the future…Thanks.
    Last edited by fredcho; 01-29-2013 at 10:06 PM. Reason: clarify

  2. #2
    Forum Expert
    Join Date
    07-15-2012
    Location
    Leghorn, Italy
    MS-Off Ver
    Excel 2010
    Posts
    3,431

    Re: How do you loop through columns

    you say "I want it to loop through entire range A1" but A1 i only a cell, how can you loop through a single cell ?
    You say "Range A1 contain given numbers" but one cel can contain only one number
    explain better your goal, attach a sample file with current and desired table
    If solved remember to mark Thread as solved

  3. #3
    Registered User
    Join Date
    01-29-2013
    Location
    nevada
    MS-Off Ver
    Excel 2012
    Posts
    9

    Re: How do you loop through columns

    that explained why i'm stucked, I've no clue.

    row 1 is given numbers.
    row 2 column 1 is blank then copy the formula from given range. Then scan again for blank cell on row2 for blank cell and repeat pasting process until the end, where cell in row1 is blank (end of given numbers).

  4. #4
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: How do you loop through columns

    This will loop through cells in row1 and place the word help. Maybe you can apply the idea to your code.

    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    01-29-2013
    Location
    nevada
    MS-Off Ver
    Excel 2012
    Posts
    9

    Re: How do you loop through columns

    Quote Originally Posted by JOHN H. DAVIS View Post
    This will loop through cells in row1 and place the word help. Maybe you can apply the idea to your code.

    Please Login or Register  to view this content.
    I'm still cluless of what is cell and what's Range, but combine John's Code with mine in previous posted it does what I want:
    [code
    sub TryMe()
    Dim Rng As Range
    Dim x As Long
    For x = 1 To ActiveSheet.UsedRange.Columns.Count
    Set Rng = ActiveSheet.Range(Cells(2, x), Cells(2, x))
    If WorksheetFunction.CountA(Rng) = 0 Then
    If ActiveSheet.Range(Cells(1, x), Cells(1, x)) <> "" Then
    ActiveSheet.Range("A16:F20").Copy Destination:=Rng
    End If
    End If
    Next x
    End Sub
    [/code]

  6. #6
    Registered User
    Join Date
    01-29-2013
    Location
    nevada
    MS-Off Ver
    Excel 2012
    Posts
    9

    Re: How do you loop through columns

    Now, I'm running into new issue. the formula has width up to 6 column, the script does copy every 6th columns until the end.

    some of the sum equal zero on the 5th column, I need the script able to know that and start to paste on the 5th cell (the blank cell).

    Please see attached.

    Thanks,
    Fred.
    Attached Files Attached Files

  7. #7
    Registered User
    Join Date
    01-29-2013
    Location
    nevada
    MS-Off Ver
    Excel 2012
    Posts
    9

    Re: How do you loop through columns

    Can someone help me correct statement below? I got compile error when run the code :

    Working Statement: If ActiveSheet.Range(Cells(2, x), Cells(2, x)) = "" Then
    Compile Error Statement: If ActiveSheet.Range(Cells(2, x), Cells(2, x)).Offset(0, -2) = "" Then

    Please Login or Register  to view this content.

  8. #8
    Registered User
    Join Date
    01-29-2013
    Location
    nevada
    MS-Off Ver
    Excel 2012
    Posts
    9

    Re: How do you loop through columns

    Solved.

    Working Code:

    Please Login or Register  to view this content.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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