乔山办公网我们一直在努力
您的位置:乔山办公网 > excel表格制作 > <em>EXCEL</em>高手用<em>VBA</em>写两种FOR&

<em>EXCEL</em>高手用<em>VBA</em>写两种FOR&

作者:乔山办公网日期:

返回目录:excel表格制作


代码改了一下,应该很多,你自己试试看:

Sub 生成()
    Dim i,j,n, arr(2 To 1001, 2 To 1001)
    n = 0
    For i = 2 To 1001
        For j = 2 To 1001
            n = n + 1
            arr(i,j) = n
        Next
    Next
    Sheets("地图").Cells(2, 2).resize(1000,1000) = arr
End Sub


Sub s1()
    Dim i As Integer
    For i = 1 To 100
        Cells(i, 1) = i
    Next
End Sub
Sub s2()
    Dim c As Range
    For Each c In [b1:b100]
        c = c.Row
    Next
End Sub

这种写法肯定是错误的。

Sheet2.Range(Cells(i - 10, j - 10), Cells(i + 10, j + 10)))

虽然你的Range加了Sheet2限定语句,但是后面的cells(i-10,j-10),cells(i+10,j+10)都没有加限定语句,如果你的代码是在Sheet3工作表中,那么就表示sheet3.cells(i-10,j-10)了。

 

再则,e69da5e887aae79fa5e98193338当i=1的时候,i-10=-9,那么这样的单元格是不存在的。最小单元格行只能是1,列也是1。

所以要改,你要注意两点:

Dim m as Long,n as Integer
for i=1 to 100 
for j=1 to 100
With Sheet2
    If i < 11 and j < 11 Then
        m = 1: n =1
    Elseif i < 11 and j >= 11 Then
        m = 1: n = j
    Elseif i >= 11 and j < 11 Then
        m = i: n = 1
    ElseIf i >= 11 and j >=11 Then
        m = i: n = j 
    End If           
    Sheets("sheet3").Cells(i, j) = Application.WorksheetFunction.Average(.Range(.Cells(m,n), .Cells(i + 10, j + 10)))


简单的循环
Sub test()
Dim i As Long
Dim endRow As Long

endRow = Range("A" & Rows.Count).End(xlUp).Row '获取末行
if endRow < 1 then exit sub

For i = 1 To endRow-1
if trim(range("A" & i).value) = "" then

Range("F" & i).Formula = "=A" & i+1 & "&B" & i+1 & "&C" & i+1
end if

Next i
End Sub

或者
Sub test()
Dim i As Long
Dim endRow As Long

endRow = Range("A" & Rows.Count).End(xlUp).Row '获取末行
if endRow < 0 then exit sub

For i = 1 To endRow
if trim(range("A" & i).value) = "" then
Range("F" & i).Formula = "=A" & i & "&B" & i & "&C" & i
end if
Next i
End Sub
二段e69da5e6ba90e799bee5baa6e79fa5e98193365代码,前面是f1=(=a2&b2&c2),后一段是f1=(=a1&b1&c1)

相关阅读

关键词不能为空
极力推荐

ppt怎么做_excel表格制作_office365_word文档_365办公网