How to make an Excel cell equal to empty, possibly via VBA code

前端 未结 2 1033
闹比i
闹比i 2021-01-18 16:47

Of course you\'re going to answer this question with the =\"\" formula to be written in the aforementioned cell, but this is not my case.

Actually, I ha

相关标签:
2条回答
  • 2021-01-18 17:27

    This may not fix your problem as I don't have the QuantLibXL, but if you wanted to delete all the cells that evaluated to "" on a sheet, you could try:

    sheet.UsedRange.SpecialCells(xlCellTypeFormulas, 2).Select
    Selection.ClearContents
    

    This will find all formulas that result in text and then delete them. Note: this could fail if there are none, and also may be too aggressive.

    Another trick is to change your formula to something like:

    =if([condition], NA(), [formula])
    

    Now, instead of empty cells, the values will be an error (#N/A). Perhaps the library understands errors instead of values of 0 or "".

    If you wanted to then delete all the errors from your sheet, you would modify the VBA code above to find all the formula's resulting in errors:

    sheet.UsedRange.SpecialCells(xlCellTypeFormulas, 16).Select
    Selection.ClearContents
    

    Again, if there are none, this would error.

    Not the best answer, but perhaps some food for thought.

    0 讨论(0)
  • 2021-01-18 17:35

    Bad news: you can't. Once a cell contains a formula it is never in a truly blank state.

    E.g. set a cell to =A1, and you see that a 0 will appear in that cell; even when A1 is itself blank.

    0 讨论(0)
提交回复
热议问题