简体   繁体   中英

Excel Macro - Search Range of Cell for 0, then return “empty” cell

This is my first time using this forum, and my VBA skill is not very well developed. I hope someone can help.

I have two columns of data, Column A and Column B .

Column A - Returns a sequential "month-year" or 0. If spreadsheet current date (=now()) is less than say Feb, then the cell for February returns 0.

Column B - I want this column to check each cell in Column A. If Column A cell has a date identifier, I want this to be placed in Column B. If Column A has a 0 identifier, I want Column B to return an "empty" cell.

Reason why I am doing this is I am graphing a bar chart. When I trick the program into making an empty cell (x-axis) the graph does not show any data for that month (which is what I want). Trying to make a dynamic graph, but I have no experience in VBA (only C programming =/).

You don't need VBA (or even formulas) to do this. Highlight Column A (the entire column), copy it. Highlight Column B (the entire column, right click, Paste Special, select values and number formats, okay. Highlight Column B again. Press Ctrl+H, Type 0 (or failing that it might be 00/01/1900) in the 'find what', leave the 'replace with' one blank. Tick Match entire cell contents. Click replace all. Done.

Instead of trying for an empty space (which would be impossible, as there's a formula in that cell), use an error condition.
Use this formula, and copy down:

=IF(A1=0,NA(),A1)

This will return the error condition #NA! and Excel will leave the column blank

The other way would be to have a dynamic range for the chart data - I have a chart that will adjust to 5,6 or 7 days, depending on the data returned for the week. The Horizontal (Category) Axis Labels is set to a named range ( ='Sample.xlsx'!LastWeekRange ) and the range checks the data, and returns the appropriate number of cells.
LastWeekRange is defined in Name Manager as =OFFSET(Data!$A$3,0,0,IF(Data!$F$9>0,7,IF(Data!$F$8>0,6,5))) , which returns A3:A7 if there's nothing in F8 , A3:A8 if there is something in F8 but not in F9 , and A3:A9 if there is something in F9 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM