简体   繁体   English

如何遍历范围对象 VBA 的每一行中的特定列?

[英]How to loop through a specific column in each row for a range object VBA?

How to loop through range object for each row for a specific column.如何为特定列的每一行循环范围对象。 For instance, if range object is A1:D5 I want to loop through A4, B4, C4, D4 values.例如,如果范围对象是A1:D5我想遍历A4, B4, C4, D4值。

When I try当我尝试

Set ranges = Application.InputBox(Title:="Select the Range", Type:=8)

For Each r In ranges.Rows
    Msgbox r(1,4) 
Next r

It trows me an error.它告诉我一个错误。

Try below codes试试下面的代码

Sub Loop4Row()
Dim r As Long, Ranges As Range

Set Ranges = Application.InputBox("Select the Range", Type:=8)
    For r = Application.Min(Ranges.Column) To Application.Max(Ranges.Columns)
        MsgBox Cells(4, r)
    Next r

End Sub

Try this.试试这个。 Edit: Added condition for displaying cells on 4th row.编辑:添加了在第 4 行显示单元格的条件。

Set Ranges = Application.InputBox(Prompt:="Select the Range", Title:="Select the Range", Type:=8)

For Each r In Ranges
  If r.Row = 4 Then
    MsgBox r.Value
  End If
Next r

Loop Through Cells of a Row循环遍历一行的单元格

Short短的

Dim r as Range
For Each r In ranges.Rows(4).Cells
    Msgbox r.Address, r.Value
Next r

' or 

Dim r As Range, rg As Range
Set rg = ranges.Rows(4)
For Each r in rg.Cells
    Msgbox r.Address, r.Value
Next r

Long

Option Explicit

Sub LoopThroughTheFourthRow()
    
    Const sTitle As String = "Loop Through the Fourth Row"
    Const sRow As Long = 4
    
    ' Determine the parameter of the 'Default' argument.
    Dim dAddress As String
    If TypeName(Selection) = "Range" Then
        dAddress = Selection.Address
    Else
        dAddress = "$A$1"
    End If
     
    ' Input.
    On Error Resume Next
    Dim srg As Variant
    Set srg = Application.InputBox(Prompt:="Select a range", _
        Title:=sTitle, Default:=dAddress, Type:=8)
    On Error GoTo 0
    
    ' Validate input (Canceled?).
    If TypeName(srg) <> "Range" Then
        MsgBox "You canceled.", vbExclamation, sTitle
        Exit Sub
    End If
    
    ' Validate input (too few rows).
    Dim srCount As Long: srCount = srg.Rows.Count
    If srCount < sRow Then
        MsgBox "There are only " & srCount & " rows in the range.", _
            vbExclamation, sTitle
        Exit Sub
    End If
    
    'Debug.Print srg.Address(0, 0)
    
    ' Loop through the cells of the one-row range.
    Dim sCell As Range
    For Each sCell In srg.Rows(sRow).Cells
        Debug.Print sCell.Address(0, 0), sCell.Value
    Next sCell

End Sub

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 VBA循环遍历范围,如果匹配:将行和特定列标题的一部分附加到新工作表中的表 - VBA to loop through range, if match: append part of row and specific column header to table in new sheet 从VBA中的用户输入范围循环遍历每一行 - Loop through each row from a user input range in VBA 在vba中使用带有多个条件的if语句循环遍历给定范围内的每一行 - loop through each row in a given range with if statement with multiple conditions in vba 使用Excel表和VBA在动态列的每一行中循环 - Loop through each row in dynamic column using Excel Tables and VBA 如何编写一个 VBA 代码,该代码将循环遍历固定范围并在另一张表中将该范围的每一行填充 N 次(带编号)? - How to write a VBA code that will loop through a fixed range and populate each row of that range N times (with numbering) in another sheet? 如何使用VBA浏览选定范围内的每一行 - How to go through each row within a selected range using VBA VBA 代码在特定范围内的每一行中循环,但循环必须在下一行开始一个新条件 - VBA code to loop in each row within a specific range, but the loop must start a new criteria in every next row VBA用于每个循环以划分范围 - VBA use for each loop to divide through a range VBA脚本循环遍历特定日期范围 - VBA Script to Loop Through Specific Date Range 如何遍历K列中的每一行,直到A列为空 - How to loop through each row in column K until column A is empty
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM