Closed Thread
Results 1 to 10 of 10

code to loop through column

Hybrid View

  1. #1
    Registered User
    Join Date
    06-08-2015
    Location
    United States
    MS-Off Ver
    2013
    Posts
    31

    code to loop through column

    hey guys,

    I am not very good with VBA but learning..

    I am trying to loop through a column in a document and when the word suit is found, I would like to copy and paste cell below it into another tab and continue to loop until blank row is found.

    any help would be appreciated.

  2. #2
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 insider Version 2504 Win 11
    Posts
    24,691

    Re: code to loop through column

    Here is some code that copies as you specified to column A on sheet2. The original data is in column A of sheet1.

    Option Explicit
    
    Sub suit()
        Dim s1 As Worksheet, s2 As Worksheet
        Set s1 = Sheets("Sheet1")
        Set s2 = Sheets("Sheet2")
        Dim lr As Long, lr2 As Long
        lr = s1.Range("A" & Rows.Count).End(xlUp).Row
        Dim i As Long
        
        Application.ScreenUpdating = False
        For i = 1 To lr
        lr2 = s2.Range("A" & Rows.Count).End(xlUp).Row + 1
        If s1.Range("A" & i) = "Suit" Then
            s1.Range("A" & i + 1).Copy s2.Range("A" & lr2)
        End If
        Next i
        Application.CutCopyMode = False
        Application.ScreenUpdating = True
        MsgBox "completed"
    
    End Sub
    Alan עַם יִשְׂרָאֵל חַי


    Change an Ugly Report with Power Query
    Database Normalization
    Complete Guide to Power Query
    Man's Mind Stretched to New Dimensions Never Returns to Its Original Form

  3. #3
    Registered User
    Join Date
    06-08-2015
    Location
    United States
    MS-Off Ver
    2013
    Posts
    31

    Re: code to loop through column

    Thanks, Alansidman!

    how would I be able to loop and copy cell that fall between the word "SUIT"

  4. #4
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 insider Version 2504 Win 11
    Posts
    24,691

    Re: code to loop through column

    how would I be able to loop and copy cell that fall between the word "SUIT"
    I don't understand what this means.

    The code I provided loops through all cells in column A until the last row. If it finds the word "Suit" it will copy the next cell below that and paste it to the next available cell in column A on sheet2. Is this what you asked for in your original thread or did I misunderstand?

  5. #5
    Registered User
    Join Date
    06-08-2015
    Location
    United States
    MS-Off Ver
    2013
    Posts
    31

    Re: code to loop through column

    almost perfect.

    I had 2 different suites in the same document. would that also follow the same criteria?

  6. #6
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 insider Version 2504 Win 11
    Posts
    24,691

    Re: code to loop through column

    Yes, just change the word Suit in the code or build an input box asking for which word to look for and place that variable in the code.

  7. #7
    Registered User
    Join Date
    06-08-2015
    Location
    United States
    MS-Off Ver
    2013
    Posts
    31

    Re: code to loop through column

    Alan,

    Thank you I think im beginning to understand... here is my sample worksheet. As you see its not much more indepth but.. is actually multiple SUIT items

    You can see each date underneath the SUIt represents an item for that suit.. This is what I am wanting to copy and paste into its corresponding tab
    Attached Files Attached Files

  8. #8
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 insider Version 2504 Win 11
    Posts
    24,691

    Re: code to loop through column

    How is this request different from the one you posted here: http://www.excelforum.com/excel-prog...ml#post4229746 where Alf supplied you with an answer which you did not acknowledge. This appears to be a duplicate request which under the forum rules is not allowed.

  9. #9
    Registered User
    Join Date
    06-08-2015
    Location
    United States
    MS-Off Ver
    2013
    Posts
    31

    Re: code to loop through column--- DUPLICATE (DO NOT RESPOND)

    I apologize,

    I never received notification and forgot I had posted this question.

    My apologies.

  10. #10
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 insider Version 2504 Win 11
    Posts
    24,691

    Re: code to loop through column

    Welcome to the Forum, unfortunately:

    This is a duplicate post and as such does not comply with Rule 5 of our forum rules. This thread will now be closed, you may continue in your other thread.

    Thread Closed.

Closed Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] How can I make this code shorter by using a loop. or there's any easier shorcuts code
    By romarkevinruiz in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 03-10-2014, 08:53 AM
  2. Loop through Column to find decimal then pass found row to bookmark code
    By beenbee in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-11-2014, 11:13 AM
  3. Code to turn this code into a loop doing muliple copies
    By fbplaya02 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-30-2013, 12:02 PM
  4. [SOLVED] Copy dynamically changing column and Paste using VBA Loop (Loop within Loop)
    By nixon72 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 02-12-2013, 12:46 PM
  5. [SOLVED] Loop till end of the sheet and loop back to first column again
    By smlim7 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-29-2012, 10:03 AM
  6. Code to loop through a column and change cell value based on list
    By davekippen in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 09-13-2011, 04:27 PM
  7. Have a loop, want it to go down a column and run the same code on each row
    By elduderino65 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-17-2010, 04:37 PM
  8. Loop a column on Sheet1 and loop a row on Sheet2 to find a match
    By johnnywinter in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 02-13-2009, 02:09 PM

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