VBA to pull "New" marked data to fixed workbook. Please help. Thanks

cizzett

Board Regular
Joined
Jan 10, 2019
Messages
121
I have been trying to get a VBA code to work for copying all rows that have "New" in the B column then copy that row A-X
and open a different workbook than paste the data to the next empty row.

I have been able to get this to work but it stops after the first instance it identifies.







Public Sub ExporttoDatabase()

Windows("Qlty Workbook Daily1").Activate
Sheets("Today").Select
' Find the last row of data
FinalRow = Worksheets("Today").Cells(Rows.Count, 1).End(xlUp).Row
' Loop through each row
For i = 2 To FinalRow
' Decide if to copy based on column B
ThisValue = Cells(i, 2).Value
If ThisValue = "New" Then
Cells(i, 1).Resize(1, 24).Copy

Workbooks.Open Filename:="I:\Inventory\Quality Reports (Tim)\2019 Qlty Data-Running.xlsm"
Sheets("Data").Select
NextRow = Cells(Rows.Count, 1).End(xlUp).Row + 1
Cells(NextRow, 1).Select
ActiveSheet.Paste

End If
Next

Application.CutCopyMode = False
Windows("Qlty Workbook Daily1").Activate
Range("C3").Select


End Sub
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Hi & welcome to MrExcel.
How about
Code:
Sub ExporttoDatabase()
   Dim Wbk As Workbook
   Dim Mws As Worksheet, Nws As Worksheet
   
   Set Mws = ThisWorkbook.Sheets("Today")
   Set Wbk = Workbooks.Open("I:\Inventory\Quality Reports (Tim)\2019 Qlty Data-Running.xlsm")
   Set Nws = Wbk.Sheets("Data")
   If Mws.AutoFilterMode Then Mws.AutoFilterMode = False
   Mws.Range("A1:X1").AutoFilter 2, "New"
   Mws.AutoFilter.Range.Offset(1).Copy Nws.Range("A" & Rows.Count).End(xlUp).Offset(1)
   Mws.AutoFilterMode = False
   
   [COLOR=#0000ff]Wbk.Close True[/COLOR]
End Sub
This code needs to go in the "Qlty Workbook Daily1" workbook.
It will also save & close the other workbook. If you don't want that, remove the line in blue
 
Upvote 0
Tnks fo the reply

I forgot to mention that the data is in a table and the column B uses data validation in a drop down list.

This code pulls the data over but it also pulls all the conditional formatting and it pulled all the rows, not just the ones displaying "New" in Column B.

I'm assuming because its in table format.
 
Upvote 0
In that case try
Code:
Sub ExporttoDatabase()
   Dim Wbk As Workbook
   Dim Mws As Worksheet, Nws As Worksheet
   
   Set Mws = ThisWorkbook.Sheets("Today")
   Set Wbk = Workbooks.Open("I:\Inventory\Quality Reports (Tim)\2019 Qlty Data-Running.xlsm")
   Set Nws = Wbk.Sheets("Data")
   If Mws.FilterMode Then Mws.ShowAllData
   Mws.Range("A1:j1").AutoFilter 2, "New"
   Mws.UsedRange.Offset(1).SpecialCells(xlVisible).Copy
   Nws.Range("A" & Rows.Count).End(xlUp).Offset(1).PasteSpecial xlPasteValues
   Mws.ShowAllData
   
   Wbk.Close True
End Sub
 
Upvote 0
Seems to be ignoring conditional formatting now but still copying like 60 rows including headers.
 
Upvote 0
If you step through the code using F8, when this line is highlighted
Code:
Mws.UsedRange.Offset(1).SpecialCells(xlVisible).Copy
Look at the sheet you are copying from, has it been filtered correctly?
 
Upvote 0
No it is not, but this column is a data validation drop down selection.

Could that b affcting it as every row has"New" in i techcally een tough its not visibl?
 
Upvote 0
The fact that you have data validation on the column shouldn't make any difference.
Does your table start in A1 with headers in row1 & data starting row2?
 
Upvote 0
Actually I think I managed to get it working, I had to add a macro to remove all formulas from the sheet and leave the values then it only grabs the new cells.

Thanks.
 
Upvote 0
Glad you got it sorted & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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