Jump to content
CC_Mu

How to replace all blank cells to zero

Recommended Posts

CC_Mu

Hey All,

 

I have an excel worksheet, about A1:BD1500, and trying to replace all blank cells to zero, but following code doesn't work. What did i do wrong?

Local $oRange = $oWorkbook.ActiveSheet.UsedRange.SpecialCells($xlCellTypeBlanks)
_Excel_RangeReplace($oWorkbook, $oWorkbook.ActiveSheet, $oRange, "", "0")
ConsoleWrite($oRange & @CRLF)

the console writes nothing about $oRange, which I believe the first line code doesn't find the cells. Any ideas? Thank you for your help!

Share this post


Link to post
Share on other sites
water

A range is an object that can not be displayed using ConsoleWrite.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-01-27 - Version 1.3.3.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites
water

What is the value of @error and @extended after you called _Excel_RangeReplace?


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-01-27 - Version 1.3.3.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites
CC_Mu
12 minutes ago, water said:

What is the value of @error and @extended after you called _Excel_RangeReplace?

it actually has no error, its says successfully replace. but actually nothing is replaced for those blank cells.

and this is my error code

If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Excel UDF: _Excel_RangeReplace", "Error replacing data the range." & @CRLF & "@error = " & @error & ", @extended = " & @extended)
MsgBox($MB_SYSTEMMODAL, "Excel UDF: _Excel_RangeReplace", "Data successfully replaced in cells.")

 

Share this post


Link to post
Share on other sites
water

I assume that the UsedRange property returns an area, not a range. An area is a collection of ranges.
Could you try:

Local $oArea = $oWorkbook.ActiveSheet.UsedRange.SpecialCells($xlCellTypeBlanks)
For $oRange in $oArea
    _Excel_RangeReplace($oWorkbook, $oWorkbook.ActiveSheet, $oRange, "", "0")
Next

 


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-01-27 - Version 1.3.3.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites
CC_Mu
1 hour ago, water said:

I assume that the UsedRange property returns an area, not a range. An area is a collection of ranges.
Could you try:

Local $oArea = $oWorkbook.ActiveSheet.UsedRange.SpecialCells($xlCellTypeBlanks)
For $oRange in $oArea
    _Excel_RangeReplace($oWorkbook, $oWorkbook.ActiveSheet, $oRange, "", "0")
Next

 

Hey Water, thank you so much. I tried this code, and add the error script just before Next

If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Excel UDF: _Excel_RangeReplace", "Error replacing data the range." & @CRLF & "@error = " & @error & ", @extended = " & @extended)
    MsgBox($MB_SYSTEMMODAL, "Excel UDF: _Excel_RangeReplace", "Data successfully replaced in cells.")

the error msg is 3: $sSearch is empty. I am not sure what should I do about this. Thank you so much for helping me

Share this post


Link to post
Share on other sites
Juvigy

You can use:

$oExcel.Application.ActiveWorkbook.Sheets(1).Range("E5:J20").Select
$oExcel.Application.Selection.Replace("", 0)

Share this post


Link to post
Share on other sites
CC_Mu
23 hours ago, junkew said:

In excel VBA it would be like this
Range("A1:BD1500").SpecialCells(xlCellTypeBlanks).value=0

This would then become in AutoIt with excel udf

$oWorkbook.ActiveSheet.Range("A1:BD1500").SpecialCells($xlCellTypeBlanks).value=0

 

This code works! thank you so much Junkew!

Share this post


Link to post
Share on other sites
CC_Mu
13 hours ago, Juvigy said:

You can use:

$oExcel.Application.ActiveWorkbook.Sheets(1).Range("E5:J20").Select
$oExcel.Application.Selection.Replace("", 0)

Your codes works too! Thank you so much Juvigy!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×