I am writing my first VBA macro for Excel and struggling a bit at the moment. What I am trying to do is as follows:
I have an old job list (Book1.xls) and a new job list (Book2.xls), I want to update cells in a column on the old spread sheet using the new one. What I have tried to do is write a macro that checks the job numbers on the old job list with those on the new one and when it finds a match pastes a cell from the relevant row on the new sheet to the old one. However if it doesn't find a match for a particular job number on the old job sheet it pastes the new job number onto sheet2 in Book1.xls. I hope that makes sense! This is the macro that I have writen:
Option Explicit
Dim Wbk1 As Object
Dim Wbk2 As Object
Dim i As Integer
Dim j As Integer
Dim k As Integer
Sub Macro1()
'
i = 1
j = 1
k = 1
Set Wbk1 = Workbooks("book1.xls"
.Worksheets("Sheet1"
Set Wbk2 = Workbooks("book2.xls"
.Worksheets("Sheet1"
Do
If Wbk2.Cells(i, 2) = Cells(j, 2) Then
Wbk2.Cells(i, 3).Copy Destination:=Cells(j, 3)
i = i + 1
ElseIf Wbk2.Cells(i, 2) = "" Then Exit Do
i = 1
j = 1
k = 1
ElseIf Wbk1.Cells(j, 2) = "" Then
Wbk2.Cells(i, 3).Copy Destination:=Worksheets("Sheet2"
.Cells(k, 3)
k = k + 1
i = i + 1
Else
j = j + 1
End If
Loop
End Sub
What is happening is that it seems to keep looping and doesn't stop. If anyone could give me some pointers at to where I am going wrong that would be great
Thanks in advance
I have an old job list (Book1.xls) and a new job list (Book2.xls), I want to update cells in a column on the old spread sheet using the new one. What I have tried to do is write a macro that checks the job numbers on the old job list with those on the new one and when it finds a match pastes a cell from the relevant row on the new sheet to the old one. However if it doesn't find a match for a particular job number on the old job sheet it pastes the new job number onto sheet2 in Book1.xls. I hope that makes sense! This is the macro that I have writen:
Option Explicit
Dim Wbk1 As Object
Dim Wbk2 As Object
Dim i As Integer
Dim j As Integer
Dim k As Integer
Sub Macro1()
'
i = 1
j = 1
k = 1
Set Wbk1 = Workbooks("book1.xls"
Set Wbk2 = Workbooks("book2.xls"
Do
If Wbk2.Cells(i, 2) = Cells(j, 2) Then
Wbk2.Cells(i, 3).Copy Destination:=Cells(j, 3)
i = i + 1
ElseIf Wbk2.Cells(i, 2) = "" Then Exit Do
i = 1
j = 1
k = 1
ElseIf Wbk1.Cells(j, 2) = "" Then
Wbk2.Cells(i, 3).Copy Destination:=Worksheets("Sheet2"
k = k + 1
i = i + 1
Else
j = j + 1
End If
Loop
End Sub
What is happening is that it seems to keep looping and doesn't stop. If anyone could give me some pointers at to where I am going wrong that would be great
Thanks in advance