VBA循环通过列 [英] VBA Loop Through Columns

查看:122
本文介绍了VBA循环通过列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个宏,该宏将选择第一行中带有"Est#"的列,并将整个列粘贴到新列中.

I am trying to write a macro that will select the column with "Est #" in the first row and paste the entire column in a new column.

我知道如何遍历行:

Sub test()
Dim i As Integer
For i = 1 To 100
    If Range("C" & i) = "All values USD Millions." Then
        Range("C" & i & ":H" & i).Delete
    Else
        Range("A3").Select
    End If
Next
End Sub

问题在于列中有字母,而不是数字,所以我不确定如何遍历前30列并将第一行中带有"Est#"的列粘贴到Range("CA:CA")中.

The problem is that columns have letters, not numbers, so I am unsure how I can loop through the first 30 columns and paste the column with "Est #" in the first row into Range("CA:CA").

推荐答案

您可以这样做,尽管查找"更有效

You could do it thus, although Find is more efficient

Sub test()

Dim c As Long

For c = 1 To 30
    If Cells(1, c).Value = "Est #" Then
        Cells(1, c).EntireColumn.Copy Cells(1, "CA")
    End If
Next

End Sub

这是Find方法

Sub test()

Dim rFind As Range

Set rFind = Range("A1").Resize(, 30).Find(What:="Est #", Lookat:=xlWhole, MatchCase:=False, SearchFormat:=False)

If Not rFind Is Nothing Then
    rFind.EntireColumn.Copy Cells(1, "CA")
End If

End Sub

这篇关于VBA循环通过列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