tgk*_*rog 9 excel vba excel-vba
想要在excel VBA中的选择中迭代每一行.
我有:
Dim rng As Range
Dim s As String
Set rng = Application.Selection
Debug.Print "c :" & rng.Address
Run Code Online (Sandbox Code Playgroud)
这打印
c:22美元:29美元
如何从第22行到第29行/解析开始和结束行?
在单独的int变量中获取开始和结束列?
dsp*_*ies 26
在Excel中循环遍历一系列单元格的一般解决方案:
Sub LoopSelection()
Dim cel As Range
Dim selectedRange As Range
Set selectedRange = Application.Selection
For Each cel In selectedRange.Cells
Debug.Print cel.Address, cel.Value
Next cel
End Sub
Run Code Online (Sandbox Code Playgroud)
从左上角的大多数细胞中迭代,然后向下移动.
仅直接遍历行。我选择了不止一列,只想要外循环行。
Option Explicit
'go over selection, merge text in cells, so all in first column in every row.
Sub mergeCombine()
Dim rng As Range
Dim s As String
Set rng = Application.Selection
Debug.Print "c :" & rng.Address
s = rng.Column
Dim cRow As Range
Debug.Print "c :" & rng.Address & "||" & rng.AddressLocal
Dim ir, ic As Integer
For ir = 1 To rng.Rows.Count
Set cRow = rng.Rows(ir)
s = ""
For ic = 1 To rng.Columns.Count
s = s & cRow.Columns(ic).Text
Cells(cRow.Row, cRow.Columns(ic).Column).Formula = ""
If (ic + 1) <= rng.Columns.Count Then
s = s & " "
End If
Next
Cells(cRow.Row, cRow.Column).Formula = ""
Cells(cRow.Row, cRow.Column).Formula = s
Next
End Sub
Run Code Online (Sandbox Code Playgroud)
它不适用于非连续选择。计数与非连续不正确,您必须使用 For Each cRow In rng.Rows 我自己没有尝试过,我的用例仅用于连续。谢谢 Danny Holstein(2019 年的评论)
归档时间: |
|
查看次数: |
66011 次 |
最近记录: |