+ Reply to Thread
Results 1 to 11 of 11

Loop issue

Hybrid View

jsmilke Loop issue 03-03-2014, 04:51 PM
alansidman Re: Loop issue 03-03-2014, 05:06 PM
jsmilke Re: Loop issue 03-03-2014, 05:08 PM
alansidman Re: Loop issue 03-03-2014, 05:12 PM
jsmilke Re: Loop issue 03-03-2014, 05:15 PM
alansidman Re: Loop issue 03-03-2014, 05:15 PM
jsmilke Re: Loop issue 03-03-2014, 05:33 PM
Solus Rankin Re: Loop issue 03-03-2014, 05:15 PM
jsmilke Re: Loop issue 03-03-2014, 05:34 PM
alansidman Re: Loop issue 03-03-2014, 05:36 PM
excel_joel Re: Loop issue 03-06-2014, 01:44 AM
  1. #1
    Registered User
    Join Date
    02-10-2014
    Location
    Somewhere
    MS-Off Ver
    Excel 2010
    Posts
    39

    Loop issue

    I'm trying to copy all the cells in a column that contain a text and paste them into a column in another sheet. Here's what I have:

    Sub PasteSO()
    Dim var1 As Integer
    var1 = Application.WorksheetFunction.CountIf(Sheets("D").Range("A1:A300"), "*SO*")
    
    Sheets("Sheet1").Range("A1").Value = "SO"
    For i = 1 To var1
    Sheets("D").Range("A:A").Find(What:="SO").Copy
    Sheets("Sheet1").Columns("A").Find("", Cells(Rows.Count, "A")).PasteSpecial
    Next i
    End Sub
    The problem is that this code pastes the same cell over and over again, whereas I want to find one cell, copy it, paste it, and move on to the next one.

    Any suggestions?

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

    Re: Loop issue

    It looks like you have defined it to only copy cells that contain the value "SO". Is that what you want? Seems rather strange to me. Would you like to clarify or change your ask?
    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
    02-10-2014
    Location
    Somewhere
    MS-Off Ver
    Excel 2010
    Posts
    39

    Re: Loop issue

    You are correct. For any cell in Column A that contains the text "SO", I would like to copy it and paste it on another worksheet.

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

    Re: Loop issue

    Won't you just end up with a column of cells with the values SO. Why not just put SO in A1 and drag it down? Is there something there that you are not telling us that will come back to bite us later in this discussion?

  5. #5
    Registered User
    Join Date
    02-10-2014
    Location
    Somewhere
    MS-Off Ver
    Excel 2010
    Posts
    39

    Re: Loop issue

    There are values after the SO.

    Col A
    SO111
    SO222
    AB555
    SO333

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

    Re: Loop issue

    Here is code to do as you have asked

    Option Explicit
    
    Sub PasteSO()
        Dim w1 As Worksheet
        Dim w2 As Worksheet
        Set w1 = Sheets("D")
        Set w2 = Sheets("Sheet1")
        Dim lr As Long
        lr = w1.Range("A" & Rows.Count).End(xlUp).Row
        Dim lr2 As Long
        Dim l As Long
    
        Application.ScreenUpdating = False
            For l = 1 To lr
            lr2 = w2.Range("A" & Rows.Count).End(xlUp).Row
                If w1.Range("A" & l) like "SO*" Then
                    w1.Range("A" & l).Copy w2.Range("A" & lr2 + 1)
                End If
            Next l
        Application.CutCopyMode = False
        Application.ScreenUpdating = True
        MsgBox ("completed")
    
    End Sub
    Last edited by alansidman; 03-03-2014 at 05:18 PM.

  7. #7
    Registered User
    Join Date
    02-10-2014
    Location
    Somewhere
    MS-Off Ver
    Excel 2010
    Posts
    39

    Re: Loop issue

    alansidman - "Invalid Next Control Variable" is the error message I get with this code.

  8. #8
    Forum Expert Solus Rankin's Avatar
    Join Date
    05-24-2013
    Location
    Hollywood, CA
    MS-Off Ver
    Win7 Office 2010 VS Express 2012
    Posts
    2,655

    Re: Loop issue

    This will find SO on sheet 1 and move it to sheet 2:
    Sub jsmilke()
    Dim l As Long
    
    For l = 1 To Sheets(1).Range("A" & Rows.Count).End(xlUp).Row
        If InStr(1, UCase(Cells(l, 1).Value), "SO") > 0 Then
            Sheets(2).Range("A" & Rows.Count).End(xlUp).Offset(1).Value = Sheets(1).Cells(l, 1).Value
        End If
    Next l
    End Sub
    Thanks,
    Solus


    Please remember the following:

    1. Use [code] code tags [/code]. It keeps posts clean, easy-to-read, and maintains VBA formatting.
    Highlight the code in your post and press the # button in the toolbar.
    2. Show appreciation to those who have helped you by clicking below their posts.
    3. If you are happy with a solution to your problem, mark the thread as [SOLVED] using the tools at the top.

    "Slow is smooth, smooth is fast."

  9. #9
    Registered User
    Join Date
    02-10-2014
    Location
    Somewhere
    MS-Off Ver
    Excel 2010
    Posts
    39

    Re: Loop issue

    Solus - for some reason I am getting no action when I run your code

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

    Re: Loop issue

    Check the code to make sure the next line is Next l (letter L lowercase) In the forum it looks like the number 1.

    Check to make sure you copied the latest code. I made a quick change to it within seconds after posting.

  11. #11
    Registered User
    Join Date
    11-15-2012
    Location
    australia
    MS-Off Ver
    Excel 2012
    Posts
    33

    Re: Loop issue

    awesome work

+ 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. Issue with For...next loop
    By Charles1104 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 01-17-2013, 04:06 PM
  2. Loop issue
    By punter in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-03-2011, 04:23 PM
  3. For...Next Loop issue
    By FK7Design in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 08-20-2010, 10:12 AM
  4. For each loop issue
    By goofy78270 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 10-19-2007, 07:22 PM
  5. Loop issue
    By punter in forum Excel Programming / VBA / Macros
    Replies: 14
    Last Post: 08-07-2007, 07:49 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