lastRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row For Each cell In ws.Range("A1:A" & lastRow) If Not IsEmpty(cell.Value) Then '检查第1列(索引1)是否非空 Debug.Print cell.Value '如果非空,打印值 End If Next cell End Sub 四、学后反思 综合来看用cell和Range的方法更为简单,运行...
str As String Set ws = ThisWorkbook.Sheets("表2") With ws lastRow = .UsedRange.Rows.Count lastCol = .UsedRange.Columns.Count arr = .Range(.Cells(1, 1), .Cells(lastRow, lastCol)).Value For i = LBound(arr) To UBound(arr) For j ...
不算Sub 和 End Sub代码只有5行,我对每行写了注释,并注明了笔记出处 Subshishi()For Each 工作表 In Worksheets '对每张工作表的A1单元格连续区域统计最大行 最大行=Sheets(工作表.Name).Range("A1").CurrentRegion.Rows.Count '对每张工作表的A1单元格连续区域统计最大列 'ExcelVBA第二季 笔记13.03讲到A...
ForEach i In Myarr MsgBox i Next i 注意,在使用ForEach时,不必给数组标明其下标为多少。Dim rng As Range, Arr, sums sums = 0 ForEach rng In Range("B1:B100")sums = sums + rng.Row Next MsgBox sums 再来看具体的例子 Set myTotal = myRange.Offset(myRange.Rows.Count - 1)....
说简单点,VBA 是运行在 Microsoft Office 软件之上,可以用来编写非软件自带的功能的编程语言。Office ...
Rows属性是一个只读属性,用于返回一个Range对象,表示制定工作表中的所有行。 【代码区域】 Sub row应用() For Each rw In Rows("1:4") '第一行到第四行 If rw.Row Mod 2 = 0 Then rw.RowHeight = 12 rw.Value = "数据" End If Next
For i = 1 To Selection.Areas.Count RowsNum = RowsNum + Selection.Areas(i).Rows.Count Next i MsgBox "所选区域中的行数为: " & RowsNum End Sub 3.统计选定区域中所包含的列数,如果选择了多个区域,则统计列数之和。Sub CountColumnsInSelection()Dim ColumnsNum As Integer For i =...
For Each rng In Selection.Words str = str & rng.Text str = str & vbTab i = i + 1 '每行8个词 If (i Mod 8) = 0 Then str = str & vbCrLf End If Next rng MsgBox str 结果如下图4所示。 图4:上图1所选文本中词和字符的分析结果 ...
' 获取数据范围 lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row arr = ws...
'清空主表数据(保留标题行)主表.Rows("2:"& 主表.Rows.Count).ClearContents ' 获取主表标题行Set 主标题行 = 主表.Rows(1) '遍历所有工作表ForEach分表InWorksheets' 排除主表If 分表.Name <> "汇总" Then'获取分表标题行Set分标题行 = 分表.Rows(1) ...