excel - 如何从第 3 行开始选择整列 - vba

标签 excel vba if-statement for-loop

嘿,伙计们,这是我的代码,它正在工作,但我不想选择整个列,而是想选择第 3 行之后的所有列,
例如,我想选择所有列 b 但在 B3 之后
我希望这是有道理的。

有任何想法吗?

Sub HorizontalLoop()
Dim lCol As Long

Sheets("output").Select
For lCol = 1 To 100

Dim inputrange As String
If Not IsEmpty(Cells(lCol).Value) Then
inputrange = Cells(1, lCol).Value

Cells(1, lCol).EntireColumn.Select
Selection.Copy
Sheets("input").Select
ActiveSheet.range(inputrange).Select
ActiveSheet.Paste
Sheets("output").Select
End If
Next lCol
End Sub

任何帮助将不胜感激:)
谢谢
H

最佳答案

这是您的代码(带有您的答案),稍微清理了一下:

Sub HorizontalLoop()
Dim lCol As Long
Dim inputrange As String
With Sheets("output")
    For lCol = 1 To .Cells(1, .Columns.Count).End(xlToLeft).Column
        If Not IsEmpty(.Cells(1, lCol).Value) Then
            inputrange = .Cells(1, lCol).Value
            .Range(.Cells(3, lCol), .Cells(.Rows.Count, lCol)).Copy Sheets("input").Range(inputrange)
        End If
    Next lCol
End With
End Sub

尽量避免使用 .Select and .Activate .

此外,最好在使用两张纸时确保正确的亲子关系。 With block 及其对应的 .允许这样做。

关于excel - 如何从第 3 行开始选择整列 - vba,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36431065/

相关文章:

c# - ClosedXML 查找最后一行编号

c# - 连接到网络上的 Excel 工作表(数据库)

css - 条件 CSS。不同输入文本的不同颜色

c# - 如果检查反转

javascript - 在 Node Js 中使用 node-excel-export 创建多个 Excel 工作表

excel - 有没有办法加快这个 VBA 宏的运行速度,以在 1000 次的大范围内执行 Vlookup?

Excel VBA 文件由同一用户与其他用户打开

json - 在 Excel VBA 中将 JSON 发布到网络

vba - 已注册的 COM DLL 在 VBA 中有效,但在 VBScript 中无效

r - 将向量的非零值变为 1