Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.newusers
external usenet poster
 
Posts: 31
Default If match copy cells in visual basic.

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.
  #2   Report Post  
Posted to microsoft.public.excel.newusers
external usenet poster
 
Posts: 11,501
Default If match copy cells in visual basic.

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




"thomsonpa" wrote:

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.

  #3   Report Post  
Posted to microsoft.public.excel.newusers
external usenet poster
 
Posts: 31
Default If match copy cells in visual basic.

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.

"Mike H" wrote:

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




"thomsonpa" wrote:

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.

  #4   Report Post  
Posted to microsoft.public.excel.newusers
external usenet poster
 
Posts: 11,501
Default If match copy cells in visual basic.

your welcome and thanks for the feedback

Mike

"thomsonpa" wrote:

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.

"Mike H" wrote:

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




"thomsonpa" wrote:

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.

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

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
Selecting Cells With Visual Basic or Macro Code RajenRajput1 Excel Discussion (Misc queries) 5 May 11th 07 05:56 PM
Another Visual Basic/Dependant Cells issue ALEX Excel Worksheet Functions 1 February 6th 07 02:09 PM
copy and paste (visual basic) ian123 New Users to Excel 1 June 8th 06 12:22 AM
visual basic (copy and paste) ian123 Excel Worksheet Functions 2 June 7th 06 11:26 PM
changing the visual basic in office 2003 to visual studio net bigdaddy3 Excel Discussion (Misc queries) 1 September 13th 05 10:57 AM


All times are GMT +1. The time now is 02:25 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"