Attempting to replace cell value

K

kayetter

I am new to excel programming. I am attempting to replace the values i
column b of sheet 1 with a corresponding value from sheet 2. I wan
the program to look at a value in column 1 of sheet 1, then find th
same value in column 1 of sheet 2. Then take the corresponding valu
from column 2 of sheet 2 and bring it into column 2 of sheet 1.

Here is what I have tried so far:

Sub Replace_TwoLists()
Dim iListCount As Integer
Dim iCtr As Integer
Dim hold As String


' Turn off screen updating to speed up macro.
Application.ScreenUpdating = False

' Get count of records to search through.
iListCount = Sheets("sheet1").Range("A1:A10").Rows.Count

' Loop through the "master" list.
For Each x In Sheets("Sheet1").Range("A1:A10")
' Loop through all records in the second list.
For iCtr = 1 To iListCount
' Do comparison of next record.
' To specify a different column, change 1 to the column number.
If x.Value = Sheets("Sheet2").Cells(iCtr, 1).Value Then
hold = Sheets("Sheet2").Cells(iCtr, 2)
Sheets("Sheet1").Cells(iCtr, 2) = hold

' Increment counter to account for next row.
iCtr = iCtr + 1
End If
Next iCtr
Next
Application.ScreenUpdating = True
MsgBox "Done!"
End Su
 
F

Frank Kabel

Hi
you may try the following macro:

Dim target_wks As Worksheet
Dim source_wks As Worksheet
Dim source_rng As Range
Dim row_index As Long
Dim lastrow As Long
Dim ret_value

Set target_wks = ActiveSheet
Set source_wks = Worksheets("sheet2")
Set source_rng = source_wks.Range("A1:B100")
Application.ScreenUpdating = False
lastrow = target_wks.Cells(Rows.count, "A").End(xlUp).row
For row_index = 1 To lastrow
On Error Resume Next
ret_value = Application.WorksheetFunction.VLookup( _
target_wks.Cells(row_index, 1), source_rng, 2, 0)
If Err.Number <> 0 Then
ret_value = ""
End If
On Error GoTo 0
target_wks.Cells(row_index, 2).Value = ret_value
Next
Application.ScreenUpdating = True
End Sub
 
K

kayetter

I changed two lines to:

hold = Sheets("temp").Cells(iCtr, 2)
Sheets("Bill Recipient packages").Cells(x, 20) = hold

That seemed to help but now I am getting Run-time error 1004
Application defined erro
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top