VBA code to extract data from sheet if criteria is met

wcf84

New Member
Joined
Aug 24, 2023
Messages
4
Office Version
  1. 365
Platform
  1. Windows
Hi there I am an excel novice and am wanting to create data output to another sheet if criteria is met. I am attempting to create a list of tasks to be undertaken based on the day of the week. I have a cell in sheet 1 which displays the day of the week in along with cells containing job details. If the day of task completion is equal to today I would like an output to a new sheet 2 which copies the entire line of the task details. I hope this makes sense!
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
You probably don't need VBA for this, and if you can provide copies of both your input (source) and output (destination) sheets I'm sure you'll find an answer here. Ideally, provide a copy of your sheets using the XL2BB - Excel Range to BBCode or alternatively, share a copy of your workbook (sensitive data hidden/disguised) via Google Drive, Dropbox or similar file sharing platform.
 
Upvote 0
Here goes!

Here is the source sheet. I am just trying to copy across the information that matches the day so when columnm EDD = TUE and the Day is Tue as in row 1 - I require the entire row which has information with tues in row to a new sheet labelled 'discharges' Essentially creating a list of information relating to client discharges. Then when day rotates I can then go to discharge tab press a created button to run a macro and it will pull all of the WED matches etc.

Tuesday, 29 August 2023Tue
RMNAMESURGEONPHYSICIANOTHERFALLS RISKNFRDIETDESTINATIONEDD
43A
43B
44A GUNNING A. CHUNOLWDHomeTBC
44B
45A SIDESZ. CHOWNOHomeTUE
45B BABUZ. CHOWNOHomeTUE
46AHome
46B
47A
47B SUTHERLANDZ. CHOWRTW@1700HomeTUE
48 MANNF. O'DONOGHUEA. RIDGESNOLWDHomeTBC
49 ELIZABETHI. MEYERNOLWDHomeWED
50 SOMERFIELD A. CHUNOLWDHomeFRI
51 VESETASI. MEYERNODDHomeWED
52 MUNDYM. MAMMENRTW@2000HomeTUE
53 DADGES. BEWSHERV. MALIPATELNOLWDEpworth - CamberwellWBA
54 PATTERSONM. MAMMENRTW@2100HomeTUE
55 TSEM. NIKFARJAMRTW 20000HomeTUE
 
Upvote 0
Thank you for providing a sample using the XL2BB add in. I mentioned that you may not need VBA for this, and with 365 (as per your profile) you have access to the Filter function. With the demonstration below, whatever day (short form) you put in cell E1 on the source sheet, the relevant records will be displayed on the discharge sheet. You may need to adjust the ranges to suit your actual data. You only need to enter the formula once into cell A2 on the discharge sheet. See if this satisfies your needs.

Source sheet:
Book1
ABCDEFGHIJK
1Tuesday, 29 August 2023Tue
2
3RMNAMESURGEONPHYSICIANOTHERFALLS RISKNFRDIETDESTINATIONEDD
443A
543B
644A GUNNING A. CHUNOLWDHomeTBC
744B
845A SIDESZ. CHOWNOHomeTUE
945B BABUZ. CHOWNOHomeTUE
1046AHome
1146B
1247A
1347B SUTHERLANDZ. CHOWRTW@1700HomeTUE
1448 MANNF. O'DONOGHUEA. RIDGESNOLWDHomeTBC
1549 ELIZABETHI. MEYERNOLWDHomeWED
1650 SOMERFIELD A. CHUNOLWDHomeFRI
1751 VESETASI. MEYERNODDHomeWED
1852 MUNDYM. MAMMENRTW@2000HomeTUE
1953 DADGES. BEWSHERV. MALIPATELNOLWDEpworth - CamberwellWBA
2054 PATTERSONM. MAMMENRTW@2100HomeTUE
2155 TSEM. NIKFARJAMRTW 20000HomeTUE
22
source


Discharge sheet:
Book1
ABCDEFGHIJK
1RMNAMESURGEONPHYSICIANOTHERFALLS RISKNFRDIETDESTINATIONEDD
245A0 SIDESZ. CHOW00NO00HomeTUE
345B0 BABUZ. CHOW00NO00HomeTUE
447B0 SUTHERLANDZ. CHOW0RTW@1700000HomeTUE
5520 MUNDYM. MAMMEN0RTW@2000000HomeTUE
6540 PATTERSONM. MAMMEN0RTW@2100000HomeTUE
7550 TSEM. NIKFARJAM0RTW 20000000HomeTUE
8
discharges
Cell Formulas
RangeFormula
A2:K7A2=FILTER(source!$A$4:$K$22,source!K4:K22=source!E1,"")
Dynamic array formulas.
 
Upvote 0
Solution
Plot twist (sorry) - I had this working beautifully at home on office 365. Taken it to work today and its Excel 2016! Is there a work around? :(
 
Upvote 0
The formula alternative for the Filter function can get quite complicated, and not really my area of interest. For a VBA option, please try the following: it is based on your source sheet being exactly as you provided in your sample, with the day (short form) in cell E1. Change the sheet names as appropriate.

VBA Code:
Option Explicit
Option Compare Text
Sub Discharge_Day()
    Dim ws1 As Worksheet, ws2 As Worksheet, s As String
    Dim r As Range, c As Range, Dday As Range
    Set ws1 = Worksheets("source")          '<-- *** Change sheet names to suit ***
    Set ws2 = Worksheets("discharges")
    s = ws1.Range("E1").Value2
    
    For Each c In ws1.Range("K4", ws1.Cells(Rows.Count, "K").End(xlUp))
        If c = s Then
            Set Dday = c.Offset(, -10).Resize(1, 11)
            If r Is Nothing Then Set r = Dday Else Set r = Union(r, Dday)
        End If
    Next c
    If r Is Nothing Then MsgBox "No records meet the criteria": Exit Sub
    ws2.UsedRange.Offset(1).Clear
    r.Copy ws2.Range("A2")
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,847
Messages
6,181,324
Members
453,032
Latest member
Pauh

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