+ Reply to Thread
Results 1 to 3 of 3

Modifying code for copying and pasting between sheets

Hybrid View

  1. #1
    Registered User
    Join Date
    07-15-2008
    Location
    Philadelphia, Pa.
    Posts
    3

    Modifying code for copying and pasting between sheets

    Hi, and welcome to the forum.

    Please take a few moments to review the rules and guidance notes, particularly the requirement to make sure VBA code is placed between code tags, which makes it more readable. I've edited it for you on this occasion.

    The answer to your question is to use the VBA instruction Instr in your code. i.e.

    If Instr(Range("A" & CStr(LSearchRow))) = ";" Then
    The default result for a Range instruction is the Value of the range addresses, hence there's no need for .Value

    You will also speed up your macro if you do away with all the .Select instructions. There's no need for them. Just address the object directly and add the appropriate instruction. So instead of

    Row(blah blah).Select
    Selection.Copy
    
    'Just use
    
    Row(blah blah).Copy

    Hello-

    I am working on a sheet where I want to copy and paste all rows within a column that contain the value ";" into another sheet. This code works if the cell value is equal to ";" alone but not if it's mixed with other characters. (ex: P330;P440). I think this is relatively simple to modify, but I am new to macros and can't figure it out. Here is the code (copied from another source and modified).........


    Sub SearchForString()
    
        Dim LSearchRow As Integer
        Dim LCopyToRow As Integer
    
        On Error GoTo Err_Execute
    
        'Start search in row 2
        LSearchRow = 2
    
        'Start copying data to row 2 in Sheet2 (row counter variable)
        LCopyToRow = 2
    
        While Len(Range("A" & CStr(LSearchRow)).Value) > 0
    
            'If value in column A equals ";", copy entire row to Sheet2
            If Range("A" & CStr(LSearchRow)).Value = ";" Then
    
                'Select row in Sheet1 to copy
                Rows(CStr(LSearchRow) & ":" & CStr(LSearchRow)).Select
                Selection.Copy
    
                'Paste row into Sheet2 in next row
                Sheets("Sheet2").Select
                Rows(CStr(LCopyToRow) & ":" & CStr(LCopyToRow)).Select
                ActiveSheet.Paste
    
                'Move counter to next row
                LCopyToRow = LCopyToRow + 1
    
                'Go back to Sheet1 to continue searching
                Sheets("Sheet1").Select
    
            End If
    
            LSearchRow = LSearchRow + 1
    
        Wend
    
        'Position on cell A3
        Application.CutCopyMode = False
        Range("A3").Select
    
        MsgBox "All matching data has been copied."
    
        Exit Sub
    
    Err_Execute:
        MsgBox "An error occurred."
    
    End Sub

    Any help would be greatly appreciated. Thanks!

    john
    Last edited by Richard Buttrey; 07-15-2008 at 12:57 PM. Reason: Code tags missing.

  2. #2
    Registered User
    Join Date
    07-15-2008
    Location
    Philadelphia, Pa.
    Posts
    3

    still not working

    Thanks a lot for the suggestion. Apologies about the formatting. Seems like it's still not working though after I modified it. It gives me an error message that reads "Compile error: Syntax error." Any ideas what's wrong now? Here is the updated code....

    Sub SearchForString()
    
        Dim LSearchRow As Integer
        Dim LCopyToRow As Integer
    
        On Error GoTo Err_Execute
    
        'Start search in row 2
        LSearchRow = 2
    
        'Start copying data to row 2 in Sheet2 (row counter variable)
        LCopyToRow = 2
    
        While Len(Range("A" & CStr(LSearchRow)).Value) > 0
    
            'If value in column A equals ";", copy entire row to Sheet2
            If Instr(Range("A" & CStr(LSearchRow))) = ";" Then
    
                'Select row in Sheet1 to copy
                Rows(CStr(LSearchRow) & ":" & CStr(LSearchRow)).Copy
    
                'Paste row into Sheet2 in next row
                Sheets("Sheet2").Select
                Rows(CStr(LCopyToRow) & ":" & CStr(LCopyToRow)).Paste
    
                'Move counter to next row
                LCopyToRow = LCopyToRow + 1
    
                'Go back to Sheet1 to continue searching
                Sheets ("Sheet1")
    
            End If
    
            LSearchRow = LSearchRow + 1
    
        Wend
    
        'Position on cell A3
        Application.CutCopyMode = False
        Range ("A3")
    
        MsgBox "All matching data has been copied."
    
        Exit Sub
    
    Err_Execute:
        MsgBox "An error occurred."
    
    End Sub
    Thanks, john

  3. #3
    Registered User
    Join Date
    07-15-2008
    Location
    Philadelphia, Pa.
    Posts
    3

    clarification

    To clarify further, this line comes up highlighted in red.........

    If Instr(Range("A" & CStr(LSearchRow))) = ";" Then

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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