Excel VBA如果满足条件,则尝试将范围设置为列并调用数据

时间:2014-09-25 16:32:14

标签: excel vba excel-vba

Dim range_row_aa As Range
Set range_row_aa = Worksheets("Accretion Amort").Range("1:1")

Dim name_column As Range
Set name_column = Worksheets("Accretion Amort").Columns("A:Z")

Dim column_x As Object 
For Each column_x In range_row_aa
  If column_x = "Balance Change Diff" Then
    Sheets("Recon").Range("J3") = "OKKKKK"
  End If
Next

如果吸积量表上的列等于余额变化差异,我想将该列中的数据复制/提取到重新分析表。

在这种情况下,阵列会更有帮助吗?也许是一个函数,因为我计划分析吸积量表中的每一列,如果列标题(所有列标题都在第1行)中提取列数据,则会满足标准。

(请注意print" OKKKKK"语句只是一个占位符来测试For / IF循环是否有效)

1 个答案:

答案 0 :(得分:0)

这是循环第1行的一些代码:

Sub loopThroughRow1()

Dim range_row_aa As Range
Set range_row_aa = Worksheets("Accretion Amort").Rows("1:1")

Dim cell As Object
For Each cell In range_row_aa.Cells
  If cell.Value = "Balance Change Diff" Then
    Sheets("Accretion Amort").Range("J3") = cell.Address
  End If
Next

End Sub

我已使用cell.address作为占位符,因此我们知道它找到了"余额变化差异的最后一个实例"

这将使用剪贴板将值移动到第二个工作表:

Sub loopThroughRow1()

Dim range_row_aa As Range
Set range_row_aa = Worksheets("Accretion Amort").Rows("1:1")

Dim x As Integer
Dim cell As Object
For Each cell In range_row_aa.Cells
  If cell.Value = "Balance Change Diff" Then

    cell.EntireColumn.Copy
    x = Sheets("recon").Cells(1, Columns.Count).End(xlToLeft).Column
    Sheets("recon").Cells(1, x + 1).PasteSpecial Paste:=xlPasteValues

  End If
Next

End Sub