Hello. Appreciate if someone can help me. I have one sheet that contains rows of data as below:
Another workbook with 3 tabs name W1, W2 & W3 which has some historical data.
The flow:
1. Select from source workbook data rows with column E (validity) values = "Y" only which are rows A2:E2 and A6:E6
2. Copy these row values
3. Paste accordingly to the tabs name in the destination workbook following the column names. IF cell name is W1, paste W1 row data into W1 sheet tab.
4. Repeat this process for other source workbooks.
The final product example for tab W1 as follows:
Tab W2 data is not available since does not meet criteria of validity = "Y"
W3 tab final product:
To get the ball rolling, the not so complete vba code as follows:
Thank you so much for your attention & appreciate any help.
Book1 | |||||||
---|---|---|---|---|---|---|---|
A | B | C | D | E | |||
1 | NAME | date | amount | remarks | validity | ||
2 | W1 | 1/8/2021 | 10 | good | Y | ||
3 | |||||||
4 | W2 | 2/8/2021 | 20 | good | N | ||
5 | |||||||
6 | W3 | 5/8/2021 | 30 | bad | Y | ||
Sheet1 |
Another workbook with 3 tabs name W1, W2 & W3 which has some historical data.
Book2 | |||||||
---|---|---|---|---|---|---|---|
A | B | C | D | E | |||
1 | NAME | date | amount | remarks | validity | ||
2 | W1 | 1/5/2021 | 20 | good | Y | ||
3 | W1 | 11/6/2021 | 30 | good | Y | ||
W1 |
Book2 | |||||||
---|---|---|---|---|---|---|---|
A | B | C | D | E | |||
1 | NAME | date | amount | remarks | validity | ||
2 | W2 | 3/2/2021 | 45 | good | Y | ||
3 | W2 | 21/5/2021 | 80 | good | Y | ||
W2 |
Book2 | |||||||
---|---|---|---|---|---|---|---|
A | B | C | D | E | |||
1 | NAME | date | amount | remarks | validity | ||
2 | W3 | 1/1/2021 | 34 | good | Y | ||
3 | W3 | 17/3/2021 | 100 | good | Y | ||
W3 |
The flow:
1. Select from source workbook data rows with column E (validity) values = "Y" only which are rows A2:E2 and A6:E6
2. Copy these row values
3. Paste accordingly to the tabs name in the destination workbook following the column names. IF cell name is W1, paste W1 row data into W1 sheet tab.
4. Repeat this process for other source workbooks.
The final product example for tab W1 as follows:
Book2.xlsm | |||||||
---|---|---|---|---|---|---|---|
A | B | C | D | E | |||
1 | NAME | date | amount | remarks | validity | ||
2 | W1 | 1/5/2021 | 20 | good | Y | ||
3 | W1 | 11/6/2021 | 30 | good | Y | ||
4 | W1 | 1/8/2021 | 10 | good | Y | ||
W1 |
Tab W2 data is not available since does not meet criteria of validity = "Y"
W3 tab final product:
Book2.xlsm | |||||||
---|---|---|---|---|---|---|---|
A | B | C | D | E | |||
1 | NAME | date | amount | remarks | validity | ||
2 | W3 | 1/1/2021 | 34 | good | Y | ||
3 | W3 | 17/3/2021 | 100 | good | Y | ||
4 | W3 | 5/8/2021 | 30 | bad | Y | ||
W3 |
To get the ball rolling, the not so complete vba code as follows:
VBA Code:
Private Sub CommandButton1_Click()
Application.AskToUpdateLinks = False
Application.DisplayAlerts = False
'Select directory
Dim path As String
Call Application.FileDialog(msoFileDialogOpen).Filters.Clear
Set Y = ActiveWorkbook
With Application.FileDialog(msoFileDialogOpen)
.Title = "Select Source File"
.Show
If .SelectedItems.Count = 1 Then
path1 = .SelectedItems(1)
Else:
GoTo Finish:
End If
End With
Set Z = Workbooks.Open(path1)
Application.CutCopyMode = False
Set SCON = Y.Sheets("control")
SCON.Cells(6, 5) = path1
'Extract data
Z.Activate
ri = 1 'first row in source workbook
rf = Z.Cells(Rows.Count).End(xlUp).Row 'last row in source workbook
'rf = 6
rout = Y.Sheets("W1")(Rows.Count).End(xlUp).Row ' starting row in destination workbook for each well sheets
For r = ri To rf
to_copy = Z.Sheets("Sheet1").Columns(5) 'column E (validity)
If to_copy = "Y" Then
Y.Sheets("W1").Cells(rout, 1) = Z.Sheets("Sheet1").Cells(r, 1) 'Name
Y.Sheets("W1").Cells(rout, 2) = Z.Sheets("Sheet1").Cells(r, 2) 'Date
Y.Sheets("W1").Cells(rout, 3) = Z.Sheets("Sheet1").Cells(r, 3) 'Amount
Y.Sheets("W1").Cells(rout, 4) = Z.Sheets("Sheet1").Cells(r, 4) 'remarks
Y.Sheets("W1").Cells(rout, 5) = Z.Sheets("Sheet1").Cells(r, 5) 'validity
End If
rout = rout + 1
'End If
Next r
Z.Close
MsgBox "Completed!"
Finish:
End Sub
Thank you so much for your attention & appreciate any help.