VBA if cell is empty then...

xunda_gunda

New Member
Joined
Aug 17, 2012
Messages
22
Hello,

I want your help.
I want to use macro, that will search a blank cells in predetermined range and if cell is empty it should paste formula.
I want to run this macro automatically in every 1 second.

I searched procedures like this and tried to adjust to my excel, but something goes wrong.

Code:
Sub Check_Empty_Cells_in_Every_3_Seconds()
Application.OnTime Now + TimeValue("00:00:01"), "Fill_Empty_Cells_with_Formulas"
End Sub


Sub Fill_Empty_Cells_with_Formulas()
    Dim rng As Range
    Dim i As Long


    'Set the range in column A you want to loop through
    Set rng = Range("D15:D139")
    For Each cell In rng
        'test if cell is empty
        If cell.Value <> "" Then
            'write to adjacent cell
[COLOR=#ff0000]           cell.Offset(0, 0).Value = "=IF(ISTEXT(INDIRECT(ADDRESS(ROW(),COLUMN()-2,4),TRUE)),"",IF(INDIRECT(ADDRESS(ROW(),COLUMN()-2,4),TRUE)<12,"",IF(INDIRECT(ADDRESS(ROW(),COLUMN()-2,4),TRUE)<=$D$8,"",IF(OFFSET(INDIRECT(ADDRESS(ROW(),COLUMN()-2,4),TRUE),-12,2)<>"",MIN(OFFSET(INDIRECT(ADDRESS(ROW(),COLUMN()-2,4),TRUE),-12,2),INDIRECT(ADDRESS(ROW()-1,COLUMN()+3,4),TRUE)),""))))"[/COLOR]
        End If
    Next
End Sub
Red part of code is not working:

Run-time error ‘1004’:
Application-defined or object defined error

What am I missing?

Thank you in advance.
 
Last edited:

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
What do you want the formula to do?

And what does this mean
"I want to run this macro automatically in every 1 second."

You want the same script to keep running every second from now to eternity?
 
Upvote 0
No you have misunderstood. I mean in the formula.

=IF(ISTEXT(INDIRECT(ADDRESS(ROW(),COLUMN()-2,4),TRUE)),"", etc etc

needs to be

=IF(ISTEXT(INDIRECT(ADDRESS(ROW(),COLUMN()-2,4),TRUE)),"""", etc etc
 
Upvote 0
No you have misunderstood. I mean in the formula.

=IF(ISTEXT(INDIRECT(ADDRESS(ROW(),COLUMN()-2,4),TRUE)),"", etc etc

needs to be

=IF(ISTEXT(INDIRECT(ADDRESS(ROW(),COLUMN()-2,4),TRUE)),"""", etc etc

ah right, sorry my mistake - I thought you were referring to the IF statement.
 
Upvote 0
Thank you all for your responses.
What do you want the formula to do?

And what does this mean
"I want to run this macro automatically in every 1 second."

You want the same script to keep running every second from now to eternity?

Yes, that's what I meant. While file is open.


Correcting quotes helped.
Code runs without any error, but it actually does another work.

In a range I have 3 kind of cells: with formulas, with values and empty cells.
I want code that will check if cell is empty and paste formula. Cells with values shouldn't be changed.

This code deleted values and paste formulas instead, but empty cells are still empty.
I think I have wrong logic in code.
Sorry for my English, I hope i explained problem clearly.
 
Upvote 0
You have

Code:
If cell.Value <> "" Then

you need

Code:
If cell.Value = "" Then
 
Upvote 0
I have this script in "ThisWorkbook"
Code:
Private Sub Workbook_Open()Application.OnTime Now + TimeValue("00:00:01"), "Fill_Empty_Cells_with_Formulas"
End Sub

and this one in Modules:
Code:
Sub Fill_Empty_Cells_with_Formulas()    Dim rng As Range
    Dim i As Long


    'Set the range in column A you want to loop through
    Set rng = Range("D15:D139")
    For Each cell In rng
        'test if cell is empty
        If cell.Value = "" Then
            'write to adjacent cell
           cell.Offset(0, 0).Value = "=IF(ISTEXT(INDIRECT(ADDRESS(ROW(),COLUMN()-2,4),TRUE)),"""",IF(INDIRECT(ADDRESS(ROW(),COLUMN()-2,4),TRUE)<12,"""",IF(INDIRECT(ADDRESS(ROW(),COLUMN()-2,4),TRUE)<=$D$8,"""",IF(OFFSET(INDIRECT(ADDRESS(ROW(),COLUMN()-2,4),TRUE),-12,2)<>"""",MIN(OFFSET(INDIRECT(ADDRESS(ROW(),COLUMN()-2,4),TRUE),-12,2),INDIRECT(ADDRESS(ROW()-1,COLUMN()+3,4),TRUE)),""""))))"
        End If
    Next
End Sub
It only works when I open workbook. But I want macro (Fill_Empty_Cells_with_Formulas) to run automatically in every 1 second after opening workbook.

Any help?
Pls :(
 
Upvote 0

Forum statistics

Threads
1,223,767
Messages
6,174,395
Members
452,561
Latest member
amir5104

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top