Link to home
Start Free TrialLog in
Avatar of Dale Fye
Dale FyeFlag for United States of America

asked on

Clearing cell if the neighboring cell is blank

I've got a large workbook, that contans a number of worksheets which capture subsets of data from a much larger worksheet and insert rows for subtotals, blank spacer lines, and totals.  The code for doing this works great.

Several of these sheets require that I insert columns between several rows and establish a formula in those columns which computes the difference between the columns on either side of the new column.  I'm using the attached code to do this.

The problem is that this code inserts the formulas from the top cell to the bottom row, and disregards the "spacer" rows, resulting in zeros in those rows, instead of blanks.

In a previous question, I was advised to use the SpecialCells method to clear these values (see the remarked line) but I have been unable to get the syntax right to blank on the cells in the current column (intCol) where the value in the next cell (intCol + 1) is blank.

So, I tried modifying the formula I was putting in the cells, to use the IF() statement to test for a blank neighboring cell, and put a "" in the current cell if the neighbor is blank.  That part of the formula works, but if the neighboring cell contains a value, then the formumla I created actually results in the cell containing the text ('=RC[1]' or '=RC[1] - RC[-1]') showing up in the cell instead of the results of that formula.

What I'm looking for is the answer to how to do both of these techniques properly.


intCol = 5
    intRow = sht.Range("B4000").End(xlUp).Row
    
    While sht.Cells(5, intCol) <> ""
    
        sht.Columns(intCol).Insert Shift:=xlToRight
        sht.Cells(4, intCol) = sht.Cells(4, intCol + 1)
        sht.Cells(4, intCol).NumberFormat = "m/d/yy;@"
        sht.Cells(5, intCol + 1) = "YTD Balance"
        sht.Cells(5, intCol) = "Activity"
        
        If intCol = 5 Then
            sht.Cells(4, 5).Select
            Selection.Font.Bold = True
            Selection.HorizontalAlignment = xlCenter
            sht.Range(Cells(6, 5), Cells(intRow, 5)).FormulaR1C1 = "=RC[1]"
'            sht.Range(Cells(6, 5), Cells(intRow, 5)).FormulaR1C1 = "=if(RC[1]="""", """", ""=RC[1]"")"
        Else
            sht.Range(Cells(6, intCol), Cells(intRow, intCol)).FormulaR1C1 = "=RC[1] - RC[-1]"
'            sht.Range(Cells(6, intCol), Cells(intRow, intCol)).FormulaR1C1 = "=if(RC[1] = """", """", ""=RC[1] - RC[-1]"")"
        End If
        'sht.Range(Cells(6, intCol + 1), Cells(intRow, intCol + 1)).SpecialCells(xlCellTypeBlanks).Offset(, -11).ClearContents
        intCol = intCol + 2
    Wend

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of dlmille
dlmille
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Dale Fye

ASKER

Dave,

The SpecialCells row is exactly what I was looking for, and it is even better since it is outside the loop.

When I use the spreadsheet you posted, everything works great, and the simple formula works great, displaying the actual value of RC[1] or RC[1]-RC[-1], as expected.

But when I rem out those two lines and unrem the second formula line in each part of the IF Then, what gets displayed in the cells is not the value, but the text "=RC[1]"  (without quotes) or "=RC[1] - RC[-1]", so it cannot be a formatting or display issue.

I've made the modifications discussed above and am posting your example back again.
formulaInCols-r1.xlsm
Ahh. Will get back to you after a few hours as I have an upcoming dinner meeting
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks,  Dave.

One of these centuries I'll be as proficient in Excel as I am in Access.  And will still look to guys like you for the tough solutions.