Create VBA code to move a row of data based on a selection in a drop down list

Doc56

New Member
Joined
Oct 26, 2022
Messages
5
Office Version
  1. 2013
Platform
  1. Windows
Hi all,

I have a sheet that contains the master data (Sheet called "Workplan"). I have also created a sheet called "Job Analysis".

In the Job Analysis sheet I have created a drop down list and what I am hoping is to get code that will copy rows of data based on the condition/selection of the drop down list.
The criteria listed in the drop down list are: "Completed", "In-Progress", "On-Hold".

Therefore, as an example: if I select "Completed" from the drop down I want all the rows that have a job status listed as: "Completed" in the Workplan sheet to populate in the Job Analysis sheet

Workplan Sheet:
1666835931589.png


Job Analysis Sheet:
1666836040207.png


The work Plan sheet acts as the master and new rows of data will be added so I am hoping the code will allow for new data to filter through based on the condition in the drop down selection.
Sorry if I am unclear. Also I am not using Microsoft 365 or Excel 2021 so cannot use the new "Filter" function which would be perfect for this scenario.
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Please use the XL2BB add in to provide a sample we can work with. You can't tell from your image what rows/columns any of the data is in. Also, that looks like a table on the Workplan sheet - is it, and what is it called?
 
Upvote 0
Please use the XL2BB add in to provide a sample we can work with. You can't tell from your image what rows/columns any of the data is in. Also, that looks like a table on the Workplan sheet - is it, and what is it called?
Apologies:

Workplan Sheet: - is a table (Table Name: WPLAN)
Construction Jobs.xlsx
ABCDEFGHIJK
5
6DateHOU jobConstn Job codeRPM Job NumberAddressDetailsNumber$AMTNOTESJob StatusDate Completed
7TBCTBA8 Kullaroo Close, Kuranda 4881DemolitionAgreement to be confirmed and signedIn-Progress
825/10/22164790/5A/213 Buchan Street, Kowrowa 4881Replacement of water filters at the water tanks at KowrowaIn-Progress
925/10/22164787/24 Coondoo St, Kuranda 4881Routine servicing and cleaning of office air conditionersIn-Progress
1025/10/22164770/40 Coondoo St, Kuranda 4881Routine servicing and cleaning of office air conditionersPreviously arranged every 6 monthsIn-Progress
1120/10/22164760/40 Coondoo St, Kuranda 4881Executive Office - air conditioner in Leandra's room is not coldIn-Progress
1220/10/221647534 Rob Veivers Drive, Kuranda 4881Fence and retaining wallIn-Progress
1320/10/221647441 Barang Street, KurandaFront fence and driveway crossover for vehicle accessIn-Progress
1420/10/22164732/213 Buchan Street, Kowrowa 4881External tap repairsIn-Progress
1518/10/22164720/40 Coondoo St, Kuranda 4881HWS in Executive office kitchen is leakingIn-Progress
Workplan
Cells with Conditional Formatting
CellConditionCell FormatStop If True
K8:K552Expression=+$J8="Completed"textNO
Cells with Data Validation
CellAllowCriteria
K7ListCompleted, In-Progress, On-Hold, Cancelled, Quote Requested
J7:J15ListCompleted, In-Progress, On-Hold, Cancelled, Quote Requested
 
Upvote 0
Apologies:

