在ExcelVBA中怎麼寫if函式

2021-03-10 01:47:52 字數 3537 閱讀 7525

1樓:匿名使用者

如果要寫公式,就直接用雙引號括起來即可,如:內 range("a1")="=if(p9=0,0,p9/(100+p9)*t9*u9)"

如果想直

接得到值容:

if range("p9")=0 thenrange("a1")=0

else

range("a1")=range("p9")/(100+range("p9"))*range("t9")*range("u9")

end if

2樓:匿名使用者

if range("p9")=0 then變數名 = 0

else

變數名 = range("p9")/(100+range("p9"))*range("t9")*range("u9")

end if

變數名可以認為是你要返回數回據的單元格答

3樓:匿名使用者

if range("p9")=0 thenrange("v9")=0

else

range("v9")=range("p9").value/(100+range("p9").value)*range("t9").

value*range("u9").value

endif

【求助】在excel vba中怎麼寫sumif函式

4樓:匿名使用者

如果要寫公式bai,du就直接用雙zhi引號括起來即可,如dao

:回 range("c9")="=sumif(m:v,a9,v:v)"

如果想直接得到答值:range("c9")=worksheetfunction.sumif(cloumns("m:v"),range("a9"),cloumns("m:v")

5樓:匿名使用者

worksheetfunction.sumif.

excel 關於vba中if多條件語句的用法,虛心求教

6樓:陽光上的橋

vba中的多條件if,一般有兩種情況:

一是使用and、or連線多個條件,例如:

if a1=1 and a2=2 or a2="" then二是使用if巢狀,例如:

if a1=1 then

if a2=2 then ... else ...

else

if a2="" then ... else ...

end if

7樓:匿名使用者

if語句的語法:

if 條件

判斷語句1 then

處理語句a

elseif 條件判斷語句2 then

處理語句b

else

處理語句c

end if

其中,條件判斷語句需要使用判斷符,常用的判斷符有「==」(相等)、「>」(大於)、「>=」(大於等於)、「<」(小於)、「<=」(小於等於)、「<>」(不等於)。

如果有多個並列的條件,可以使用「and」(邏輯與)、「or」(邏輯或)來連線。

例子:if ***==*** and yyy<>yyy then<...>

elseif ***==yyy or ***<=yyy then<...>

else

<...>

end if

補充:如果有多條並列條件,且要混用or和and,不妨使用括號。

8樓:匿名使用者

1、if語句的含義:if語句字面意思就是如果的意思,如果條件為真,那麼就執行if語句裡面的程式,這個程式,一般指then後面的,一個if語句可以沒有end if,但絕對不能沒有then,這一點是要注意的。

2、if……then……結構

該結構一般用於比較簡單的判斷語句執行,下面以一段例項程式來講解。

例項:如果第一個單元格不為空,那麼就彈出一個提示框「單元格不為空」。

程式:sub 判斷語句()

if cells(1, 1) <> "" then msgbox "單元格不為空"

end sub

3、if……then……end if結構

該結構用於單選擇判斷語句執行,具體用法看下面例項。

例項:對於第一列的第一行到第十行單元格,作如下判斷,如果單元格為空值了那麼在相應的第二列單元格輸出「vba教研室」。

程式:sub 判斷語句()

dim i as integer

for i = 1 to 100

if cells(i, 1) = "" then

cells(i, 2) = "vba教研室"

exit for

end if

next

end sub

4、if ……then……else……end if結構

該結構用於事件只有兩個可能的情況先選擇的判斷語句,具體見下面例項分析。

例項:對於第一列的第1,2行兩個單元格做判斷,如果單元格值為空,則在相應的第二列單元格輸出false,否則輸出ture。

程式:sub 判斷語句()

dim i as integer

for i = 1 to 2

if cells(i, 1) = "" then

cells(i, 2) = "false"

else

cells(i, 2) = "true"

end if

next

end sub

5、if ……then……elseif……then……else……end if結構

該結構用於多條件判斷語句執行,具體見下面例項分析

例項:對三名同學的成績作出判斷,大於等於80分的為優秀;60到80之間的為及格;0到60分之間的為不及格;0分為考試無效。

程式:sub 判斷語句()

dim i as integer

for i = 2 to 7

if cells(i, 2) >= 80 then

cells(i, 3) = "優秀"

elseif cells(i, 2) < 80 and cells(i, 2) >= 60 then

cells(i, 3) = "及格"

elseif cells(i, 2) < 60 and cells(i, 2) > 0 then

cells(i, 3) = "不及格"

else

cells(i, 3) = "考試無效"

end if

next

end sub

6、if語句巢狀使用

if語句可以靈活的巢狀使用的,如下面這套程式

sub 判斷語句()

sub 判斷語句()

if cells(1, 1) <> "" then

if cells(1, 1) = "vba教研室" then

msgbox "ok!"

else

msgbox "no!"

end if

end if

end sub

excelvba中執行sqlselect的問題

sub test dim conn as new adodb.connectiondim strconn as stringdim strsql as string dim rs as new adodb.recordsetstrconn provider sqloledb server 192.1...

請高手幫我寫excel vba程式,實現自動記錄A列最近3日內資料

如果每天都會開啟 而且a列資料每天不同時刻開啟的資料都相同,實現應該不難。如果不開啟 的情況下讓它自動記錄,就不知道有沒有辦法了。excel裡vba如何實現1列輸入資料,2列自動出現時間 3列輸入資料,4列自動出現時間 如圖功能再擴充些 5 你圖裡面的 已經是實現任意列輸入後,下一列自動出現時間了哦...

如果在excel VBA中判斷A列中的空白單元格,遇到後就終止程式

在工作表名稱上點右鍵,選檢視 貼上下面的 sub 檢查空單元格 dim rng as range,arr for each rng in range a1 a30 if rng then n n 1 redim preserve arr 1 to n arr n rng.address 0,0 en...