excelVBA判断语句
本文教你如何使用VBA判断语句。1)If…Then…Else语句
If condition Then [statements][Else elsestatements]
如1:If A>B And C<D Then A=B+2 Else A=C+2
如2:If x>250 Then x=x-100
或者,可以使用块形式的语法:
If condition Then
[statements]
[ElseIf condition-n Then
[elseifstatements] …
[Else
[elsestatements]]
End If
如1:
If Number < 10 Then
Digits = 1
ElseIf Number < 100 Then
Digits = 2
Else
Digits = 3
End If
2)Select Case…Case…End Case语句
如1:
Select Case Pid
Case “A101”
Price=200
Case “A102”
Price=300
……
Case Else
Price=900
End Case
3)Choose 函数
choose(index, choce-1,choice-2,…,choice-n),可以用来选择自变量串列中的一个值,并将其返回,index 必要参数,数值表达式或字段,它的运算结果是一个数值,且界于 1 和可选择的项目数之间。choice 必要参数,Variant表达式,包含可选择项目的其中之一。如:
GetChoice = Choose(Ind, "Speedy", "United", "Federal")
4)Switch函数
Switch(expr-1, value-1[, expr-2, value-2 _ [, expr-n,value-n]])
switch函数和Choose函数类似,但它是以两个一组的方式返回所要的值,在串列中,最先为TRUE的值会被返回。 expr 必要参数,要加以计算的 Variant 表达式。value 必要参数。如果相关的表达式为 True,则返回此部分的数值或表达式,没有一个表达式为 True,Switch 会返回一个 Null值。
绛旓細浜虹敓缁忓父瑕侀潰涓粹滃閫変竴鈥濈殑鎯呭喌锛屽湪缂栧啓绋嬪簭浠g爜鏃朵篃鏄姝ゃ傚湪涓婄瘒鏂囩珷涓紝鎴戜滑璁茶В浜嗕娇鐢↖f-Then缁撴瀯鏉ヨ繘琛鍒ゆ柇锛屼篃涓句簡寰堝涓皬渚嬪瓙銆傛垜浠湅鍒帮紝闅忕潃鏉′欢鐨勫澶氾紝浠g爜鐨勭粨鏋勪篃鍙樺緱澶嶆潅鎴栭毦浠ョ悊瑙c傚垢杩愮殑鏄紝VBA涓烘垜浠彁渚涗簡Select Case缁撴瀯锛岃鎴戜滑鏍规嵁澶氫釜鏉′欢鍒ゆ柇閫夋嫨鏃讹紝鑳藉鍐欏嚭瀹规槗鐞嗚В鐨勪唬鐮併備笂绡...
绛旓細鈥濅负渚嬶紝鍏蜂綋浠g爜濡備笅銆1.�0�2�0�2If cells(1,1) Like 鈥*?*鈥 Then2. �0�2If InStr(1, cells(1,1), 鈥?鈥) = 0 Then鐢VBA鍒ゆ柇EXCEL鍏冪礌鏄惁鍖呭惈鐗瑰畾瀛楃鐨勬儏鍐佃緝涓哄父鐢紝涓斿鏀惧湪寰幆璇彞涓紝浠庤繍琛屾晥鐜囨潵璁诧紝绗簩绉嶆柟娉曚紭...
绛旓細鏂规硶鍜岃缁嗙殑鎿嶄綔姝ラ濡備笅锛1銆佺涓姝ワ紝鍚屾椂鎸夆 win + R鈥濋敭锛屽湪鈥滆繍琛屸濈殑瀵硅瘽妗嗕腑杈撳叆鈥 excel鈥濓紝鐒跺悗鎸塃nter閿墦寮鏂扮殑excel鏂囨。锛岃涓嬪浘锛岃浆鍒颁笅闈㈢殑姝ラ銆2銆佺浜屾锛屾墽琛屽畬涓婇潰鐨勬搷浣滀箣鍚庯紝渚濇鍗曞嚮鈥滃紑鍙戝伐鍏封-->鈥 Visual Basic鈥濆伐鍏锋爮锛岃涓嬪浘锛岃浆鍒颁笅闈㈢殑姝ラ銆3銆佺涓夋锛屾墽琛...
绛旓細鍙互浣跨敤VBA涓鐨処f璇彞鍜孎or Each寰幆鏉ュ疄鐜般傞鍏堬紝鍦╒BA涓畾涔夌鍚堟潯浠剁殑鏁版嵁锛屽彲浠ヤ娇鐢ㄧ被浼间笅闈㈢殑浠g爜锛欴im targetSheet As Worksheet Dim sourceSheet As Worksheet Dim lastRow As Long Dim i As Long Set sourceSheet = Worksheets("Sheet1") ' 鎸囧畾婧愭暟鎹墍鍦ㄥ伐浣滆〃 Set targetSheet = ...
绛旓細Sub 瀹() With Application.FindFormat.Interior .PatternColorIndex = xlAutomatic .Color = 65535 .TintAndShade = 0 .PatternTintAndShade = 0 End With Set Rng = Columns("A:A").Find(What:="", After:=ActiveCell, LookIn:=xlFormulas, LookAt _ :=xlPart, SearchOr...
绛旓細Dim rng1 As Range '鎶妑ng鍙橀噺瀹氫箟涓簉ange绫诲瀷 On Error Resume Next '涓汉缁忛獙鍔犱簡杩欐潯璇彞鍙互閬垮厤涓浜涜帿鍚嶅叾濡欑殑閿欒锛屼篃鍙互涓嶈銆係et rng1 = Application.InputBox(prompt:="璇烽夋嫨鍖哄煙锛", Title:="妤肩尓", Default:=ActiveCell.Address, Type:=8) '杩欐潯璇彞灏辫皥鍑哄璇濇锛岃浣犻夋嫨鍖哄煙...
绛旓細瑕侀氳繃VBA鎶奡heet1宸ヤ綔琛ㄤ腑鐨勬暟鎹啓鍏heet2宸ヤ綔琛ㄤ腑锛屽彲浠ラ噰鐢ㄥ涓嬫柟娉曪細1銆佸亣濡傝鎶奡heet1宸ヤ綔琛ㄤ腑A1鍗曞厓鏍肩殑鏁版嵁鍐欏叆Sheet2宸ヤ綔琛ㄧ殑A1鍗曞厓鏍间腑銆2銆佸彲浠ユ墽琛屼互涓璇彞锛歴ub test()sheets("Sheet2").range("A1").Value=sheets("Sheet1").range("A1").Value end sub 3銆佽鍙ヨВ閲婏細range("A1")...
绛旓細EXCEL VBA涓紝濡備綍鍒ゆ柇鏁版嵁绫诲瀷锛熶娇鐢ㄥ彉閲忕殑绗竴姝ユ槸浜嗚В鍙橀噺鐨勬暟鎹被鍨嬨傚彉閲忕殑鏁版嵁绫诲瀷鎺у埗鍙橀噺鍏佽淇濆瓨浣曠绫诲瀷鐨勬暟鎹備笅闈㈣繖涓〃鍒楀嚭浜哣BA鏀寔鐨勬暟鎹被鍨嬶紝浠ュ強鍚勭鏁版嵁绫诲瀷鐨勫彉閲忔墍闇瑕佺殑瀛樺偍绌洪棿鍜岃兘澶熷瓨鍌ㄧ殑鏁板艰寖鍥淬備綔涓烘垜浠紪鍐欎唬鐮侊紝涓轰簡鍋氬埌鏁堢巼锛屼竴涓洰鏍囨槸閫夋嫨闇瑕佸瓨鍌ㄧ┖闂村敖鍙兘灏忕殑鏁版嵁绫诲瀷鏉...
绛旓細Sub 鍒ゆ柇()Dim int2 As Integer For int1 = 2 To Range("A65536").End(xlUp).Row If Cells(int1, 1).Value = Cells(int1, 3).Value And Cells(int1, 2).Value = Cells(int1, 4).Value Then With Cells(int1, 5).Value = "pass".Interior.ColorIndex = 3 .Font.Color = 1...
绛旓細鍑芥暟涓殑if璇彞=if锛堟潯浠讹紝绗﹀悎鏉′欢缁撴灉锛屼笉绗﹀悎鏉′欢缁撴灉锛夈VBA涓涔熸湁涓涓悓鏍风殑鍑芥暟锛屼负浜嗚窡鍑芥暟鐨刬f鍖哄垎锛vba涓鐨勫悕绉颁负iif锛屼娇鐢ㄦ柟娉曚笌鍑芥暟瀹屽叏涓鑷达紝iif锛堟潯浠讹紝绗﹀悎鏉′欢缁撴灉锛屼笉绗﹀悎鏉′欢缁撴灉锛夈俈BA涓彟澶栦竴绉峣f琛ㄨ揪鏂瑰紡鏇翠负甯哥敤锛屾牸寮忎负 if 鏉′欢 then 鎵ц缁撴灉鎴栬 if 鏉′欢 then缁撴灉涓缁撴灉浜宔lse...