basic help please...

PhilKib

New Member
Joined
Aug 16, 2015
Messages
17
I've been struggling with this for sometime and can't find the answer.

I have a range of data in my Reference sheet, which holds specific information on certain members of a club, I then want to loop through each entry starting at A3 and compare it against the Data sheet which holds all information of all members. If the reference file entry is not found on the data sheet I want to miss that out but continue searching through the last of the reference file. So for example the reference file has A3:A20 at A10, for example, that person's name is not found in the datasheet I then want to continue searching through. Please see the code I have so far.
also... if there is a more elegant way of doing this then all ears!! :)

VBA Code:
Sub myReportaa()

Dim dSheet As Worksheet
Dim rptSheet As Worksheet
Dim refSheet As Worksheet
Dim custname As Variant

Set dSheet = ThisWorkbook.Sheets("Data")
Set rptSheet = ThisWorkbook.Sheets("Report")
Set refSheet = ThisWorkbook.Sheets("Reference")
rptLR = rptSheet.Cells(Rows.Count, 1).End(xlUp).Row
rptSheet.Range("a2:g" & rptLR).ClearContents
lastrow = dSheet.Cells(Rows.Count, 1).End(xlUp).Row

y = 2 'starting row

x = 3

  
    custname = refSheet.Cells(x, 1)
   
    Do
     rptSheet.Cells(y, 1) = dSheet.Cells(x, 1)
     rptSheet.Cells(y, 2) = dSheet.Cells(x, 2)
     rptSheet.Cells(y, 3) = dSheet.Cells(x, 4)
     rptSheet.Cells(y, 4) = dSheet.Cells(x, 16)
     rptSheet.Cells(y, 5) = dSheet.Cells(x, 18)
     rptSheet.Cells(y, 6) = dSheet.Cells(x, 19)
     rptSheet.Cells(y, 7) = dSheet.Cells(x, 20)
     y = y + 1
     x = x + 1
     custname = refSheet.Cells(x, 1)
   
    Loop While dSheet.Cells(x, 2) = custname
  
rptSheet.Range ("A1")
End Sub
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Try this (untested) :
VBA Code:
Sub myReportaa()
Dim dSheet As Worksheet, rptSheet As Worksheet
Dim refSheet As Worksheet, cel As Range, custname As Range, y%

Set dSheet = ThisWorkbook.Sheets("Data")
Set rptSheet = ThisWorkbook.Sheets("Report")
Set refSheet = ThisWorkbook.Sheets("Reference")

rptSheet.Cells.ClearContents

y = 2
For Each cel In refSheet.Range("A3:A" & Cells(Rows.Count, "A").End(3).Row)
    Set custname = dSheet.[A:A].Find(cel)
    If Not custname Is Nothing Then
        rptSheet.Cells(y, 1).Resize(, 20) = custname.Resize(, 20).Value
        y = y + 1
    End If
Next
rptSheet.Range("C:C, E:O, Q:Q").Delete
End Sub
 
Upvote 0
Try this (untested) :
VBA Code:
Sub myReportaa()
Dim dSheet As Worksheet, rptSheet As Worksheet
Dim refSheet As Worksheet, cel As Range, custname As Range, y%

Set dSheet = ThisWorkbook.Sheets("Data")
Set rptSheet = ThisWorkbook.Sheets("Report")
Set refSheet = ThisWorkbook.Sheets("Reference")

rptSheet.Cells.ClearContents

y = 2
For Each cel In refSheet.Range("A3:A" & Cells(Rows.Count, "A").End(3).Row)
    Set custname = dSheet.[A:A].Find(cel)
    If Not custname Is Nothing Then
        rptSheet.Cells(y, 1).Resize(, 20) = custname.Resize(, 20).Value
        y = y + 1
    End If
Next
rptSheet.Range("C:C, E:O, Q:Q").Delete
End Sub

Hi FooToo

Thanks for replying this is certainly a lot neater than my attempt!, there is one thing though, I don't get anything in the Report Sheet,
Also, the variable custName doesn't appear to hold any name? or should that happen? also if you could be so kind, what does this line do;

