Please tell me why this macro is giving a false outcome

oldbrewer

Well-known Member
Joined
Apr 11, 2010
Messages
11,012
It is probably blindingly obvious but I am stumped - logic is explained below

IDCOLORTEMPStartDateEndDateY/N
123ABCgreen1hot01/01/2017Y
123ABC01/01/2017Y
123ABCred2hot01/01/201731/01/2017Y
123ABChot01/01/2017macro
123ABChot01/01/2017Y
123ABCgreen301/01/201731/01/2017rrow = 39
123ABC01/01/2017Y For k = 1 To 2
123ABChot01/01/2017Y Match = Cells(k + 32, 1)
456DEFgreen4hot08/01/201731/01/2017Y For j = 2 To 26
456DEF08/01/2017Y If Cells(j, 1) = Match Then GoTo 20 Else GoTo 50
456DEFgreen5hot08/01/201731/01/2017Y20 If Cells(j, 2) <> "" And Cells(j, 3) <> "" And Cells(j, 4) <> "" Then GoTo 40 Else GoTo 50
456DEFhot08/01/201740 If Cells(j, 5) <> "" And Cells(j, 6) <> "" Then GoTo 45
456DEFhot08/01/2017Y45 rrow = rrow + 1
456DEFgreen608/01/201731/01/2017 For z = 1 To 7
456DEF08/01/2017Y Cells(rrow, z) = Cells(j, z)
456DEFhot08/01/2017Y Next z
456DEFgreen7hot08/01/2017Y50 Next j
456DEF08/01/2017YNext k
123ABCgreen8hot08/01/201731/01/2017YEnd Sub
123ABChot08/01/2017
123ABChot08/01/2017Y
123ABCgreen908/01/201731/01/2017
123ABC08/01/2017Ythe macro is meant to look for all the 123ABC rows (as defined in A33)
123ABCred1hot08/01/201731/01/2017Ythat have no blanks in columns 2 to 6
123ABC08/01/2017Yrow 26and print them from row 40
col Fand then repeat the search for 456DEF (A34)
I cannot figure out why the very first row (color = green1)
appears in the output table
123ABCrow 33
456DEFI have checked that E2 really is a blank cell by copying another blank cell into it
123ABCgreen1hot01/01/2017Y
123ABCred2hot01/01/201731/01/2017Y
123ABCgreen8hot08/01/201731/01/2017Y
123ABCred1hot08/01/201731/01/2017Y
456DEFgreen4hot08/01/201731/01/2017Y
456DEFgreen5hot08/01/201731/01/2017Y
456DEFgreen7hot08/01/2017Y

<colgroup><col span="3"><col><col><col span="4"><col><col></colgroup><tbody>
</tbody>
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
This line

Code:
40    If Cells(iRow, 5) <> "" And Cells(iRow, 6) <> "" Then GoTo 45

... doesn't do anything. It goes to line 45 irrespective.
 
Upvote 0
How about instead,

Code:
Sub ob()
  Dim iRowOut       As Long
  Dim cell          As Range
  Dim iRow          As Long
  Dim iCol          As Long

  iRowOut = 39

  For Each cell In Range("A33:A34")
    For iRow = 2 To 26
      If Cells(iRow, 1) = cell.Value Then
        If WorksheetFunction.CountA(Rows(iRow).Range("B1:F1")) = 5 Then
          iRowOut = iRowOut + 1
          Rows(iRow).Range("A1:F1").Copy Cells(iRowOut, "A")
        End If
      End If
    Next iRow
  Next cell
End Sub

A​
B​
C​
D​
E​
F​
1​
ID
COLOR
TEMP
StartDate
EndDate
Y/N
2​
123ABC
green1​
hot​
1/1/2017​
Y​
3​
123ABC
1/1/2017​
Y​
4​
123ABC
red2​
hot​
1/1/2017​
31/01/2017​
Y​
5​
123ABC
hot​
1/1/2017​
6​
123ABC
hot​
1/1/2017​
Y​
7​
123ABC
green3​
1/1/2017​
31/01/2017​
8​
123ABC
1/1/2017​
Y​
9​
123ABC
hot​
1/1/2017​
Y​
10​
456DEF
green4​
hot​
8/1/2017​
31/01/2017​
Y​
11​
456DEF
8/1/2017​
Y​
12​
456DEF
green5​
hot​
8/1/2017​
31/01/2017​
Y​
13​
456DEF
hot​
8/1/2017​
14​
456DEF
hot​
8/1/2017​
Y​
15​
456DEF
green6​
8/1/2017​
31/01/2017​
16​
456DEF
8/1/2017​
Y​
17​
456DEF
hot​
8/1/2017​
Y​
18​
456DEF
green7​
hot​
8/1/2017​
Y​
19​
456DEF
8/1/2017​
Y​
20​
123ABC
green8​
hot​
8/1/2017​
31/01/2017​
Y​
21​
123ABC
hot​
8/1/2017​
22​
123ABC
hot​
8/1/2017​
Y​
23​
123ABC
green9​
8/1/2017​
31/01/2017​
24​
123ABC
8/1/2017​
Y​
25​
123ABC
red1​
hot​
8/1/2017​
31/01/2017​
Y​
26​
123ABC
8/1/2017​
Y​
27​
32​
33​
123ABC
34​
456DEF
35​
40​
123ABC
red2​
hot​
1/1/2017​
31/01/2017​
Y​
41​
123ABC
green8​
hot​
8/1/2017​
31/01/2017​
Y​
42​
123ABC
red1​
hot​
8/1/2017​
31/01/2017​
Y​
43​
456DEF
green4​
hot​
8/1/2017​
31/01/2017​
Y​
44​
456DEF
green5​
hot​
8/1/2017​
31/01/2017​
Y​
 
Upvote 0

Forum statistics

Threads
1,215,463
Messages
6,124,963
Members
449,200
Latest member
indiansth

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top