Analyzing A Range Of Cells, Clear Values Immediately Preceeding and Proceeding A Blank Cell

Ark68

Well-known Member
Joined
Mar 23, 2004
Messages
4,564
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I am struggling to find a vba solution for a task I need to do with a range of cells in my worksheet. Part of the problem will be trying to explain.

Consider this sample of data that prior code produces.
wsop 22.0327.xlsm
HIJKLMNOPQ
15VacnacyVacnacyVacnacyHPEHPLHPL
DISPATCH


My goal is to to eliminate any values in the cells except those immediately preceeding and proceeding an empty unshaded cell.
I think what I am aiming for is this ...
wsop 22.0327.xlsm
HIJKLMNOP
16VacnacyHPEHPLHPL
DISPATCH


The values in this range are dynamic, and can be an any combination.



 

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.
as starter, this eliminates the inner grey cells.
No you still have to clear the outer grey cells if the cell aside isn't empty.
Perhaps for you it's easier/more simple, to do everything in the DO ... LOOP WHILE and forget the last part.

VBA Code:
Sub Find_Grey()
     With Range("A15")
          Application.FindFormat.Clear
          Application.FindFormat.Interior.Color = .Interior.Color     'this is the format you'e looking for

          With .EntireRow                                       'the range you're looking in
               Set c = .Find("*", .Cells(1, 1), , , , xlNext, , , True)
               If Not c Is Nothing Then
                    firstAddress = c.Address
                    Set UN = c
                    Do
                         Set UN = Union(UN, c)                  'makes a range with all the cells with that interior color
                         Set c = .Find("", c, , , , xlNext, , , True)
                         DoEvents
                    Loop While Not c Is Nothing And c.Address <> firstAddress
               End If
          End With

          If Not UN Is Nothing Then                             'all those cells
               MsgBox UN.Address
               For Each ar In UN.Areas                          'loop through the contigious ranges of those cells
                    MsgBox ar.Address
                    If ar.Cells.Count > 2 Then MsgBox ar.Offset(, 1).Resize(1, ar.Cells.Count - 2).ClearContents     'clear contents of the middle cells
                    If ar.Column = 1 Then ar(1, 1).ClearContents     'do the same if the cell in the 1st column is that colour
                    If ar(1, ar.Columns.Count).Column = ActiveSheet.Columns.Count Then ar(1, ar.Columns.Count).ClearContents     'the same with the last column
               Next
          End If

          Application.FindFormat.Clear
     End With
End Sub
 
Upvote 0
Following your example of cells H15 to P15, try this:

VBA Code:
Sub ClearValues()
  Dim rng As Range
  Dim ant As Variant
  Dim j As Long
  
  Set rng = Range("H15:P15")
  ant = rng.Cells(1, rng.Columns.Count)
  For j = rng.Columns.Count - 1 To 1 Step -1
    If rng.Cells(1, j).Value = ant Then rng.Cells(1, j).Value = "" Else ant = rng.Cells(1, j)
  Next
End Sub
 
Upvote 0
Thank you both for stepping up to offer your unique solutions. BSALV, I hadn't tested yours simply becuase I opted for Dante's shorter vesion, but it doesn't mean that I don't appreciate the kind contribution.

Dante, thank you. No errors, but you code isn't doing exactly what I am expecting. ("eliminate any values in the cells except those immediately preceeding and proceeding an empty unshaded cell").

In testing, consider this before data range ...
WS 06-Aug-22.xlsx
HIJKLMNOPQ
14VacancyVacancyVacancyVacancy
DS_HPL


After running this code:
Code:
set domRng = .Range("H" & dr_trow & ":Q" & dr_trow)
ant = domRng.Cells(1, domRng.Columns.Count)
For j = domRng.Columns.Count - 1 To 1 Step -1
        If domRng.Cells(1, j).Value = ant Then domRng.Cells(1, j).Value = "" Else ant = domRng.Cells(1, j)
Next

This is the result ...
WS 06-Aug-22.xlsx
HIJKLMNOPQ
14VacancyVacancyVacancy
DS_HPL


The value originally in L14 is missing. As L14 proceeds an empty unshaded cell, it needs to retain the original value of "vacancy".

Thoughts?
 
Upvote 0
Hence my comment. the macro works for your first example. Could you give an example of the possible scenarios, with the original data and the expected results. to better understand your requirement, especially in the cases of the start and end of the cells, since they will not have a previous or subsequent cell to compare against. I need to know what the pattern is or what you want as a result.
 
