Exforsys

Free Training

Finding Values and Copying Corresponding values

This is a discussion on Finding Values and Copying Corresponding values within the Visual Basic Tutorials forums, part of the Articles and Tutorials category; Hi all, I am a newbie to VBA, and any help/advice is extremely welcome. I have a peice of coding ...

Go Back   Exforsys > Articles and Tutorials > Visual Basic Tutorials

Exforsys.com


Visual Basic Tutorials Visual Basic Tutorials Discussions.

Reply

 

LinkBack Thread Tools Search this Thread
  #1 (permalink)  
Old 07-28-2009, 12:29 PM
Junior Member
 
Join Date: Aug 2008
Posts: 2
Clubber is on a distinguished road
Finding Values and Copying Corresponding values

Hi all,

I am a newbie to VBA, and any help/advice is extremely welcome.

I have a peice of coding which searches for certain values in a sheet, copies these values, and pastes them on the next sheet, yet with two problems: -

1. It only copies the values, and not the data in the neighbouring cell, as i require both parts of the data

2. The values i will search for will range from 1 to maybe a thousand, in incraments of 10 (eg 1, 10, 20....), and it seems very time consuming to type all these variants in.

Below is the code: -

Code:
Sub Copy_To_Another_Sheet_1()
    Dim FirstAddress As String
    Dim MyArr As Variant
    Dim Rng As Range
    Dim Rcount As Long
    Dim I As Long
    Dim NewSh As Worksheet

    With Application
        .ScreenUpdating = False
        .EnableEvents = False
    End With

    'Fill in the search Value
    MyArr = Array("10", "20")

    'You can also use more values in the Array
    'myArr = Array("10", "20", "30")

    'Add new worksheet to your workbook to copy to
    'You can also use a existing sheet like this
    'Set NewSh = Worksheets.Add
    Set NewSh = Sheets("Sheet2")

    With Sheets("Sheet1").Range("A:A")

        Rcount = 0

        For I = LBound(MyArr) To UBound(MyArr)

            'If you use LookIn:=xlValues it will also work with a
            'formula cell that evaluates to "@"
            'Note : I use xlPart in this example and not xlWhole
            Set Rng = .Find(What:=MyArr(I), _
                            After:=.Cells(.Cells.Count), _
                            LookIn:=xlFormulas, _
                            LookAt:=xlPart, _
                            SearchOrder:=xlByRows, _
                            SearchDirection:=xlNext, _
                            MatchCase:=False)
            If Not Rng Is Nothing Then
                FirstAddress = Rng.Address
                Do
                    Rcount = Rcount + 1

                    Rng.Copy NewSh.Range("A" & Rcount)

                    ' Use this if you only want to copy the value
                    ' NewSh.Range("A" & Rcount).Value = Rng.Value

                    Set Rng = .FindNext(Rng)
                Loop While Not Rng Is Nothing And Rng.Address <> FirstAddress
            End If
        Next I
    End With

    With Application
        .ScreenUpdating = True
        .EnableEvents = True
    End With
End Sub
Thanks in advance for any help.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads

Thread Thread Starter Forum Replies Last Post
comp.lang.c Answers to Frequently Asked Questions (FAQ List) Steve Summit Tech FAQ 0 06-01-2004 07:00 AM


All times are GMT -4. The time now is 05:13 AM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0
Copyright 2004 - 2009 Exforsys Inc. All rights reserved.