rptSheet.Range("C:C, E:O, Q:Q").Delete (I just want to try to understand your code so that I learn as I go along.

Thanks Phil
 
Upvote 0
Made some changes and added a few notes.
Post again if you need further explanation.
VBA Code:
Sub myReportaa()
Dim dSheet As Worksheet, rptSheet As Worksheet
Dim refSheet As Worksheet, cel As Range, custname As Range, y%

Set dSheet = ThisWorkbook.Sheets("Data")
Set rptSheet = ThisWorkbook.Sheets("Report")
Set refSheet = ThisWorkbook.Sheets("Reference")

rptSheet.Range("A2:G" & Rows.Count).ClearContents

y = 2
'Loop thru names in col A of refSheet
For Each cel In refSheet.Range("A3:A" & refSheet.Cells(Rows.Count, "A").End(3).Row)
    'Find name on dSheet
    Set custname = dSheet.[A:A].Find(cel)
    'If name found
    If Not custname Is Nothing Then
        'Copy required columns from dSheet to rptSheet
        With rptSheet.Cells(y, 1)
            .Resize(, 2) = custname.Resize(, 2).Value
            .Offset(0, 2) = custname.Offset(0, 3).Value
            .Offset(0, 3) = custname.Offset(0, 15).Value
            .Offset(0, 4).Resize(, 3) = custname.Offset(0, 17).Resize(, 3).Value
        End With
        y = y + 1
    End If
Next
End Sub
 
Upvote 0
Here's another macro with another approach for you to consider

VBA Code:
Sub Report_1()
  With Sheets("Report")
    .Cells.ClearContents
    Sheets("Data").Range("A1:T" & Sheets("Data").Range("A" & Rows.Count).End(3).Row).AdvancedFilter 2, _
      Sheets("Reference").Range("A2:A" & Sheets("Reference").Range("A" & Rows.Count).End(3).Row), .Range("A1")
    .Range("C:C, E:O, Q:Q").Delete
  End With
End Sub

The macro assumes that in row 1 of the "Data" sheet, you have the headings.
Dante Amor
ABCDEFGHIJKLMNOPQRST
1HEAD1HEAD2HEAD3HEAD4HEAD5HEAD6HEAD7HEAD8HEAD9HEAD10HEAD11HEAD12HEAD13HEAD14HEAD15HEAD16HEAD17HEAD18HEAD19HEAD20
2A2B2C2D2E2F2G2H2I2J2K2L2M2N2O2P2Q2R2S2T2
3A3B3C3D3E3F3G3H3I3J3K3L3M3N3O3P3Q3R3S3T3
4A4B4C4D4E4F4G4H4I4J4K4L4M4N4O4P4Q4R4S4T4
5A5B5C5D5E5F5G5H5I5J5K5L5M5N5O5P5Q5R5S5T5
6A6B6C6D6E6F6G6H6I6J6K6L6M6N6O6P6Q6R6S6T6
7A7B7C7D7E7F7G7H7I7J7K7L7M7N7O7P7Q7R7S7T7
Data

In the "Reference" sheet in cell A2 you must put the same heading that you have in cell A1 of the "Data" sheet
Dante Amor
A
1
2HEAD1
3A2
4A4
5A51
6A6
7
Reference

Result in "Report" sheet
Dante Amor
ABCDEFG
1HEAD1HEAD2HEAD4HEAD16HEAD18HEAD19HEAD20
2A2B2D2P2R2S2T2
3A4B4D4P4R4S4T4
4A6B6D6P6R6S6T6
Report
 
Last edited:
Upvote 0
The macro assumes that in row 1 of the "Data" sheet, you have the headings.
Based on post #1, the Data sheet data starts on row 3, so some adjustment may be necessary to DanteAmors macro to get the correct headings on the Reference sheet.
(Also, it is assumed that columns after col G on the Data sheet are not used.)
 
Upvote 0
the Data sheet data starts on row 3

You're right. Thanks for watching.
I put the updated macro to start at row 3.

Rich (BB code):
Sub Report_1()
  With Sheets("Report")
    .Cells.ClearContents
    Sheets("Data").Range("A3:T" & Sheets("Data").Range("A" & Rows.Count).End(3).Row).AdvancedFilter 2, _
      Sheets("Reference").Range("A2:A" & Sheets("Reference").Range("A" & Rows.Count).End(3).Row), .Range("A1")
    .Range("C:C, E:O, Q:Q").Delete
  End With
End Sub
 
Upvote 0
Thank you for your help with this one, your time is very much appreciated. I've ran this code and for the first time of execution it runs fine, however, if for any reason the reference file is changed i.e. a name added or subtracted, then the result in the Report sheet is just the header row with no other data shown. I must be doing something 'wrong' but can't figure it out!
 
Upvote 0
if for any reason the reference file is changed i.e. a name added or subtracted,

You can put the sample of the data when the macro doesn't work, use XL2BB tool, look at my signature.
 
Upvote 0
hi, please see below.

workingfilter.xlsm
F
14
Reference


workingfilter.xlsm
A
1JOHN LYNCH
2ELAINE CARLIN & RICKY HARRISON
3CHARLEY GRIFFIN
4CAMERON EGGLESTON & FRASER EGGLESTON
5DIONNE DOLAN
6ANTONIO SANTANIELLO & ELENA TEPELIGA
7ANDREW THOMSON
8SHONA THOMSON & STEVEN WILSON
9KIM MACDONALD
10FRASER EGGLESTON
11CRAIG NICOLL
12Phil Kibble
13CALLUM TAYLOR
14LAUREN JOHNSTONE & CONNOR CARSON
15DANIEL FOX
16JORDAN CUSHNIE
17PAUL MCKAY
18LEANNE PERCIVAL
19ALAN WATSON
20MOHAMED HEGAZI & ALINA SOTIR
21MARIA BRIEN
22KENNETH DIAMOND & ELIZABETH DIAMOND
23CAROLINE SMITH
24PETER POLAND
Reference


workingfilter.xlsm
ABCDEFGHIJKLMNOPQRST
1Internal No.NameAcsValueCurYr AcsCurYr O/S ValueDecOn HoldBDetsEDetsActInsO/S Dil.HCEnqDiaryL/YearBandHome Tel. No.Oth. Tel. No.Work Tel. No.
204020039369IAN DAVIDSON & ALISON DAVIDSON8£10,472.630£0.00NYYYNNNNN2019
304020023376ALBERT BRUCE & PAMELA BRUCE10£8,247.540£0.00NYYYNNNNY2018
404020054701ANGELA MORAN17£5,505.840£0.00NYNNNNNNN2019
504020047936SHAUN WILKIE & CAITLIN NICHOLSON10£5,088.420£0.00NYYYNNNNY2019
604020020605SHIRLEY KYLES & ALAN DRYSDALE6£4,865.640£0.00NYYYNYNNY2015
704020054709WILLIAM MCKIMMIE9£4,248.730£0.00NYNNNNNNN2019
804020050700LYNZI SMITH & JAMES MCCABE7£3,317.210£0.00NYNYNNNNN2019
904020053303LISA POTTER12£3,134.510£0.00NYNNNNNNN2009
1004020054719AMEEN ILAHI6£2,858.570£0.00NYNNNNNNN2019
1104020045524SHONA THOMSON9£2,741.510£0.00NYYYNNNNN2013
1204020009635PAUL CHRISTIE & CHERYL SINCLAIR3£2,480.060£0.00NYYYNNNNY2017
1304020040963NEILL CONNOLLY5£2,404.280£0.00NYYYNNNNY2019
1404020040345STEVEN MOIR & SAMANTHA MCCASH2£2,119.860£0.00NYYYNNNNY2019
1504020032208CAMERON EGGLESTON1£2,041.500£0.00NYNNNNNNY2018
1604020046587MARTIN GALLACHER8£2,009.610£0.00NYNYNYNNN2019
1704020049738RYAN WALLACE & STACY DUNCAN5£1,976.570£0.00NYYYNNNNY2019
1804020054732JOHN LYNCH11£1,930.560£0.00NYNNNNNNN2019
1904020047371ELAINE CARLIN & RICKY HARRISON5£1,533.050£0.00NYYYNNNNY2017
2004020044128CHARLEY GRIFFIN3£1,521.690£0.00NYYYNNNNY2019
2104020041259CAMERON EGGLESTON & FRASER EGGLESTON1£1,404.380£0.00NYYNNNNNY2019
2204020050746DIONNE DOLAN5£1,221.240£0.00NYYYNNNNY2019
2304020052711ANTONIO SANTANIELLO & ELENA TEPELIGA1£1,112.750£0.00NYYYNNNNY2019
2404020054711ANDREW THOMSON2£1,108.220£0.00NYNNNNNNN2019
2504020045527SHONA THOMSON & STEVEN WILSON2£1,054.850£0.00NYYYNNNNN2019
2604020054721KIM MACDONALD2£1,028.260£0.00NYNNNNNNN2019
2704020012695FRASER EGGLESTON2£979.340£0.00NYNNNNNNY2016
2804020036475CRAIG NICOLL4£817.820£0.00NYYYNYNNY2011
2904020017395CALLUM TAYLOR2£732.000£0.00NYYNNNNNY2018
3004020022083LAUREN JOHNSTONE & CONNOR CARSON3£727.570£0.00NYYYNNNNN2017
3104020052511DANIEL FOX1£723.300£0.00NYNYNNNNN2019
Data
 
Upvote 0

Forum statistics

Threads
1,214,971
Messages
6,122,517
Members
449,088
Latest member
RandomExceller01

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