Workplan Sheet: - is a table (Table Name: WPLAN)
Construction Jobs.xlsx
ABCDEFGHIJK
5
6DateHOU jobConstn Job codeRPM Job NumberAddressDetailsNumber$AMTNOTESJob StatusDate Completed
7TBCTBA8 Kullaroo Close, Kuranda 4881DemolitionAgreement to be confirmed and signedIn-Progress
825/10/22164790/5A/213 Buchan Street, Kowrowa 4881Replacement of water filters at the water tanks at KowrowaIn-Progress
925/10/22164787/24 Coondoo St, Kuranda 4881Routine servicing and cleaning of office air conditionersIn-Progress
1025/10/22164770/40 Coondoo St, Kuranda 4881Routine servicing and cleaning of office air conditionersPreviously arranged every 6 monthsIn-Progress
1120/10/22164760/40 Coondoo St, Kuranda 4881Executive Office - air conditioner in Leandra's room is not coldIn-Progress
1220/10/221647534 Rob Veivers Drive, Kuranda 4881Fence and retaining wallIn-Progress
1320/10/221647441 Barang Street, KurandaFront fence and driveway crossover for vehicle accessIn-Progress
1420/10/22164732/213 Buchan Street, Kowrowa 4881External tap repairsIn-Progress
1518/10/22164720/40 Coondoo St, Kuranda 4881HWS in Executive office kitchen is leakingIn-Progress
Workplan
Cells with Conditional Formatting
CellConditionCell FormatStop If True
K8:K552Expression=+$J8="Completed"textNO
Cells with Data Validation
CellAllowCriteria
K7ListCompleted, In-Progress, On-Hold, Cancelled, Quote Requested
J7:J15ListCompleted, In-Progress, On-Hold, Cancelled, Quote Requested
Thank you for this. Could you also do the same for the Job Analysis sheet? (Can't tell where that data starts, or which cell has the drop down)
 
Upvote 0
You'll need to make a couple of changes to the code below: change the sheet name of the Job Analysis sheet, and change the cell address of the dropdown that has "Completed" in it.

VBA Code:
Option Explicit
Sub Workplan()
    Dim Ws1 As Worksheet, Ws2 As Worksheet
    Set Ws1 = Worksheets("Workplan")
    Set Ws2 = Worksheets("Sheet2")      '<<< Change this to the actual Job Analysis sheet name
    Dim LRow As Long, Status As String
    
    LRow = Ws2.Cells(Rows.Count, 1).End(3).Row + 1
    Status = Ws2.Range("D4").Value      '<<< Change this to the actual cell with the dropdown
    
    With Ws1.ListObjects("WPLAN").Range
        .AutoFilter 10, Status
        .Offset(1).Resize(.Rows.Count - 1).Copy Sheet2.Range("A" & LRow)
        .AutoFilter 10
    End With

End Sub
 
Upvote 0
My apologies,

The Job Analysis example:
Construction Jobs.xlsx
ABCDEFGHIJK
2Filter Data CriteriaCompleted
3WORK PLAN
4
5DateHOU jobConstn Job codeRPM Job NumberAddressDetailsNumber$AMTNOTESJob StatusDate Completed
6
7
Job Analysis
Cells with Data Validation
CellAllowCriteria
D2List=List!$A$2:$A$5
 
Upvote 0
No need to apologise, and thank you for providing the requested information. Try this:

VBA Code:
Option Explicit
Sub Workplan()
    Dim Ws1 As Worksheet, Ws2 As Worksheet
    Set Ws1 = Worksheets("Workplan")
    Set Ws2 = Worksheets("Job Analysis")
    Dim LRow As Long, Status As String
    
    LRow = Ws2.Cells(Rows.Count, 1).End(3).Row + 1
    Status = Ws2.Range("D2").Value
    
    With Ws1.ListObjects("WPLAN").Range
        .AutoFilter 10, Status
        .Offset(1).Resize(.Rows.Count - 1).Copy Sheet2.Range("A" & LRow)
        .AutoFilter 10
    End With

End Sub
 
Upvote 0
Hi Kevin,
That is brilliant!

Just in addition, is there VBA code to add a way that by selecting the drop down item it replaces the previous selection data rather than putting it at the bottom of the previous selection.

Example: I run the code on the first drop down selection, say "Completed" and it populates that data, then if I want to select another criteria, say "In-Progress" is there a piece of code to add to wipe the previous selection and replace with the new selection criteria.

Again this is greatly appreciated.
 
Upvote 0
No problem

VBA Code:
Option Explicit
Sub Workplan_2()
    Dim Ws1 As Worksheet, Ws2 As Worksheet
    Set Ws1 = Worksheets("Workplan")
    Set Ws2 = Worksheets("Job Analysis")
    Dim LRow As Long, Status As String
    
    LRow = Ws2.Cells.Find("*", , xlFormulas, , 1, 2).Row
    If LRow = 5 Then LRow = 6
    Ws2.Range("A6:K" & LRow).Clear
    
    Status = Ws2.Range("D2").Value
    
    With Ws1.ListObjects("WPLAN").Range
        .AutoFilter 10, Status
        .Offset(1).Resize(.Rows.Count - 1).Copy Sheet2.Range("A6")
        .AutoFilter 10
    End With

End Sub
 
Upvote 0
Solution
Thanks very much Kevin,

It is perfect and much appreciated.

Cheers
Darren
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,324
Members
452,635
Latest member
laura12345

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