本文介绍了在单元格中使用IF()语句运行Sub过程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Excel工作表中遇到了某些问题,这可能是由于缺乏理解所致.基本上,我在单元格I2中有一个IF()语句,对于if_true,我想运行一个公共Sub,即Sub MyFunc(),它仅将行中的数据复制到新的工作表中.

I am having trouble with something in my excel sheet and it's probably due to lack of understanding. Basically, I have an IF() statement in cell I2, and for the if_true I would like to run a Public Sub, namely Sub MyFunc() that just copies data from the row to a new sheet.

换句话说,我在单元格I2中:

In other words, I have in cell I2:

=IF(OR(AND($D2="ABOVE", $F2>$E2, $H2="YES"), AND($D2="BELOW", $F2<$E2, $H2="YES")),MyFunc(),"")

这似乎根本不运行sub,如果我在VBE中运行Sub,它就可以正常工作.

This does not seem to run the sub at all and if I run the Sub in the VBE it works fine.

Sub MyFunc()

'begin populating table
'Date
Sheets("Sheet3").Range("A:A").End(xlDown).Offset(1, 0).Value = Format(Now(), "dd/mm/yyyy")
'DayofWeek
Sheets("Sheet3").Range("B:B").End(xlDown).Offset(1, 0).Value = Format(Now(), "ddd")
'copy code and paste in cell
Sheets("Sheet1").Range("A2").Copy Destination:=Sheets("Sheet3").Range("E:E").End(xlDown).Offset(1, 0)
'copy level
Sheets("Sheet1").Range("E2").Copy Destination:=Sheets("Sheet3").Range("F:F").End(xlDown).Offset(1, 0)
'delete row information
Worksheets("Sheet1").Range("A2").ClearContents
Worksheets("Sheet1").Range("C2:E2").ClearContents
Worksheets("Sheet1").Range("G2:H2").ClearContents
'resort colums
SortByMarket
   End Sub

基于上述条件,关于如何调用此Sub,我有什么建议吗?

Is there any advice I can get in how to call this Sub based on the above condition?

任何帮助都将不胜感激:)

Any help is greatly appreciated :)

推荐答案

因此,将if张贴在您的vba代码中的答案.

So an answer to post the if inside your vba code.

Sub Test()

If (Range("D2").Value = "ABOVE" And Range("F2").Value > Range("E2").Value And Range("H2").Value = "YES") Or (Range("D2").Value = "BELOW" And Range("F2").Value < Range("E2").Value And Range("H2").Value = "YES") Then

MsgBox "CLEAR"

' HERE YOUR CLEARING CODE

End If

End Sub

注意:我不会删除其他答案,因为我认为这是一种可能的选择,它也可以回答您提出的问题.

Note: I don't delete the other answer as I think it was a possible alternative that also answers the question you asked.

这篇关于在单元格中使用IF()语句运行Sub过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 01:18