PDA

View Full Version : VBA IF nesting and loop



froggy76
10-07-2014, 11:17 AM
Dear All ,

I am trying to correct a VBA coding but it does not work as need :

If column X in excel 1 is not 00.00.00 , then vlookup data from sheet 1 with data in sheet based on following criteria :
if value in column 1,3,7,14,28,51 in sheet 1 = value in column 1,3,7,14,28,51 , ignore the lines
but if one of the criteria differ from the second sheet , the line should be kept . The code :
Sub Check()
Dim Sheet1 As Worksheet
Dim Sheet2 As Worksheet
Set Sheet2 = Worksheets("xxx")
Dim Sheet3 As Worksheet
Set Sheet3 = Worksheets("xxxx")
Workbooks.Open Filename:="xxx"
Set Sheet1 = Workbooks("x").Worksheets(xx"Sheet1")
Sheet3.Activate
Dim off
off = 2
Dim off2
off2 = 3
Dim off3
off3 = 0
Dim Found

Found = 0
Sheet3.Range("Ax:AYxxx").Clear

Do Until Sheet1.Cells(off, 1) = ""
If (Sheet1.Cells(off, 28) <> "00:00:00") Then
Do Until (Sheet2.Cells(off2, 1) = "" Or Found = 1)
If (Sheet1.Cells(off, 1).Value = Sheet2.Cells(off2, 1).Value _
And Sheet1.Cells(off, 3).Value = Sheet2.Cells(off2, 3).Value _
And Sheet1.Cells(off, 7).Value = Sheet2.Cells(off2, 7).Value _
And Sheet1.Cells(off, 14).Value = Sheet2.Cells(off2, 14).Value _
And Sheet1.Cells(off, 43).Value = Sheet2.Cells(off2, 43).Value _
And Sheet1.Cells(off, 51).Value = Sheet2.Cells(off2, 51).Value _
And Sheet1.Cells(off, 28).Value=Sheet2.Cells(off2, 28).Value ) Then
Found = 1
End If
off2 = off2 + 1
Loop
If (Found = 0) Then
off3 = off3 + 1
For j = 1 To 51
Sheet3.Cells(off3, j) = Sheet1.Cells(off, j).Value
Next j
End If
End If
off = off + 1
off2 = 3
Found = 0
Loop
Workbooks("xxx").Close SaveChanges:=False
End Sub

Thank you for your help

Bob Phillips
10-07-2014, 01:41 PM
You say column X, but test column AB. You say test for 00.00.00 but the code tests for 00:00:00. So what is not working?

froggy76
10-08-2014, 11:47 AM
Hi ,
Sorry . Yes I am testing for 00:00:00 and the column is AB.
As of today the coding is not working . Basically , I want the following : If column AB in sheet 1 is not 00:00:00 then compare lines from sheet 1 to sheet 2 . Identical lines ( in sheet 1 and sheet 2 ) should be excluded but the macro should only show the lines were the cell 1,3,7,14,43 in sheet 1 is equal to cell 1,3,7,14,43,51 but were the cell 28 is different from sheet 1 to sheet 2 .
Right now the macro is showing all lines from sheet 2 .
Thanks