VBA Search a table header row filter & copy used range

Bagsy

Active Member
Joined
Feb 26, 2005
Messages
467
Office Version
  1. 365
Platform
  1. Windows
I hope somebody can help me a bit please.
I am attempting to put some code together to search for a name in a header row of a table, filter that column non blank cells. But it needs to filter the whole table.
I have the code working up to the point where it searches the header rows and finds the name
But I am struggling to get filter and copy to work.
I need the whole table filtered because I am eventually trying to achieve as follows
• Search for a name in a table in Sheets("Collated Data")
• Filter & copy the used range in found column (this is day or ½ day holiday)
• Paste onto an employee holiday allocation sheet
• Come back to the table in Sheets("Collated Data")
• Copy column D used range (these are the holiday dates)
• Paste this on the same holiday allocation sheet
At the moment I cannot get passed the filter and copy stage to work on the rest of the code
Any help is very much appreciated




Code:
Sub Addholidays()
Dim WB As Workbook
Dim CurrentSheet As Worksheet
Set CurrentSheet = ActiveSheet
Dim Sh As Worksheet
Dim Locate As Range
Dim Name As String
'store Name value???
Dim Found As Boolean

Ans = MsgBox("Have you selected the correct employee name", vbYesNo)
If Ans = vbNo Then Exit Sub

Application.ScreenUpdating = False

'On Error GoTo ErrorHandler

Sheets("Planner").Select
    Name = ActiveCell.Value
        Sheets("Collated Data").Visible = True
            Sheets("Collated Data").Select
        Range("D4").Select 'select the first line of data in range D4:BP4
    Found = False ' Set Boolean variable "found" to false.
Do Until IsEmpty(ActiveCell) ' Set Do loop to stop at empty cell.

If ActiveCell.Value = Name Then ' Check active cell for search value.
    Found = True
        Exit Do
    End If
ActiveCell.Offset(0, 1).Select ' Step over 1 column from present location.
Loop
 
If Found = True Then ' Checked for found.

'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx below this line not working yet
Range("Name").AutoFilter.Column , Criteria1:="<>"  'filter the whole table from found name column, non-blank cells. Table range (D4:BP370 including header row)
Range("Name").Copy.UsedRange  'copy all non blank cells in filtered used range below found name
End If
 
Hi Fluff, apologies if this is taking longer than we thought
It would be a full match, the name is there as this sheet is linked to the planner, where the names are entered, it’s just in a different format with the holidays in a column instead of a row.
D4:BP4 cells are linked with offset formula’s =INDEX(Planner!$C$6:$C$69,COLUMN(A1))

 
Upvote 0

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
What are the actual values in those cells?
 
Upvote 0
They are just names Fluff

Planner sheet (Landscape)
On the Planner sheet Column (“C6:C56”) holds the names
Column D the department (“D6:D56”)
Column E the allowance (“E6:E56”)
Column F Days taken (“F6:F56”)
Column G Blank (“G6:G56”)
Columns H4:NI4 are the dates dd/mm format
Columns H5:NI5 are the dates shown ddd format

Then on the planner sheet you enter 1 or ½ depending on holiday taken for that employee for any particular date

I needed the Collated data sheet (Portrait)

The Collated data sheet cells (“E4:BP4”) Names are linked to Planner sheet (“C6:C56”) using =INDEX(Planner!$C$6:$C$69,COLUMN(A1))

Collated data sheet (“D4”) Header is Dates (Typed)
Collated data sheet (“D5:D370”) dates are linked to planner sheet (“H4:NI4”) 366 days 1 for a leap year using
=OFFSET(Planner!$H$4, 0, ROW(337:337)-1)

Holidays taken on collated data sheet are linked to the planner sheet cells ("H6:BP370") using
=IF(OFFSET(Planner!$H$6, 0, ROW(337:337)-1)="","",(OFFSET(Planner!$H$6, 0, ROW(337:337)-1)))

So the collated data sheet shows the data with the names along the top, the dates running down the rows & the holiday taken in the middle against the name & the dates holiday is taken.
This will allow me to filter the rows from the found name column (non blank cells) these will be either a 1 or ½ showing holiday taken for that day.


I can then copy that filtered row to paste xlvalues on the employees personal holiday taken sheet (Days taken column)
Then copy the filtered dates row and copy and paste xlvalues on employees personal holiday taken sheet (Dates taken Column)
This will then show dates holidays taken and whether it was a day or ½ day.
 
