VBA fill right

qvinh

New Member
Joined
Sep 14, 2023
Messages
1
Office Version
  1. 365
Platform
  1. Windows
This code works the issue I have is it takes a very long time for it to execute due to the large data. If I were to do this manually then I would apply my filter criteria then go to column F select the first cell of filtered data then control shift down then drag that selection over the Column G. The data changes daily so one day the filtered data may begin in cell F1590 next day may be F1580 etc. Any advice would be appreciated.
Sub Scrubbing()
Dim LastRow As Long
Dim FilteredDataRange As Range
Dim Cell As Range

' Disable screen updating to improve performance
Application.ScreenUpdating = False

' Find the last row in column A (assuming it contains the data)
LastRow = Cells(Rows.Count, 1).End(xlUp).Row

' Apply the filter criteria to column AB (Field 24)
ActiveSheet.Range("$A$1:$AB$" & LastRow).AutoFilter Field:=24, Criteria1:="=No", Operator:=xlOr, Criteria2:="="

' Apply the filter criteria to column G (Field 7)
ActiveSheet.Range("$A$1:$AB$" & LastRow).AutoFilter Field:=7, Criteria1:=Array( _
"ALL SUPERVISORS", "IN-PLANT SUPPORT", "MAIN DISTRIBUTION TOUR-I", _
"MAIN DISTRIBUTION TOUR-II", "MAIN DISTRIBUTION TOUR-III", _
"MAINTENANCE OPERATIONS"), Operator:=xlFilterValues

' Find the first cell with filtered data in column F (excluding header)
Set FilteredDataRange = Columns("F").SpecialCells(xlCellTypeVisible)

If Not FilteredDataRange Is Nothing Then
' Loop through each visible cell in column F
For Each Cell In FilteredDataRange
' Fill right in column G for the current cell in column F
Cell.Offset(0, 1).Resize(1, Cell.MergeArea.Cells.Count).Value = Cell.Value
Next Cell
End If

' Clear the filters
ActiveSheet.AutoFilterMode = False

' Re-enable screen updating
Application.ScreenUpdating = True
End Sub
[/CODE]
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
This code works the issue I have is it takes a very long time for it to execute due to the large data. If I were to do this manually then I would apply my filter criteria then go to column F select the first cell of filtered data then control shift down then drag that selection over the Column G. The data changes daily so one day the filtered data may begin in cell F1590 next day may be F1580 etc. Any advice would be appreciated.
Sub Scrubbing()
Dim LastRow As Long
Dim FilteredDataRange As Range
Dim Cell As Range

' Disable screen updating to improve performance
Application.ScreenUpdating = False

' Find the last row in column A (assuming it contains the data)
LastRow = Cells(Rows.Count, 1).End(xlUp).Row

' Apply the filter criteria to column AB (Field 24)
ActiveSheet.Range("$A$1:$AB$" & LastRow).AutoFilter Field:=24, Criteria1:="=No", Operator:=xlOr, Criteria2:="="

' Apply the filter criteria to column G (Field 7)
ActiveSheet.Range("$A$1:$AB$" & LastRow).AutoFilter Field:=7, Criteria1:=Array( _
"ALL SUPERVISORS", "IN-PLANT SUPPORT", "MAIN DISTRIBUTION TOUR-I", _
"MAIN DISTRIBUTION TOUR-II", "MAIN DISTRIBUTION TOUR-III", _
"MAINTENANCE OPERATIONS"), Operator:=xlFilterValues

' Find the first cell with filtered data in column F (excluding header)
Set FilteredDataRange = Columns("F").SpecialCells(xlCellTypeVisible)

If Not FilteredDataRange Is Nothing Then
' Loop through each visible cell in column F
For Each Cell In FilteredDataRange
' Fill right in column G for the current cell in column F
Cell.Offset(0, 1).Resize(1, Cell.MergeArea.Cells.Count).Value = Cell.Value
Next Cell
End If

' Clear the filters
ActiveSheet.AutoFilterMode = False

' Re-enable screen updating
Application.ScreenUpdating = True
End Sub
[/CODE]
Can you please use code tags to submit your code. Use the VBA option on the ribbon?

Can you also please submit a good representatitve example of your data using XL2BB?

An explanation of what you are trying to achieve rather than people having to work this out from your code would be appreciated.
 
Upvote 0
Welcome to the MrExcel Message Board!

Cross-posting (posting the same question in more than one forum) is not against our rules, but the method of doing so is covered by #13 of the Forum Rules.

Be sure to follow & read the link at the end of the rule too!

Cross posted at: Excel VBA with fill right
There is no need to repeat the link(s) provided above but if you have posted the question at other places, please provide links to those as well.

If you do cross-post in the future and also provide links, then there shouldn’t be a problem.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,177
Members
453,021
Latest member
Justyna P

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