Filter rows by color and apply formulas to visible rows only
I've tried searching for this but have only ended up with a non-functioning Frankenstein sub routine. I need to:
-Filter Column B to Grey Cells.
-In column AB on all visible rows, set formula equal to the values in Column B.
If a row is filtered out, I need it to remain blank.
Bonus question (because I'm a pain in the neck): I need to do some kind of loop to replicate this process in columns AC:BA as well. For example, filter Column C to grey cells, and make all visible cells in AC equal to the corresponding row in Column C.
EDIT: I was also thinking to just do a Control + Find, replace any cell where the background color is no fill, and replace with a blank or 0. I can't get that to work either, however.
Code that I have (currently I'm stuck at having selected the first visible cell in Column AB):
Dim Last_Cell As Range
Set Last_Cell = Range("A3").SpecialCells(xlLastCell)
' [Good ]Filter Column B by Color
Range("$A$3", Last_Cell).AutoFilter Field:=2, Criteria1:=RGB(165,165,_
165), Operator:=xlFilterCellColor
' [Pending ] Set all visible AB cells = same row in B
Range("AB3").Offset(1, 0).Activate
Do Until Selection.EntireRow.Hidden = False
If Selection.EntireRow.Hidden = True Then
ActiveCell.Offset(1, 0).Activate
End If
Loop
excel vba
add a comment |
I've tried searching for this but have only ended up with a non-functioning Frankenstein sub routine. I need to:
-Filter Column B to Grey Cells.
-In column AB on all visible rows, set formula equal to the values in Column B.
If a row is filtered out, I need it to remain blank.
Bonus question (because I'm a pain in the neck): I need to do some kind of loop to replicate this process in columns AC:BA as well. For example, filter Column C to grey cells, and make all visible cells in AC equal to the corresponding row in Column C.
EDIT: I was also thinking to just do a Control + Find, replace any cell where the background color is no fill, and replace with a blank or 0. I can't get that to work either, however.
Code that I have (currently I'm stuck at having selected the first visible cell in Column AB):
Dim Last_Cell As Range
Set Last_Cell = Range("A3").SpecialCells(xlLastCell)
' [Good ]Filter Column B by Color
Range("$A$3", Last_Cell).AutoFilter Field:=2, Criteria1:=RGB(165,165,_
165), Operator:=xlFilterCellColor
' [Pending ] Set all visible AB cells = same row in B
Range("AB3").Offset(1, 0).Activate
Do Until Selection.EntireRow.Hidden = False
If Selection.EntireRow.Hidden = True Then
ActiveCell.Offset(1, 0).Activate
End If
Loop
excel vba
1
UseSpecialCells(xlCellTypeVisible)
to drop a value in a visible cell
– urdearboy
Nov 12 '18 at 19:24
Also, do not worry about the loop yet. You need to take steps to the final goal. If this doesn't work on one column, why bother with the column loop? Just focus on a one column solution, then look into extending the solution to other columns via a loop
– urdearboy
Nov 12 '18 at 19:26
When I tried that, it cleared the contents of the first visible cell in AB. There were only two rows- are more required for that to work? Thank you!
– Aria Dewes
Nov 12 '18 at 19:27
Note that your loop will exit at the first instance of Selection.EntireRow.Hidden = True, so even if it did something, it would do it just once.
– Excelosaurus
Nov 12 '18 at 19:31
That is not VB.NET code. If the question has nothing to do with .NET, you may remove the tag
– WelcomeOverflow
Nov 12 '18 at 20:49
add a comment |
I've tried searching for this but have only ended up with a non-functioning Frankenstein sub routine. I need to:
-Filter Column B to Grey Cells.
-In column AB on all visible rows, set formula equal to the values in Column B.
If a row is filtered out, I need it to remain blank.
Bonus question (because I'm a pain in the neck): I need to do some kind of loop to replicate this process in columns AC:BA as well. For example, filter Column C to grey cells, and make all visible cells in AC equal to the corresponding row in Column C.
EDIT: I was also thinking to just do a Control + Find, replace any cell where the background color is no fill, and replace with a blank or 0. I can't get that to work either, however.
Code that I have (currently I'm stuck at having selected the first visible cell in Column AB):
Dim Last_Cell As Range
Set Last_Cell = Range("A3").SpecialCells(xlLastCell)
' [Good ]Filter Column B by Color
Range("$A$3", Last_Cell).AutoFilter Field:=2, Criteria1:=RGB(165,165,_
165), Operator:=xlFilterCellColor
' [Pending ] Set all visible AB cells = same row in B
Range("AB3").Offset(1, 0).Activate
Do Until Selection.EntireRow.Hidden = False
If Selection.EntireRow.Hidden = True Then
ActiveCell.Offset(1, 0).Activate
End If
Loop
excel vba
I've tried searching for this but have only ended up with a non-functioning Frankenstein sub routine. I need to:
-Filter Column B to Grey Cells.
-In column AB on all visible rows, set formula equal to the values in Column B.
If a row is filtered out, I need it to remain blank.
Bonus question (because I'm a pain in the neck): I need to do some kind of loop to replicate this process in columns AC:BA as well. For example, filter Column C to grey cells, and make all visible cells in AC equal to the corresponding row in Column C.
EDIT: I was also thinking to just do a Control + Find, replace any cell where the background color is no fill, and replace with a blank or 0. I can't get that to work either, however.
Code that I have (currently I'm stuck at having selected the first visible cell in Column AB):
Dim Last_Cell As Range
Set Last_Cell = Range("A3").SpecialCells(xlLastCell)
' [Good ]Filter Column B by Color
Range("$A$3", Last_Cell).AutoFilter Field:=2, Criteria1:=RGB(165,165,_
165), Operator:=xlFilterCellColor
' [Pending ] Set all visible AB cells = same row in B
Range("AB3").Offset(1, 0).Activate
Do Until Selection.EntireRow.Hidden = False
If Selection.EntireRow.Hidden = True Then
ActiveCell.Offset(1, 0).Activate
End If
Loop
excel vba
excel vba
edited Nov 12 '18 at 23:05
Excelosaurus
2,0921715
2,0921715
asked Nov 12 '18 at 19:19
Aria DewesAria Dewes
133
133
1
UseSpecialCells(xlCellTypeVisible)
to drop a value in a visible cell
– urdearboy
Nov 12 '18 at 19:24
Also, do not worry about the loop yet. You need to take steps to the final goal. If this doesn't work on one column, why bother with the column loop? Just focus on a one column solution, then look into extending the solution to other columns via a loop
– urdearboy
Nov 12 '18 at 19:26
When I tried that, it cleared the contents of the first visible cell in AB. There were only two rows- are more required for that to work? Thank you!
– Aria Dewes
Nov 12 '18 at 19:27
Note that your loop will exit at the first instance of Selection.EntireRow.Hidden = True, so even if it did something, it would do it just once.
– Excelosaurus
Nov 12 '18 at 19:31
That is not VB.NET code. If the question has nothing to do with .NET, you may remove the tag
– WelcomeOverflow
Nov 12 '18 at 20:49
add a comment |
1
UseSpecialCells(xlCellTypeVisible)
to drop a value in a visible cell
– urdearboy
Nov 12 '18 at 19:24
Also, do not worry about the loop yet. You need to take steps to the final goal. If this doesn't work on one column, why bother with the column loop? Just focus on a one column solution, then look into extending the solution to other columns via a loop
– urdearboy
Nov 12 '18 at 19:26
When I tried that, it cleared the contents of the first visible cell in AB. There were only two rows- are more required for that to work? Thank you!
– Aria Dewes
Nov 12 '18 at 19:27
Note that your loop will exit at the first instance of Selection.EntireRow.Hidden = True, so even if it did something, it would do it just once.
– Excelosaurus
Nov 12 '18 at 19:31
That is not VB.NET code. If the question has nothing to do with .NET, you may remove the tag
– WelcomeOverflow
Nov 12 '18 at 20:49
1
1
Use
SpecialCells(xlCellTypeVisible)
to drop a value in a visible cell– urdearboy
Nov 12 '18 at 19:24
Use
SpecialCells(xlCellTypeVisible)
to drop a value in a visible cell– urdearboy
Nov 12 '18 at 19:24
Also, do not worry about the loop yet. You need to take steps to the final goal. If this doesn't work on one column, why bother with the column loop? Just focus on a one column solution, then look into extending the solution to other columns via a loop
– urdearboy
Nov 12 '18 at 19:26
Also, do not worry about the loop yet. You need to take steps to the final goal. If this doesn't work on one column, why bother with the column loop? Just focus on a one column solution, then look into extending the solution to other columns via a loop
– urdearboy
Nov 12 '18 at 19:26
When I tried that, it cleared the contents of the first visible cell in AB. There were only two rows- are more required for that to work? Thank you!
– Aria Dewes
Nov 12 '18 at 19:27
When I tried that, it cleared the contents of the first visible cell in AB. There were only two rows- are more required for that to work? Thank you!
– Aria Dewes
Nov 12 '18 at 19:27
Note that your loop will exit at the first instance of Selection.EntireRow.Hidden = True, so even if it did something, it would do it just once.
– Excelosaurus
Nov 12 '18 at 19:31
Note that your loop will exit at the first instance of Selection.EntireRow.Hidden = True, so even if it did something, it would do it just once.
– Excelosaurus
Nov 12 '18 at 19:31
That is not VB.NET code. If the question has nothing to do with .NET, you may remove the tag
– WelcomeOverflow
Nov 12 '18 at 20:49
That is not VB.NET code. If the question has nothing to do with .NET, you may remove the tag
– WelcomeOverflow
Nov 12 '18 at 20:49
add a comment |
1 Answer
1
active
oldest
votes
Here's some code for you to tinker with.
The sub below takes 4 arguments: the whole filter column, the whole formula column, the row number of the filter headers, and the RGB color to look for.
'ASSUMPTION: prngFilterCol is to the left of prngFormulaCol.
Public Sub FilterByColorThenSetFormulas(ByVal prngFilterCol As Excel.Range, ByVal prngFormulaCol As Excel.Range, ByVal plHeaderRow As Long, ByVal plColorRGB As Long)
Dim rngFirstCellInFilterArea As Excel.Range
Dim rngLastCellInFilterArea As Excel.Range
Dim rngFilterTarget As Excel.Range
Dim rngFormulasTarget As Excel.Range
Dim rngVisibleCells As Excel.Range
Dim lColumnsDifference As Long
'Initialization.
Set rngFirstCellInFilterArea = prngFilterCol.Cells(plHeaderRow, 1)
Set rngLastCellInFilterArea = Application.Intersect(rngFirstCellInFilterArea.SpecialCells(xlLastCell).EntireRow, prngFormulaCol)
lColumnsDifference = prngFilterCol.Column - prngFormulaCol.Column
'Remove existing filtering.
prngFilterCol.Worksheet.AutoFilterMode = False
If rngLastCellInFilterArea.Row > plHeaderRow Then
Set rngFilterTarget = prngFilterCol.Worksheet.Range(rngFirstCellInFilterArea, rngLastCellInFilterArea)
'Clear the contents (formulas) in the target column.
'Our assumption (above) is crucial.
Set rngFormulasTarget = rngFilterTarget.Columns(rngFilterTarget.Columns.Count)
rngFormulasTarget.ClearContents
'Filter.
rngFilterTarget.AutoFilter Field:=1, Criteria1:=plColorRGB, Operator:=xlFilterCellColor
'Find the remaining visible cells.
'Note: SpecialCells will fail if there are no visible cells, hence the On Error Resume Next.
Set rngVisibleCells = Nothing
On Error Resume Next
Set rngVisibleCells = rngFilterTarget.Offset(1).Resize(rngFilterTarget.Rows.Count - 1).SpecialCells(XlCellType.xlCellTypeVisible)
On Error GoTo 0
If Not rngVisibleCells Is Nothing Then
'Assign formulas to the visible cells within prngFormulaCol, using the relative notation of FormulaR1C1.
rngFormulasTarget.FormulaR1C1 = "=R[0]C[" & CStr(lColumnsDifference) & "]"
End If
End If
'Cleanup.
prngFilterCol.Worksheet.AutoFilterMode = False
Set rngFormulasTarget = Nothing
Set rngVisibleCells = Nothing
Set rngFilterTarget = Nothing
Set rngLastCellInFilterArea = Nothing
Set rngFirstCellInFilterArea = Nothing
End Sub
You can invoke it as follows:
Public Sub TestFilterByColorThenSetFormulas()
Dim lColIndex As Long
'Example 1.
'Column 2 is B, column 28 is AB.
FilterByColorThenSetFormulas Sheet1.Columns(2), Sheet1.Columns(28), 3, RGB(165, 165, 165)
'Example 2.
'Loop from column B to Z, putting formulas in columns AB to AZ.
For lColIndex = 2 To 26
FilterByColorThenSetFormulas Sheet1.Columns(lColIndex), Sheet1.Columns(lColIndex + 26), 3, RGB(165, 165, 165)
Next
End Sub
I trust you will find your way around the code. Try it out, put some breakpoints, see how it works, and have fun.
Note that the code leaves the target worksheet unfiltered. If you'd like filters to stay, you can re-establish them afterwards, programmatically. I'll leave this as an exercise ;-)
Below is my test worksheet's setup:
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53268728%2ffilter-rows-by-color-and-apply-formulas-to-visible-rows-only%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Here's some code for you to tinker with.
The sub below takes 4 arguments: the whole filter column, the whole formula column, the row number of the filter headers, and the RGB color to look for.
'ASSUMPTION: prngFilterCol is to the left of prngFormulaCol.
Public Sub FilterByColorThenSetFormulas(ByVal prngFilterCol As Excel.Range, ByVal prngFormulaCol As Excel.Range, ByVal plHeaderRow As Long, ByVal plColorRGB As Long)
Dim rngFirstCellInFilterArea As Excel.Range
Dim rngLastCellInFilterArea As Excel.Range
Dim rngFilterTarget As Excel.Range
Dim rngFormulasTarget As Excel.Range
Dim rngVisibleCells As Excel.Range
Dim lColumnsDifference As Long
'Initialization.
Set rngFirstCellInFilterArea = prngFilterCol.Cells(plHeaderRow, 1)
Set rngLastCellInFilterArea = Application.Intersect(rngFirstCellInFilterArea.SpecialCells(xlLastCell).EntireRow, prngFormulaCol)
lColumnsDifference = prngFilterCol.Column - prngFormulaCol.Column
'Remove existing filtering.
prngFilterCol.Worksheet.AutoFilterMode = False
If rngLastCellInFilterArea.Row > plHeaderRow Then
Set rngFilterTarget = prngFilterCol.Worksheet.Range(rngFirstCellInFilterArea, rngLastCellInFilterArea)
'Clear the contents (formulas) in the target column.
'Our assumption (above) is crucial.
Set rngFormulasTarget = rngFilterTarget.Columns(rngFilterTarget.Columns.Count)
rngFormulasTarget.ClearContents
'Filter.
rngFilterTarget.AutoFilter Field:=1, Criteria1:=plColorRGB, Operator:=xlFilterCellColor
'Find the remaining visible cells.
'Note: SpecialCells will fail if there are no visible cells, hence the On Error Resume Next.
Set rngVisibleCells = Nothing
On Error Resume Next
Set rngVisibleCells = rngFilterTarget.Offset(1).Resize(rngFilterTarget.Rows.Count - 1).SpecialCells(XlCellType.xlCellTypeVisible)
On Error GoTo 0
If Not rngVisibleCells Is Nothing Then
'Assign formulas to the visible cells within prngFormulaCol, using the relative notation of FormulaR1C1.
rngFormulasTarget.FormulaR1C1 = "=R[0]C[" & CStr(lColumnsDifference) & "]"
End If
End If
'Cleanup.
prngFilterCol.Worksheet.AutoFilterMode = False
Set rngFormulasTarget = Nothing
Set rngVisibleCells = Nothing
Set rngFilterTarget = Nothing
Set rngLastCellInFilterArea = Nothing
Set rngFirstCellInFilterArea = Nothing
End Sub
You can invoke it as follows:
Public Sub TestFilterByColorThenSetFormulas()
Dim lColIndex As Long
'Example 1.
'Column 2 is B, column 28 is AB.
FilterByColorThenSetFormulas Sheet1.Columns(2), Sheet1.Columns(28), 3, RGB(165, 165, 165)
'Example 2.
'Loop from column B to Z, putting formulas in columns AB to AZ.
For lColIndex = 2 To 26
FilterByColorThenSetFormulas Sheet1.Columns(lColIndex), Sheet1.Columns(lColIndex + 26), 3, RGB(165, 165, 165)
Next
End Sub
I trust you will find your way around the code. Try it out, put some breakpoints, see how it works, and have fun.
Note that the code leaves the target worksheet unfiltered. If you'd like filters to stay, you can re-establish them afterwards, programmatically. I'll leave this as an exercise ;-)
Below is my test worksheet's setup:
add a comment |
Here's some code for you to tinker with.
The sub below takes 4 arguments: the whole filter column, the whole formula column, the row number of the filter headers, and the RGB color to look for.
'ASSUMPTION: prngFilterCol is to the left of prngFormulaCol.
Public Sub FilterByColorThenSetFormulas(ByVal prngFilterCol As Excel.Range, ByVal prngFormulaCol As Excel.Range, ByVal plHeaderRow As Long, ByVal plColorRGB As Long)
Dim rngFirstCellInFilterArea As Excel.Range
Dim rngLastCellInFilterArea As Excel.Range
Dim rngFilterTarget As Excel.Range
Dim rngFormulasTarget As Excel.Range
Dim rngVisibleCells As Excel.Range
Dim lColumnsDifference As Long
'Initialization.
Set rngFirstCellInFilterArea = prngFilterCol.Cells(plHeaderRow, 1)
Set rngLastCellInFilterArea = Application.Intersect(rngFirstCellInFilterArea.SpecialCells(xlLastCell).EntireRow, prngFormulaCol)
lColumnsDifference = prngFilterCol.Column - prngFormulaCol.Column
'Remove existing filtering.
prngFilterCol.Worksheet.AutoFilterMode = False
If rngLastCellInFilterArea.Row > plHeaderRow Then
Set rngFilterTarget = prngFilterCol.Worksheet.Range(rngFirstCellInFilterArea, rngLastCellInFilterArea)
'Clear the contents (formulas) in the target column.
'Our assumption (above) is crucial.
Set rngFormulasTarget = rngFilterTarget.Columns(rngFilterTarget.Columns.Count)
rngFormulasTarget.ClearContents
'Filter.
rngFilterTarget.AutoFilter Field:=1, Criteria1:=plColorRGB, Operator:=xlFilterCellColor
'Find the remaining visible cells.
'Note: SpecialCells will fail if there are no visible cells, hence the On Error Resume Next.
Set rngVisibleCells = Nothing
On Error Resume Next
Set rngVisibleCells = rngFilterTarget.Offset(1).Resize(rngFilterTarget.Rows.Count - 1).SpecialCells(XlCellType.xlCellTypeVisible)
On Error GoTo 0
If Not rngVisibleCells Is Nothing Then
'Assign formulas to the visible cells within prngFormulaCol, using the relative notation of FormulaR1C1.
rngFormulasTarget.FormulaR1C1 = "=R[0]C[" & CStr(lColumnsDifference) & "]"
End If
End If
'Cleanup.
prngFilterCol.Worksheet.AutoFilterMode = False
Set rngFormulasTarget = Nothing
Set rngVisibleCells = Nothing
Set rngFilterTarget = Nothing
Set rngLastCellInFilterArea = Nothing
Set rngFirstCellInFilterArea = Nothing
End Sub
You can invoke it as follows:
Public Sub TestFilterByColorThenSetFormulas()
Dim lColIndex As Long
'Example 1.
'Column 2 is B, column 28 is AB.
FilterByColorThenSetFormulas Sheet1.Columns(2), Sheet1.Columns(28), 3, RGB(165, 165, 165)
'Example 2.
'Loop from column B to Z, putting formulas in columns AB to AZ.
For lColIndex = 2 To 26
FilterByColorThenSetFormulas Sheet1.Columns(lColIndex), Sheet1.Columns(lColIndex + 26), 3, RGB(165, 165, 165)
Next
End Sub
I trust you will find your way around the code. Try it out, put some breakpoints, see how it works, and have fun.
Note that the code leaves the target worksheet unfiltered. If you'd like filters to stay, you can re-establish them afterwards, programmatically. I'll leave this as an exercise ;-)
Below is my test worksheet's setup:
add a comment |
Here's some code for you to tinker with.
The sub below takes 4 arguments: the whole filter column, the whole formula column, the row number of the filter headers, and the RGB color to look for.
'ASSUMPTION: prngFilterCol is to the left of prngFormulaCol.
Public Sub FilterByColorThenSetFormulas(ByVal prngFilterCol As Excel.Range, ByVal prngFormulaCol As Excel.Range, ByVal plHeaderRow As Long, ByVal plColorRGB As Long)
Dim rngFirstCellInFilterArea As Excel.Range
Dim rngLastCellInFilterArea As Excel.Range
Dim rngFilterTarget As Excel.Range
Dim rngFormulasTarget As Excel.Range
Dim rngVisibleCells As Excel.Range
Dim lColumnsDifference As Long
'Initialization.
Set rngFirstCellInFilterArea = prngFilterCol.Cells(plHeaderRow, 1)
Set rngLastCellInFilterArea = Application.Intersect(rngFirstCellInFilterArea.SpecialCells(xlLastCell).EntireRow, prngFormulaCol)
lColumnsDifference = prngFilterCol.Column - prngFormulaCol.Column
'Remove existing filtering.
prngFilterCol.Worksheet.AutoFilterMode = False
If rngLastCellInFilterArea.Row > plHeaderRow Then
Set rngFilterTarget = prngFilterCol.Worksheet.Range(rngFirstCellInFilterArea, rngLastCellInFilterArea)
'Clear the contents (formulas) in the target column.
'Our assumption (above) is crucial.
Set rngFormulasTarget = rngFilterTarget.Columns(rngFilterTarget.Columns.Count)
rngFormulasTarget.ClearContents
'Filter.
rngFilterTarget.AutoFilter Field:=1, Criteria1:=plColorRGB, Operator:=xlFilterCellColor
'Find the remaining visible cells.
'Note: SpecialCells will fail if there are no visible cells, hence the On Error Resume Next.
Set rngVisibleCells = Nothing
On Error Resume Next
Set rngVisibleCells = rngFilterTarget.Offset(1).Resize(rngFilterTarget.Rows.Count - 1).SpecialCells(XlCellType.xlCellTypeVisible)
On Error GoTo 0
If Not rngVisibleCells Is Nothing Then
'Assign formulas to the visible cells within prngFormulaCol, using the relative notation of FormulaR1C1.
rngFormulasTarget.FormulaR1C1 = "=R[0]C[" & CStr(lColumnsDifference) & "]"
End If
End If
'Cleanup.
prngFilterCol.Worksheet.AutoFilterMode = False
Set rngFormulasTarget = Nothing
Set rngVisibleCells = Nothing
Set rngFilterTarget = Nothing
Set rngLastCellInFilterArea = Nothing
Set rngFirstCellInFilterArea = Nothing
End Sub
You can invoke it as follows:
Public Sub TestFilterByColorThenSetFormulas()
Dim lColIndex As Long
'Example 1.
'Column 2 is B, column 28 is AB.
FilterByColorThenSetFormulas Sheet1.Columns(2), Sheet1.Columns(28), 3, RGB(165, 165, 165)
'Example 2.
'Loop from column B to Z, putting formulas in columns AB to AZ.
For lColIndex = 2 To 26
FilterByColorThenSetFormulas Sheet1.Columns(lColIndex), Sheet1.Columns(lColIndex + 26), 3, RGB(165, 165, 165)
Next
End Sub
I trust you will find your way around the code. Try it out, put some breakpoints, see how it works, and have fun.
Note that the code leaves the target worksheet unfiltered. If you'd like filters to stay, you can re-establish them afterwards, programmatically. I'll leave this as an exercise ;-)
Below is my test worksheet's setup:
Here's some code for you to tinker with.
The sub below takes 4 arguments: the whole filter column, the whole formula column, the row number of the filter headers, and the RGB color to look for.
'ASSUMPTION: prngFilterCol is to the left of prngFormulaCol.
Public Sub FilterByColorThenSetFormulas(ByVal prngFilterCol As Excel.Range, ByVal prngFormulaCol As Excel.Range, ByVal plHeaderRow As Long, ByVal plColorRGB As Long)
Dim rngFirstCellInFilterArea As Excel.Range
Dim rngLastCellInFilterArea As Excel.Range
Dim rngFilterTarget As Excel.Range
Dim rngFormulasTarget As Excel.Range
Dim rngVisibleCells As Excel.Range
Dim lColumnsDifference As Long
'Initialization.
Set rngFirstCellInFilterArea = prngFilterCol.Cells(plHeaderRow, 1)
Set rngLastCellInFilterArea = Application.Intersect(rngFirstCellInFilterArea.SpecialCells(xlLastCell).EntireRow, prngFormulaCol)
lColumnsDifference = prngFilterCol.Column - prngFormulaCol.Column
'Remove existing filtering.
prngFilterCol.Worksheet.AutoFilterMode = False
If rngLastCellInFilterArea.Row > plHeaderRow Then
Set rngFilterTarget = prngFilterCol.Worksheet.Range(rngFirstCellInFilterArea, rngLastCellInFilterArea)
'Clear the contents (formulas) in the target column.
'Our assumption (above) is crucial.
Set rngFormulasTarget = rngFilterTarget.Columns(rngFilterTarget.Columns.Count)
rngFormulasTarget.ClearContents
'Filter.
rngFilterTarget.AutoFilter Field:=1, Criteria1:=plColorRGB, Operator:=xlFilterCellColor
'Find the remaining visible cells.
'Note: SpecialCells will fail if there are no visible cells, hence the On Error Resume Next.
Set rngVisibleCells = Nothing
On Error Resume Next
Set rngVisibleCells = rngFilterTarget.Offset(1).Resize(rngFilterTarget.Rows.Count - 1).SpecialCells(XlCellType.xlCellTypeVisible)
On Error GoTo 0
If Not rngVisibleCells Is Nothing Then
'Assign formulas to the visible cells within prngFormulaCol, using the relative notation of FormulaR1C1.
rngFormulasTarget.FormulaR1C1 = "=R[0]C[" & CStr(lColumnsDifference) & "]"
End If
End If
'Cleanup.
prngFilterCol.Worksheet.AutoFilterMode = False
Set rngFormulasTarget = Nothing
Set rngVisibleCells = Nothing
Set rngFilterTarget = Nothing
Set rngLastCellInFilterArea = Nothing
Set rngFirstCellInFilterArea = Nothing
End Sub
You can invoke it as follows:
Public Sub TestFilterByColorThenSetFormulas()
Dim lColIndex As Long
'Example 1.
'Column 2 is B, column 28 is AB.
FilterByColorThenSetFormulas Sheet1.Columns(2), Sheet1.Columns(28), 3, RGB(165, 165, 165)
'Example 2.
'Loop from column B to Z, putting formulas in columns AB to AZ.
For lColIndex = 2 To 26
FilterByColorThenSetFormulas Sheet1.Columns(lColIndex), Sheet1.Columns(lColIndex + 26), 3, RGB(165, 165, 165)
Next
End Sub
I trust you will find your way around the code. Try it out, put some breakpoints, see how it works, and have fun.
Note that the code leaves the target worksheet unfiltered. If you'd like filters to stay, you can re-establish them afterwards, programmatically. I'll leave this as an exercise ;-)
Below is my test worksheet's setup:
edited Nov 12 '18 at 21:05
answered Nov 12 '18 at 20:54
ExcelosaurusExcelosaurus
2,0921715
2,0921715
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53268728%2ffilter-rows-by-color-and-apply-formulas-to-visible-rows-only%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Use
SpecialCells(xlCellTypeVisible)
to drop a value in a visible cell– urdearboy
Nov 12 '18 at 19:24
Also, do not worry about the loop yet. You need to take steps to the final goal. If this doesn't work on one column, why bother with the column loop? Just focus on a one column solution, then look into extending the solution to other columns via a loop
– urdearboy
Nov 12 '18 at 19:26
When I tried that, it cleared the contents of the first visible cell in AB. There were only two rows- are more required for that to work? Thank you!
– Aria Dewes
Nov 12 '18 at 19:27
Note that your loop will exit at the first instance of Selection.EntireRow.Hidden = True, so even if it did something, it would do it just once.
– Excelosaurus
Nov 12 '18 at 19:31
That is not VB.NET code. If the question has nothing to do with .NET, you may remove the tag
– WelcomeOverflow
Nov 12 '18 at 20:49