Vba,for循环(列的循环)1 to 他的自己可以找到数据范围,而不是设定的 用VBA实现循环,计算各列数据两两间的Cos值,只有第一次计...
VBA for\u5faa\u73af\u4e2d\u6709if\uff0c\u8bbe\u4e3a\u6761\u4ef6\u8df3\u5230\u4e0b\u4e00\u5faa\u73af\u5982\u679c\u8981\u8df3\u5230\u5916\u5c42\u5faa\u73af\u7684\u8bdd\uff0c\u5fc5\u987b\u5148\u7ed3\u675f\u5185\u5c42\u5faa\u73af, \u53ef\u4ee5\u7528exit for\u8bed\u53e5\u7ed3\u675f\u5185\u5c42\u5faa\u73af\uff0c\u5177\u4f53\u7684\u4ee3\u7801\u5982\u4e0b\uff1a
if .... then
next
end if
VBA for\u5faa\u73af\u662f\u4e00\u79cd\u91cd\u590d\u63a7\u5236\u7ed3\u6784\uff0c\u5b83\u5141\u8bb8\u5f00\u53d1\u4eba\u5458\u6709\u6548\u5730\u7f16\u5199\u9700\u8981\u6267\u884c\u7279\u5b9a\u6b21\u6570\u7684\u5faa\u73af\u3002
\u6269\u5c55\u8d44\u6599\uff1a
for\u5faa\u73af\u8bed\u6cd5
1\u3001\u8bed\u53e5\u6700\u7b80\u5f62\u5f0f\u4e3a\uff1a
for( ; ; )
2\u3001\u4e00\u822c\u5f62\u5f0f\u4e3a\uff1a
for\uff08\u5355\u6b21\u8868\u8fbe\u5f0f;\u6761\u4ef6\u8868\u8fbe\u5f0f;\u672b\u5c3e\u5faa\u73af\u4f53\uff09
{
\u4e2d\u95f4\u5faa\u73af\u4f53\uff1b
}
\u5176\u4e2d\uff0c\u8868\u793a\u5f0f\u7686\u53ef\u4ee5\u7701\u7565\uff0c\u4f46\u5206\u53f7\u4e0d\u53ef\u7701\u7565\uff0c\u56e0\u4e3a\u201c;\u201d\u53ef\u4ee5\u4ee3\u8868\u4e00\u4e2a\u7a7a\u8bed\u53e5\uff0c\u7701\u7565\u4e86\u4e4b\u540e\u8bed\u53e5\u51cf\u5c11\uff0c\u5373\u4e3a\u8bed\u53e5\u683c\u5f0f\u53d1\u751f\u53d8\u5316\uff0c\u5219\u7f16\u8bd1\u5668\u4e0d\u80fd\u8bc6\u522b\u800c\u65e0\u6cd5\u8fdb\u884c\u7f16\u8bd1\u3002
for\u5faa\u73af\u5c0f\u62ec\u53f7\u91cc\u7b2c\u4e00\u4e2a\u201c;\u201d\u53f7\u524d\u4e3a\u4e00\u4e2a\u4e3a\u4e0d\u53c2\u4e0e\u5faa\u73af\u7684\u5355\u6b21\u8868\u8fbe\u5f0f\uff0c\u5176\u53ef\u4f5c\u4e3a\u67d0\u4e00\u53d8\u91cf\u7684\u521d\u59cb\u5316\u8d4b\u503c\u8bed\u53e5, \u7528\u6765\u7ed9\u5faa\u73af\u63a7\u5236\u53d8\u91cf\u8d4b\u521d\u503c\u3002
\u53c2\u8003\u8d44\u6599\u6765\u6e90\uff1a\u767e\u5ea6\u767e\u79d1-for\u5faa\u73af
h = m = n = 0 \u8fd9\u79cd\u8d4b\u503c\u5728VBA\u4e2d\u4e0d\u89c4\u8303\uff0c\u4e0d\u63a8\u8350\u4f7f\u7528\u3002
For m = h + 1 To 4 \u8fd9\u79cd\u8d4b\u503c\u5728VBA\u4e2d\u4e0d\u89c4\u8303\uff0c\u4e0d\u63a8\u8350\u4f7f\u7528\u3002
\u5982\u679c\u4f60\u53ea\u662f\u5faa\u73af4\u5217\uff0c1651\u884c\uff1a
For n = 1 To 1651
\u5148\u53d64\u5217\u6570\u636e\u3002
a=Sheet1.Cells(n, 1).value
b=Sheet1.Cells(n, 2).value
c=Sheet1.Cells(n, 3).value
d=Sheet1.Cells(n, 4).value
\u8fd9\u91cc\u8fdb\u884ccos a \u548cb \u7684\u8ba1\u7b97......
\u8ba1\u7b97\u5b8c\u8d4b\u503c\u7ed9\uff1a
Sheet4.Cells(h, m) = s
next n
\u8fd9\u6837\u903b\u8f91\u6e05\u6670\uff0c\u54ea\u91cc\u6709\u95ee\u9898\u4e00\u770b\u5c31\u660e\u767d\u4e86\u3002
举个例子
假设某个表格A列有n行,其中没有空行
Sub xxx()
Dim n As String
n = WorksheetFunction.CountA(Columns(1))
For i = 1 To n
xxxxxx
Next
End Sub
n = WorksheetFunction.CountA(Columns(1))是调用EXCEL的CountA函数,统计A列的非空单元格个数,A列有多少行,n就得几,for就执行几次
绛旓細1銆佹垜浠厛鍒涘缓涓涓┖鐨勫嚱鏁般2銆佸湪鍑芥暟浣撻噷锛屽氨鍙互鍒涘缓涓涓for寰幆浜嗭紝浣跨敤For Next鐨勭粨鏋勶紝鍏朵腑J鍙橀噺灏辨槸瀹氫箟鐨勫惊鐜娆℃暟锛屼粠1鍒20锛屽垯琛ㄧず寰幆20娆°3銆佸惊鐜仛浠涔堟搷浣滃憿锛熷彲浠ュ湪for閲屽啓涓婂搴旂殑閫昏緫锛岃繖閲屽氨鏄缃崟鍏冩牸(Cells锛夌殑鍊笺4銆佹垜浠墽琛岃繖涓嚱鏁帮紝鐪嬩笅鏁堟灉銆5銆佸彲浠ョ湅鍒板湪B鍒楅噷鐨勫墠...
绛旓細1.for鈥︹ext缁撴瀯 杩欎釜缁撴瀯涓鑸槸杩欐牱鐨勶細for i=鍒濆鍊 to 缁撴潫鍊 step 姝ラ暱鍊 鈥︹ext 鍏蜂綋瑙i噴涓涓嬶紝鍒濆鍊兼寚鐨勬槸寰幆寮濮嬬殑鍊硷紝缁撴潫鍊兼寚鐨勬槸寰幆缁撴潫鐨勫硷紝瀹炶川涓婂氨鏄痠鍊煎緱涓涓垽鏂紝濡傛灉i鍊煎湪璧峰鍊煎拰缁撴潫鍊间箣闂达紝閭d箞杩涘叆寰幆璇彞锛岀劧鍚巌鍊兼瘡杩愯鍒皀ext璇彞鍚庝細鑷姩鍔犱竴涓闀垮硷紝鐩磋嚦i鍊...
绛旓細for next寰幆璇彞鍙ュ紡鐞嗚В锛氫粠寮濮嬪埌缁撴潫锛屽弽澶嶆墽琛For鍜孨ext涔嬮棿鐨勬寚浠ゅ潡锛岄櫎闈為亣鍒癊xit For璇彞锛屽皢鎻愬墠璺冲嚭寰幆銆傚叾涓紝姝ラ暱鍜孍xit For璇彞浠ュ強Next鍚鐨勫惊鐜鍙橀噺鍧囧彲鐪佺暐锛屾闀跨渷鐣ョ殑鏃跺欓粯璁や负1銆侲xit for 璇彞鏄己鍒剁粓姝㈠惊鐜殑璇彞锛屾墽琛屽畠鍚庡皢閫鍑寰幆锛鎵цnext鍚庨潰鐨勮鍙ャ傚for next寰幆璇彞鍙ュ紡杩涜...
绛旓細1銆佹墦寮涓涓〃锛岄変腑搴忓垪锛岀偣鍑诲伐鍏-瀹忥紝寮濮嬪綍鍒躲2銆佸綍鍒跺畬鎴愬悗鎸塧lt+f11 鍒囨崲鍒颁唬鐮佺紪杈戠獥鍙o紝璁剧疆鍙橀噺i涓洪暱鏁村舰锛屽苟涓旇缃竴涓for寰幆锛屽惊鐜娆℃暟姣旇娣诲姞鐨勫伐璧勬潯鏍囩澶э紝鍦╡nd sub涔嬪墠鍔爊ext銆3銆佹寜alt+f11鍒囨崲鍒癳xcel鐣岄潰锛屽啀褰曞埗涓涓垹闄ゆ爣棰樼殑瀹忥紝淇敼浠g爜锛屾坊鍔爁or寰幆鎸塧lt+f8璋冨嚭瀹忕獥鍙c4...
绛旓細涓句釜渚嬪瓙 鍋囪鏌愪釜琛ㄦ牸A鍒楁湁n琛岋紝鍏朵腑娌℃湁绌鸿 Sub xxx() Dim n As Stringn = WorksheetFunction.CountA(Columns(1))For i = 1 To nxxxxxxNext End Subn = WorksheetFunction.CountA(Columns(1))鏄皟鐢‥XCEL鐨凜ountA鍑芥暟锛岀粺璁鍒楃殑闈炵┖鍗曞厓鏍间釜鏁帮紝A鍒楁湁澶氬皯琛岋紝n灏卞緱鍑锛宖or灏辨墽琛屽嚑娆 ...
绛旓細鐢ㄨ鍙xit for鏉ヨ烦鍑for寰幆锛杩欓噷璺冲嚭寰幆鎸囩殑鏄烦鍑烘渶閲岄潰涓灞鐨勫惊鐜缁撴瀯銆備唬鐮佸疄渚嬪涓嬶細for ii = 1 to 1000 if ii = 500 then exit for end if next 杩欓噷鐨勪唬鐮佽〃绀篿i浠1鍒1000杩涜寰幆锛屽鏋渋i涓500鏃跺垯璺冲嚭寰幆銆
绛旓細鐢ㄨ鍙xit for鏉ヨ烦鍑for寰幆锛杩欓噷璺冲嚭寰幆鎸囩殑鏄烦鍑烘渶閲岄潰涓灞鐨勫惊鐜缁撴瀯銆備唬鐮佸疄渚嬪涓嬶細for ii = 1 to 1000 if ii = 500 then exit for end if next 杩欓噷鐨勪唬鐮佽〃绀篿i浠1鍒1000杩涜寰幆锛屽鏋渋i涓500鏃跺垯璺冲嚭寰幆
绛旓細Excel鐨VBA閲岃繘琛屼竴鍒楃殑閫愪釜鎿嶄綔锛岄偅涔堝彲浠ョ敤For寰幆璇彞锛岄鍏堝畾涔変竴涓彉閲忥紝濡傦細x锛屼唬鐮佸涓嬶細For x=1 to 30 鏌ユ壘鐩稿悓鏁版嵁鐨勪唬鐮 Next x 娉細1琛ㄧず浠庣涓涓暟鎹紑濮嬶紝30琛ㄧず缁撴潫鐨勪綅缃紝濡傛灉鏈60琛岋紝閭d箞鏀规垚60鍗冲彲銆
绛旓細For m = 1 To 4 sheets("1").Cells(60, 3 * m) = Application.WorksheetFunction.Max(cells(4,3*m).resize(1,47)) 鈥樻眰鍖洪棿涓渶澶у sheets("1").Cells(61, 3 * m) = Application.WorksheetFunction.countif(cells(4,3*m).resize(1,47),">60") '杩欓噷鐨勫兼槸缁熻澶т簬60鐨勪釜鏁...
绛旓細鍦VBA鐨刦or寰幆涓紝濡傛灉浣犳兂瑕佹牴鎹煇涓潯浠惰烦鍒颁笅涓娆寰幆锛鑰屼笉鏄墽琛屽畬褰撳墠鐨勮凯浠o紝鍙互浣跨敤if...then璇彞缁撳悎exitfor鏉ュ疄鐜般傚叿浣撴楠ゅ涓嬶細棣栧厛锛屽湪for寰幆鍐呴儴锛屽綋婊¤冻鐗瑰畾鏉′欢鏃讹紝鍙互鎻掑叆涓涓猧f璇彞:ifcondition_variable婊¤冻鐗瑰畾鏉′欢then 鎺ヤ笅鏉ワ紝濡傛灉鏉′欢鎴愮珛锛屼綘鍙互浣跨敤exitfor璇彞鐩存帴缁撴潫褰撳墠鐨...