Use VBA to print out the value of a column cell

VBA is a relatively simple programming language. As long as you install Microsoft Office, you can use it to debug the code. You don't need to install third-party tools. It is worth taking the time to learn it. If you have the foundation of Python programming, it is very simple to learn. Today we will learn how to print out the value of a column of cells with VBA.

For example: we have values ​​from 1..10 in column A and 10 rows in Excel, and now we want to use debug.print to print out these values, how should we do it?

1. Use cells to get the value of the cell

Use Cells() to get the value of the cell, and use the for loop to traverse. code show as below:

Sub PrintColumnValues()
    Dim i As Integer
    For i = 1 To 10 '要打印的列数
        debug.print Cells(i, 3).Value '指定要打印的列,这里是第3列
    Next i
End Sub

This macro code will print the value of the first 10 cells in column 3.

You can modify the range of i and the column number in Cells(i, 3) to print the columns of cells you want.

debug.print will print the output in the "immediate window" of the VBA editor. You can open the Immediate Window tab of the View menu before running the macro.

This is very useful when debugging VBA code to easily view the values ​​of variables and cells without inserting a message box in the Excel sheet.

Second, use the Range object to get the value of the cell

First define a Range object, and then define its range

Sub PrintColumnValues()
    Dim col As Range
    Set col = Range("A1:A10") '指定第3列
    For Each cell In col
        debug.print cell.Value
    Next cell
End Sub

This prints all cell values ​​in column 1 A1:A10.

We use the Set keyword to create col as a range for column 3. We can then use a For Each loop to iterate through each cell in this range and print the value of each cell.

This is more flexible than using a fixed number of rows i and prints all cells in the column. If the column has more rows added in the future, this macro will still work fine.

Using range and For Each loops is a powerful way to access and manipulate Excel ranges in VBA.

3. Use the worksheets object to traverse and print

We can also implement traversal printing through the rows property of the worksheets object

Sub PrintNonBlankColumn1Values()
    Dim ws As Worksheet
    Set ws = Worksheets("Sheet1")
    For Each Row In ws.Rows
        If Row.Cells(1).Value <> "" Then  '检查第1列(索引1)是否非空
            Debug.Print Row.Cells(1).Value  '如果非空,打印值
        End If
    Next Row
End Sub

When looping through each row, first check if the cell value in the first column (index 1) of the current row is empty: row.Cells(1).Value <> ""

If not empty, print the value: debug.print row.Cells(1).Value

If empty, skip the loop and do nothing

Continue looping until all rows have been processed

This will only print the values ​​of all non-empty cells in the first column of the worksheet. Cells left blank are skipped, achieving the desired functionality.

row.Cells(1) accesses the first column (index 1) cell of each row. This is a common way to get the value of a cell at the intersection of a particular row and column.

Fourth, use the ThisWorkbook object to traverse

This method uses isempty to judge whether it is empty, and uses ws.Cells(ws.Rows.Count, 1).End(xlUp).Row to get the last row of the first column.
Get worksheets with ThisWorkbook.sheets

Sub PrintNonBlankColumn1Values()
    Dim ws As Worksheet
    Dim lastRow As Long
    Dim cell As Range
    Set ws = ThisWorkbook.Sheets("Sheet1")
    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

4. Post-school reflection

  1. On the whole, the method of using cell and Range is simpler and more efficient.
  2. Unlike Python, note that there is no colon in the for loop, but there is more next, so don't forget it when writing.

Guess you like

Origin blog.csdn.net/henanlion/article/details/130906021