Upvote 0
If you add the part in blue
Code:
Sub Bagsy2()
   Dim Fnd As Range
   Dim Nme As String
   
   Nme = ActiveCell.Value
   With Sheets("Collated Data")
      If .AutoFilterMode Then .AutoFilterMode = False
      Set Fnd = .Range("D4:BP4").Find(Nme, , , xlWhole, , , False, , False)
     [COLOR=#0000ff] If Fnd Is Nothing Then
         MsgBox "|" & Nme & "| not found"
      Else
         MsgBox "column is " & Fnd.Column
      End If[/COLOR]
      .Range("D4:BP4").AutoFilter Fnd.Column - 3, "<>"
      .AutoFilter.Range.Offset(1).Columns(Fnd.Column - 3).Copy Sheets("Planner").Range("C2")
      .AutoFilter.Range.Offset(1).Columns(1).Copy Sheets("Planner").Range("B2")
      .AutoFilterMode = False
   End With
End Sub
What does the message box say?
 
Upvote 0
Fluff
It gives the name entered in the cell and then not found
 
Upvote 0
Part of the Collated data sheet
Excel 2010
DEFGHIJKLMNOPQRSTUVWXYZAAABACADAEAFAGAHAIAJAKALAMAN
Dates
01/01/2019
02/01/20191
03/01/20191
04/01/20191
05/01/2019
06/01/2019
07/01/2019
08/01/2019
09/01/2019
10/01/2019
11/01/2019
12/01/2019
13/01/2019
14/01/2019
15/01/2019
16/01/2019
17/01/2019
18/01/2019
19/01/2019

<tbody>
[TD="align: center"]4[/TD]

[TD="align: right"]Maurice Cliffen[/TD]
[TD="align: right"]Katrina Fairman[/TD]
[TD="align: right"]Tom Kimber Smith[/TD]
[TD="align: right"]James Platten[/TD]
[TD="align: right"]Neil Boags[/TD]
[TD="align: right"]Ashley Sewell[/TD]
[TD="align: right"]Nicole Feveryear[/TD]
[TD="align: right"]Lauren Angus[/TD]
[TD="align: right"]Gary Baker[/TD]
[TD="align: right"]Dale Sellers[/TD]
[TD="align: right"]Shirley Robinson[/TD]
[TD="align: right"]Spare 1[/TD]
[TD="align: right"]Spare 2[/TD]
[TD="align: right"]Spare 3[/TD]
[TD="align: right"]Spare 4[/TD]
[TD="align: right"]Ian George[/TD]
[TD="align: right"]Brad Smith[/TD]
[TD="align: right"]Spare 5[/TD]
[TD="align: right"]Spare 6[/TD]
[TD="align: right"]Spare 7[/TD]
[TD="align: right"]Steve Johnson[/TD]
[TD="align: right"]Jack Keyzor[/TD]
[TD="align: right"]James Shuckford[/TD]
[TD="align: right"]Jack Perry[/TD]
[TD="align: right"]Harrison Brown[/TD]
[TD="align: right"]Mark Bessey[/TD]
[TD="align: right"]Michael O'Flanagan[/TD]
[TD="align: right"]Spare 8[/TD]
[TD="align: right"]Spare 9[/TD]
[TD="align: right"]Spare 10[/TD]
[TD="align: right"]Spare 11[/TD]
[TD="align: right"]Spare 12[/TD]
[TD="align: right"]Spare 13[/TD]
[TD="align: right"]Spare 14[/TD]
[TD="align: right"]Robert Muncaster[/TD]
[TD="align: right"]Michael Butler[/TD]

[TD="align: center"]5[/TD]

[TD="align: center"]6[/TD]

[TD="align: center"]7[/TD]

[TD="align: center"]8[/TD]

[TD="align: center"]9[/TD]

[TD="align: center"]10[/TD]

[TD="align: center"]11[/TD]

[TD="align: center"]12[/TD]

[TD="align: center"]13[/TD]

[TD="align: center"]14[/TD]

[TD="align: center"]15[/TD]

[TD="align: center"]16[/TD]

[TD="align: center"]17[/TD]

[TD="align: center"]18[/TD]

[TD="align: center"]19[/TD]

[TD="align: center"]20[/TD]

[TD="align: center"]21[/TD]

[TD="align: center"]22[/TD]

[TD="align: center"]23[/TD]

</tbody>
Collated Data

[TABLE="width: 85%"]
<tbody>[TR]
[TD]Worksheet Formulas[TABLE="width: 100%"]
<tbody>[TR="bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=DAE7F5]#DAE7F5[/URL] "]
[TH]Cell[/TH]
[TH="align: left"]Formula[/TH]
[/TR]
[TR]
[TH="bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=DAE7F5]#DAE7F5[/URL] "]E4[/TH]
[TD="align: left"]=INDEX(Planner!$C$6:$C$69,COLUMN(A1))[/TD]
[/TR]
[TR]
[TH="bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=DAE7F5]#DAE7F5[/URL] "]D5
[/TH]
[TD="align: left"]=OFFSET(Planner!$H$4, 0, ROW(1:1)-1)[/TD]
[/TR]
[TR]
[TH="bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=DAE7F5]#DAE7F5[/URL] "]E5
[/TH]
[TD="align: left"]=IF(OFFSET(Planner!$H$6, 0, ROW(1:1)-1)="","",(OFFSET(Planner!$H$6, 0, ROW(1:1)-1)))[/TD]
[/TR]
</tbody>[/TABLE]
[/TD]
[/TR]
</tbody>[/TABLE]
 
Last edited by a moderator:
Upvote 0
Part of the planner sheet

Excel 2010
CDEFGHIJKLMNOPQRSTUVWXYZAA
401/0102/0103/0104/0105/0106/0107/0108/0109/0110/0111/0112/0113/0114/0115/0116/0117/0118/0119/0120/01
5NameDepartmentAllowanceDays taken/BookedTueWedThuFriSatSunMonTueWedThuFriSatSunMonTueWedThuFriSatSun
6Maurice CliffenGeneral Manager238111
7Katrina FairmanAccounts200
8Tom Kimber SmithSales Executive200
9James PlattenWorks Manager200
10Neil BoagsEngineering Manager200
11Ashley SewellQA Manager200
12Nicole FeveryearPurchasing Administrator200
13Lauren AngusReceptionist200
14Gary BakerSub-Con Manager200
15Dale SellersEstimator200
16Shirley RobinsonOffice Cleaner (Part-time)200
17Spare 10
18Spare 20
19Spare 30
20Spare 40
21Ian GeorgeInspection200
22Brad SmithInspection0
23Spare 50
24Spare 60
25Spare 70
26Steve JohnsonMachinist250
27Jack KeyzorMachinist200
28James ShuckfordMachinist200
29Jack PerryMachinist200
30Harrison BrownMachinist200
31Mark BesseyMachinist200
32Michael O'FlanaganMachinist0
33Spare 80
Planner
 
Upvote 0
How about
Code:
Sub Bagsy2()
   Dim Fnd As Range
   Dim Nme As String
   
   Nme = ActiveCell.Value
   With Sheets("Collated Data")
      If .AutoFilterMode Then .AutoFilterMode = False
      Set Fnd = .Range("D4:BP4").Find(Nme, , xlValues, xlWhole, , , False, , False)
      .Range("D4:BP4").AutoFilter Fnd.Column - 3, "<>"
      .AutoFilter.Range.Offset(1).Columns(Fnd.Column - 3).Copy Sheets("Planner").Range("C2")
      .AutoFilter.Range.Offset(1).Columns(1).Copy Sheets("Planner").Range("B2")
      .AutoFilterMode = False
   End With
End Sub

Also, in future when posting formulae, please limit the number of cells do not use "All Formulas"
 
Upvote 0
Thanks Fluff, I really appreciate this
That has got it, sorry about the formula thing, first time I had posted a sheet, didn’t realise.

Can you tell me what this line of code is doing Why (Fnd.column -3)?
.AutoFilter.Range.Offset(1).Columns(Fnd.Column - 3).Copy Sheets("Planner").Range("C2")


Do you think you would be able to find time to help with this last part
This works great to copy & paste onto the Planner sheet
but what I need to do is search all worksheets in the workbook for the fnd name in Cell (“E15”) (Except sheets “Macros”, “Planner”, “Collated Data”, “Bank Holidays”) select this sheet. This is the employee’s personal record sheet.
Then paste the data into this sheet in column(“F26”) & (“C26”) instead of the planner sheet
The name is always in cell ("E15") which is linked to the planner =Planner!C6 etc.

Bagsy
 
Upvote 0

Forum statistics

Threads
1,223,721
Messages
6,174,096
Members
452,542
Latest member
Bricklin

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