简体   繁体   中英

Excel VBA how to have Sheet update after Macro

So I have this code...

           'Range("K" & varOffset).Select
            If ActiveCell.Offset(0, -1) = "" Then
                Output = "No"
            Else
                Output = "Yes"
            End If
            ActiveCell.FormulaR1C1 = Output

...But when I use the macro, the cell will only show up "Yes" or "No" regardless of whether I changed the cell to have something in it or not, but I want to be able to update this after I use the macro. Is there any way to put that formula into the cells using VBA?

For a purely VBA solution you need to refer to the .Value2 property element of the ActiveCell.Offset(0, -1) object. Just referencing the object does not do a compare with what value it currently has. Code would be:

'Range("K" & varOffset).Select
If ActiveCell.Offset(0, -1).Value2 = "" Then
    Output = "No"
Else
    Output = "Yes"
End If
ActiveCell.Value2 = Output

For a solution the creates a formula you would use something like:

ActiveCell.FormulaR1C1 = "=IF(ISBLANK(RC[-1]), ""No"", ""Yes"")"

Regards,

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