Upvote 0
VBA Code:
Sub xxxx()
     Dim bLeft, bRight, c As Range
     With Rows(15)
          On Error Resume Next
          Set c = .SpecialCells(xlConstants)                    'cells with content (not empty, not formulas)
          If c Is Nothing Then MsgBox "no cells", vbCritical: Exit Sub
          On Error GoTo 0
          For Each cl In c.Cells                                'loop through this cells
               If cl.Column = 1 Then bLeft = False Else bLeft = (cl.Offset(, -1).Interior.ColorIndex = xlNone And Len(cl.Offset(, -1).Value) = 0)     'the cell at the LHS is empty and no color
               If cl.Column = ActiveSheet.Columns.Count Then bRight = False Else bRight = (cl.Offset(, 1).Interior.ColorIndex = xlNone And Len(cl.Offset(, 1).Value) = 0)
               If Not bLeft And Not bRight Then cl.ClearContents
          Next
     End With
End Sub
 
Upvote 0
Hi Dante, there really is no pattern to predict. The cells in range H to Q could be any combination of blank unshaded cells and cells that are shaded with or without values.
Basically, if an unshaded cell or a series of contigous unshaded cells is encountered, the cell immediately preceeding and proceeding it that is shaded (grey) with or without a value remains unchanged. All other shaded cells that don't border an empty unshaded cell (or contigious cells) remain shaded, but their values are eliminated.

Here are some examples ....
WS 06-Aug-22.xlsx
HIJKLMNOPQ
16HPEHPE
17CULHPLHPEHPE
18CULHPLHPE
19CULHPLCWPCULCWPHPL
20CULCWPHPL
21CULCWPHPLRPEHPL
22HPL
23PLR
24CUL
DS_HPL


BSalv, in my testing so far, your solution seems to be providing the results. More testing will be needed as I don't think I've covered all the different possible combinations that I may not have considered yet. Thank you!
 
Upvote 0
the macro doesn't look to cells with content that is the result of a formula.
So are there formulas in that range ?
 
Upvote 0
Hi Bsalv, it appears I found one exception in which I don't want your code to do it's thing lol.

Consider this original (before your code is encountered)"
WS 06-Aug-22.xlsx
HIJKLMNOPQ
14AUTOAUTO
DS_RPL


Only in the case of columns K and L, I want to keep both "AUTO"s. In any case, if column K holds "AUTO", column L will always also hold the value "AUTO". "AUTO" will only ever populate columns K and L. Columns K and L could be clear with no shading in some cases. There will never be a mix of clear unshaded cells and "AUTO" shaded cells in column K and L.

ie you will never have these:
WS 06-Aug-22.xlsx
HIJKLMNOPQ
18WPLAUTO
19WPLAUTOCUL
DS_RPL


but you could see any of these combinations:
WS 06-Aug-22.xlsx
HIJKLMNOPQ
22WPLAUTOAUTO
23WPLCUL
24WPL
25WPLCUL
DS_RPL


Note, in row 24 of the examples cell K24 is shaded but doesn't have a value to remain revealed.

I thought I might be able to bypass your code with an "if/then" statement if "AUTO" is encountered, but not sure where, if at all, the value of the cell is dtermnined.
Rich (BB code):
With ws_cs.Range("H" & dr_trow & ":Q" & dr_trow) 'Rows(dr_trow)
        On Error Resume Next
        Set c = .SpecialCells(xlConstants)                    
        If c Is Nothing Then MsgBox "no cells", vbCritical: Exit Sub
       On Error GoTo 0
       If c.value <> "AUTO" Then  '<-this doesn't work
               For Each cl In c.Cells                                'loop through this cells
                       If cl.Column = 1 Then bLeft = False Else bLeft = (cl.Offset(, -1).Interior.ColorIndex = xlNone And Len(cl.Offset(, -1).Value) = 0)     
                       If cl.Column = ActiveSheet.Columns.Count Then bRight = False Else bRight = (cl.Offset(, 1).Interior.ColorIndex = xlNone And Len(cl.Offset(, 1).Value) = 0)
                       If Not bLeft And Not bRight Then cl.ClearContents
               Next
       End If
End With
 
Upvote 0

Forum statistics

Threads
1,221,490
Messages
6,160,133
Members
451,622
Latest member
xmrwnx89

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