Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Excel VBA > Excel VBA
| Search | Today's Posts | Mark Forums Read
Excel VBA Discuss using VBA for Excel programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Excel VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 23rd, 2010, 01:23 AM
Authorized User
 
Join Date: Jul 2010
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default Copying from worksheet to worksheet

Hello everyone. Im very new into this excel vba programming. I have two sheets namely sheet1 and sheet2 which contains details of items. Sheet 1 contains old data and Sheet2 contains updated data. All items are given a unique itemcode. I would like to copy the updated data of each and every item from sheet2 to sheet1. For example

Sheet1 -
Itemcode Name Status
item0001 Adapter Working
item0002 Monitor Working
item0003 Adapter Repair
item0004 Adapter Working
item0005 Adapter Working

Sheet2 -
Itemcode Name Status
item0001 Adapter Working
item0002 Monitor Repair
item0003 Adapter Scrap

I want to update the status of Items(around 3000 items) in sheet1. I want excel to search for itemcodes in sheet1 that is in sheet2 and update their status. Urgent Please help. This is for my project purpose. Thankyou All.

Last edited by millenniumbuddy; August 23rd, 2010 at 02:31 AM..
 
Old August 23rd, 2010, 03:08 AM
Friend of Wrox
Points: 3,060, Level: 23
Points: 3,060, Level: 23 Points: 3,060, Level: 23 Points: 3,060, Level: 23
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Sep 2005
Location: , , .
Posts: 812
Thanks: 1
Thanked 53 Times in 49 Posts
Default

Hi

Here is a hint:

Code:
Sub Find_Replace_Example()
Dim oCell As Range
Dim oColBMatch As Range
 
For Each oCell In Worksheets(1).Range("A1:A3000")
    
    sText = oCell.Value
    
    If Len(sText) <> 0 Then
    
    
    Set oColBMatch = Worksheets(2).Range("A:A").Find(What:=sText, Lookat:=xlWhole)
    If Not oColBMatch Is Nothing Then
        oCell.Offset(0, 2).Value = Worksheets(2).Cells(oColBMatch.Row, 3).Value
    End If
Next oCell
End Sub
Cheers
Shasur
__________________
C# Code Snippets (http://www.dotnetdud.blogspot.com)

VBA Tips &amp; Tricks (http://www.vbadud.blogspot.com)
 
Old August 23rd, 2010, 07:13 AM
Authorized User
 
Join Date: Jul 2010
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Unhappy Hello...

Thanks a lot Shasur for supporting me. I still have an issue with that code. I dont get any errors but sheet1 still isnt updated the data(status) is as it is.

Sub FindReplace1()
Dim oCell As Range
Dim oColBMatch As Range

For Each oCell In Worksheets(1).Range("A2:A3000")

sText = oCell.Value

If Len(sText) <> 0 Then


Set oColBMatch = Worksheets(2).Range("A2:A3000").Find(What:=sText, Lookat:=xlWhole)
If Not oColBMatch Is Nothing Then
oCell.Offset(0, 2).Value = Worksheets(2).Cells(oColBMatch.Row, 3).Value
End If
End If
Next oCell

End Sub

Sheet1
Itemcode Name Status
item0001 Adapter Working
item0002 Monitor Working
item0003 Adapter Repair
item0004 Adapter Working
item0005 Adapter Working

Sheet2
Itemcode Name Status
item0001 Adapter Working
item0002 Monitor Repair
item0003 Adapter Scrap
 
Old August 23rd, 2010, 08:05 AM
Friend of Wrox
Points: 3,060, Level: 23
Points: 3,060, Level: 23 Points: 3,060, Level: 23 Points: 3,060, Level: 23
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Sep 2005
Location: , , .
Posts: 812
Thanks: 1
Thanked 53 Times in 49 Posts
Default

Hi

The code takes value from Sheet 1 and searches in Sheet 2. If you find something in Sheet 2 it copies it

If you have different order you can try the following

Sub FindReplace1()
Dim oCell As Range
Dim oColBMatch As Range

For Each oCell In Worksheets("Sheet1").Range("A2:A3000")

sText = oCell.Value

If Len(sText) <> 0 Then


Set oColBMatch = Worksheets("Sheet2").Range("A2:A3000").Find(What:= sText, Lookat:=xlPart)
If Not oColBMatch Is Nothing Then
oCell.Offset(0, 2).Value = Worksheets(2).Cells(oColBMatch.Row, 3).Value
End If
End If
Next oCell

End Sub

Cheers
Shasur
__________________
C# Code Snippets (http://www.dotnetdud.blogspot.com)

VBA Tips &amp; Tricks (http://www.vbadud.blogspot.com)
 
Old August 23rd, 2010, 10:01 AM
Authorized User
 
Join Date: Jul 2010
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default Hi

Great! Now that worked. Thanks a lot
 
Old August 24th, 2010, 01:38 AM
Friend of Wrox
Points: 3,060, Level: 23
Points: 3,060, Level: 23 Points: 3,060, Level: 23 Points: 3,060, Level: 23
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Sep 2005
Location: , , .
Posts: 812
Thanks: 1
Thanked 53 Times in 49 Posts
Default

Glad to hear that.

Happy coding :)

Cheers
Shasur
__________________
C# Code Snippets (http://www.dotnetdud.blogspot.com)

VBA Tips &amp; Tricks (http://www.vbadud.blogspot.com)




Similar Threads
Thread Thread Starter Forum Replies Last Post
copying input data to another worksheet with code kwik10z Excel VBA 3 November 2nd, 2007 01:45 PM
Copying Rows to New worksheet eusanpe Excel VBA 7 December 21st, 2006 10:23 AM
finding a worksheet ozPATT Excel VBA 4 November 16th, 2005 10:54 AM
Email Worksheet alannoble26 Excel VBA 2 November 7th, 2005 10:33 AM
Send Worksheet alannoble26 Excel VBA 3 November 2nd, 2005 01:04 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.