Macro needed to empty a cell based on two criteria

Posted on 2011-03-09
Last Modified: 2012-05-11
See attached report.  Each month we need to empty the cell "Grand Total" on whatever row contains the word "Pending" is located on. In the spreadsheet example, there are 7 cells to empty. To make it interesting, the example attached only shows January.  Once February's data is added, then the "Grand Total" column moves one column to the right. to make room for the February data  Same will happen when we add March and so on.  Since I have 100 spreadsheets to remove this pending/Grand Total cell each month, is there a macro that can do this even as the spreadsheet grows?  Thanks for any help!  

Question by:SFrongillo
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
  • +1
LVL 22

Expert Comment

ID: 35085510
Hi SF.

I'm just wondering why you're not using formulas for things.  I realize you're trying to do some dynamic manipulation of the sheets, but if done right (like with named ranges), it will all work without having to deal with all of these other issues.

Do you want to explore that direction?  Otherwise I'll throw out some VBA code to assist with your task, but it will likely be more complex than some good formulas :)

Author Comment

ID: 35085544
Can you give me a little more info on the formula's method? I have over 100 spreadsheets that I need to open and remove the Pending -Grand Total cell for.
LVL 22

Expert Comment

ID: 35085881
How is the data getting into the cell?  When I look, I see that it is already blank.
Are you saying that when you insert a column for February, you'd like the Pending for January to clear, or does the grand total somehow get a new value?
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

LVL 22

Expert Comment

ID: 35085905
Just another thought...what if you pre-created the columns for each month, then hid them all until you needed then?

Expert Comment

ID: 35086517
I would precreate the columns.  The field that you want to clear each time, I would name with a named range.  Then instead of searching for the pending, you could just reference that cell.

Author Comment

ID: 35094988
The sample is a Pivot created from data.  The pivot creates the totals for Open, Closed, Pending, however we want to remove the "Grand Total" from the Pending - since it is not needed (Pending for the month is actually the current total/snap shot for the YTD month).  Yes - the sample is blank to show just what I wanted to wipe out.  When we add Februarys data, the "Grand Total" column adds Jan + Feb data which is fine for the Open/Closed rows, but not for the pending.

Expert Comment

ID: 35095935
The below code will work on the sheet you provided.  It assumes that the word "Pending" will only appear in Col A thru D and that the last col of every row will be the grand total so no how many months you stick inbetween the last col will be the "Grand Total" and rows containg the word "Pending" will have the last column emptied.

Good Luck

Sub Remove_GT()
'Remove Gramd Total
' Count the # of occurances of "Pending" in Column A:D
    tmp1 = Application.WorksheetFunction.CountIf(Range("A:D"), "*Pending*")
    Dim LastCol As Integer
    Dim CurCol As Integer
    Dim CurRow As Integer
    Cells.Find(What:="Pending", After:=ActiveCell, LookIn:=xlValues, _
    LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
    'Start Loop
    For x = 1 To tmp1
      With ActiveSheet
        CurRow = ActiveCell.Row
        CurCol = ActiveCell.Column
        LastCol = .Cells(CurRow, .Columns.Count).End(xlToLeft).Column
        Cells(CurRow, LastCol).Select
        ActiveCell = "JUNK"
        Cells(CurRow, CurCol).Select
      End With
End Sub

Expert Comment

ID: 35095960
oops sorry replace the word JUNK with null value
LVL 22

Accepted Solution

rspahitz earned 500 total points
ID: 35318163
I would suggest 35095935 as the answer.

Also, to author, 35085881 was a good direction to go in.

Featured Post

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

628 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question