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

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
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
4DatesMaurice CliffenKatrina FairmanTom Kimber SmithJames PlattenNeil BoagsAshley SewellNicole FeveryearLauren AngusGary BakerDale SellersShirley RobinsonSpare 1Spare 2Spare 3Spare 4Ian GeorgeBrad SmithSpare 5Spare 6Spare 7Steve JohnsonJack KeyzorJames ShuckfordJack PerryHarrison BrownMark BesseyMichael O'FlanaganSpare 8Spare 9Spare 10Spare 11Spare 12Spare 13Spare 14Robert MuncasterMichael Butler
501/01/2019
602/01/20191
703/01/20191
804/01/20191
905/01/2019
1006/01/2019
1107/01/2019
1208/01/2019
1309/01/2019
1410/01/2019
1511/01/2019
1612/01/2019
1713/01/2019
1814/01/2019
1915/01/2019
2016/01/2019
2117/01/2019
2218/01/2019
2319/01/2019

<tbody>
</tbody>
Collated Data

Worksheet Formulas
CellFormula
E4=INDEX(Planner!$C$6:$C$69,COLUMN(A1))
D5
=OFFSET(Planner!$H$4, 0, ROW(1:1)-1)
E5
=IF(OFFSET(Planner!$H$6, 0, ROW(1:1)-1)="","",(OFFSET(Planner!$H$6, 0, ROW(1:1)-1)))

<tbody>
</tbody>

<tbody>
</tbody>
 
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,215,731
Messages
6,126,535
Members
449,316
Latest member
sravya

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