VBA Question

JoeyGaspard

Board Regular
Joined
Jul 22, 2019
Messages
147
Hi all, I am trying to get the below code to work, but I have something wrong. In the sections in Bold, I need it to filter the data, and delete all the entire rows containing the filtered word in "Column C" Please help! Thanks in advance!

Sub Update()


Application.ScreenUpdating = False

Sheets("Import").Select
Range(Range("A:F"), Range("A:F").End(xlDown)).Copy
Worksheets("Inventory").Range("A:F").PasteSpecial Paste:=xlPasteValues




With Sheets("Inventory")
.AutoFilterMode = False
.Range("C1").AutoFilter 3, "USCOLDSTO"
.AutoFilter.Range.Columns("A:F").Offset(1).Copy Sheets("USCOLD").Range("A2")
.AutoFilterMode = False
End With

With Sheets("Inventory")
.AutoFilterMode = False
.Range("C1").AutoFilter 3, "USCOLDSTO"
.AutoFilter.Range.Columns("A:F").Offset(1).Delete
.AutoFilterMode = False
End With

With Sheets("Inventory")
.AutoFilterMode = False
.Range("C1").AutoFilter 3, "Fzr Dock"
.AutoFilter.Range.Columns("A:F").Offset(1).Delete
.AutoFilterMode = False
End With






ThisWorkbook.RefreshAll

Sheets("Inventory").Select

Application.ScreenUpdating = True

End Sub
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
A much better and faster way of doing what you are trying to do is rather than copying all the data and then trying to delete rows ( which is very slow if there are lots of them) is to do all the processing in memory using variant arrays . This code copies all the data from "Import" into memory then runs through it in memory and only copies rows that don't have USCOLDSTO in column Cto a second variant array and then writes the array out toe the inventory sheet.
VBA Code:
Sub test()
Dim outarr()
With Sheets("Import")
lastcol = 6 ' Column F
lastrow = .Cells(Rows.Count, "A").End(xlUp).Row
datar = .Range(.Cells(1, 1), .Cells(lastrow, lastcol))  ' pick up all the data from Import Sheet
End With
ReDim outarr(1 To lastrow, 1 To lastcol)
  ' now copy the rows that don't have USCOLD in column 3
indi = 1 ' set first output row
   For i = 1 To lastrow
    If datar(i, 3) <> "USCOLDSTO" Then
     For j = 1 To lastcol
      outarr(indi, j) = datar(i, j)
     Next j
     indi = indi + 1
    End If
   Next i
 With Sheets("Inventory")
 
 .Range(.Cells(1, 1), .Cells(indi - 1, lastcol)) = outarr
 End With

End Sub
You can add more conditions to the If statement to include other words by using the AND construct
 
Upvote 0
Solution
A much better and faster way of doing what you are trying to do is rather than copying all the data and then trying to delete rows ( which is very slow if there are lots of them) is to do all the processing in memory using variant arrays . This code copies all the data from "Import" into memory then runs through it in memory and only copies rows that don't have USCOLDSTO in column Cto a second variant array and then writes the array out toe the inventory sheet.
VBA Code:
Sub test()
Dim outarr()
With Sheets("Import")
lastcol = 6 ' Column F
lastrow = .Cells(Rows.Count, "A").End(xlUp).Row
datar = .Range(.Cells(1, 1), .Cells(lastrow, lastcol))  ' pick up all the data from Import Sheet
End With
ReDim outarr(1 To lastrow, 1 To lastcol)
  ' now copy the rows that don't have USCOLD in column 3
indi = 1 ' set first output row
   For i = 1 To lastrow
    If datar(i, 3) <> "USCOLDSTO" Then
     For j = 1 To lastcol
      outarr(indi, j) = datar(i, j)
     Next j
     indi = indi + 1
    End If
   Next i
 With Sheets("Inventory")
 
 .Range(.Cells(1, 1), .Cells(indi - 1, lastcol)) = outarr
 End With

End Sub
You can add more conditions to the If statement to include other words by using the AND construct
Thank you so much! I would like to also exclude the lines that have Fzr Dock in Column c, Im not sure I understand what all to use after the AND statement to make that happen? I tried but I am missing something?
 
Upvote 0
A much better and faster way of doing what you are trying to do is rather than copying all the data and then trying to delete rows ( which is very slow if there are lots of them) is to do all the processing in memory using variant arrays . This code copies all the data from "Import" into memory then runs through it in memory and only copies rows that don't have USCOLDSTO in column Cto a second variant array and then writes the array out toe the inventory sheet.
VBA Code:
Sub test()
Dim outarr()
With Sheets("Import")
lastcol = 6 ' Column F
lastrow = .Cells(Rows.Count, "A").End(xlUp).Row
datar = .Range(.Cells(1, 1), .Cells(lastrow, lastcol))  ' pick up all the data from Import Sheet
End With
ReDim outarr(1 To lastrow, 1 To lastcol)
  ' now copy the rows that don't have USCOLD in column 3
indi = 1 ' set first output row
   For i = 1 To lastrow
    If datar(i, 3) <> "USCOLDSTO" Then
     For j = 1 To lastcol
      outarr(indi, j) = datar(i, j)
     Next j
     indi = indi + 1
    End If
   Next i
 With Sheets("Inventory")
 
 .Range(.Cells(1, 1), .Cells(indi - 1, lastcol)) = outarr
 End With

End Sub
You can add more conditions to the If statement to include other words by using the AND construct
Thank you so very much! I figured out the AND construct and it is working beautifully!! I really do appreciate your help!
 
Upvote 0
I figured out the AND construct and it is working beautifully!!
Glad to have helped. It is always better to get to the final solution under your own steam then you really understand what is happening.
 
Upvote 0

Forum statistics

Threads
1,214,819
Messages
6,121,739
Members
449,050
Latest member
excelknuckles

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