Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Excel / New Users / December 2007

Tip: Looking for answers? Try searching our database.

If match copy cells in visual basic.

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
thomsonpa - 13 Dec 2007 10:04 GMT
How do I write the visual basic code for the following.
If the value in the cells in column A (sheet 1) matches the value in column
A (sheet 2), copy contents of cells N, O & P, columns in the same row, to
sheet 2 in A,B & C.

Many thanks, in anticipation.
Mike H - 13 Dec 2007 11:16 GMT
Hi,

I didn't quite follow your logic

A (sheet 2), copy contents of cells N, O & P, columns in the same row, to
> sheet 2 in A,B & C.

This would overwrite the value in column A which I assume you don't want to
do so instead this writes to columns B,C & D in sheet 2.

This should work as worksheet code or in a module

Sub copyit()
lastrows1 = Sheets("Sheet1").Range("A65536").End(xlUp).Row
lastrows2 = Sheets("Sheet2").Range("A65536").End(xlUp).Row
Dim MyRangeS1, MyRangeS2 As Range
Set MyRangeS1 = Sheets("Sheet1").Range("A1:A" & lastrows1)
Set MyRangeS2 = Sheets("Sheet2").Range("A1:A" & lastrows2)
For Each c1 In MyRangeS1
   For Each c2 In MyRangeS2  
       If c1 = c2 Then
           c2.Offset(0, 1).Value = c1.Offset(0, 13).Value
           c2.Offset(0, 2).Value = c1.Offset(0, 14).Value
           c2.Offset(0, 3).Value = c1.Offset(0, 15).Value
       End If
   Next
   Next
End Sub

Mike

> How do I write the visual basic code for the following.
> If the value in the cells in column A (sheet 1) matches the value in column
> A (sheet 2), copy contents of cells N, O & P, columns in the same row, to
> sheet 2 in A,B & C.
>
> Many thanks, in anticipation.
thomsonpa - 13 Dec 2007 16:53 GMT
Thank you ever so much Mike, you were right about a and b.  It is no wonder I
can never get my visual basic to work when I make such mistakes. Still, I am
learning all the time from this discussion group.

Your answer worked first time.

> Hi,
>
[quoted text clipped - 33 lines]
> >
> > Many thanks, in anticipation.
Mike H - 13 Dec 2007 17:14 GMT
your welcome and thanks for the feedback

Mike

> Thank you ever so much Mike, you were right about a and b.  It is no wonder I
> can never get my visual basic to work when I make such mistakes. Still, I am
[quoted text clipped - 39 lines]
> > >
> > > Many thanks, in anticipation.
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.