做个双重循环咯,就是比较没效率。 Public Sub Count_Line() Dim iCount As Integer iCount = 0 For i = 1 To 65536 For j = 1 To 256 If Worksheets(1).Cells(i, j) <> "" Then iCount = iCount + 1 Exit For End If Next j Next i MsgBox iCount End Sub估计跑完需要点时间哦。汗~~我是没跑
Public Sub Count_Line() with worksheet Dim iCount As Integer iCount = 0 For i = 1 To .usedrange.rows.count For j = 1 To .usedrange.columns.count If Worksheets(1).Cells(i, j) <> "" Then iCount = iCount + 1 Exit For End If Next j Next i MsgBox iCount End Sub
按楼主的意思程序应该是这样写: Public Sub Count_Line() Dim ii, jj, MaxRow, MaxColumns As Integer ii = 1: jj = 1 MaxRow = 0: MaxColumns = 0 For i = Sheets(1).UsedRange.Rows.Count To ii Step -1 For j = Sheets(1).UsedRange.Columns.Count To jj Step -1 If Cells(i, j) <> "" Then jj = j If MaxRow = 0 Then MaxRow = i Exit For End If Next j Next i MaxColumns = jj MsgBox "有数据的最大行是:" + Str(MaxRow) + ",最大列是:" + Str(MaxColumns) End Sub
那你得一行行,一格格判断了。
srcnum = myworksheet1.range['a1'].currentregion.rows.count '取得某列的最后一个数据所在的行
Public Sub Count_Line()
Dim iCount As Integer
iCount = 0
For i = 1 To 65536
For j = 1 To 256
If Worksheets(1).Cells(i, j) <> "" Then
iCount = iCount + 1
Exit For
End If
Next j
Next i
MsgBox iCount
End Sub估计跑完需要点时间哦。汗~~我是没跑
with worksheet
Dim iCount As Integer
iCount = 0
For i = 1 To .usedrange.rows.count
For j = 1 To .usedrange.columns.count
If Worksheets(1).Cells(i, j) <> "" Then
iCount = iCount + 1
Exit For
End If
Next j
Next i
MsgBox iCount
End Sub
Public Sub Count_Line()
Dim ii, jj, MaxRow, MaxColumns As Integer
ii = 1: jj = 1
MaxRow = 0: MaxColumns = 0
For i = Sheets(1).UsedRange.Rows.Count To ii Step -1
For j = Sheets(1).UsedRange.Columns.Count To jj Step -1
If Cells(i, j) <> "" Then
jj = j
If MaxRow = 0 Then MaxRow = i
Exit For
End If
Next j
Next i
MaxColumns = jj
MsgBox "有数据的最大行是:" + Str(MaxRow) + ",最大列是:" + Str(MaxColumns)
End Sub