简体   繁体   English

如果函数宏 - 导致特定列

[英]if function macro - results in a specific column

I have a macro which uses if function. 我有一个使用if函数的宏。 It places the results (1 or 0) to the column specified. 它将结果(1或0)放置到指定的列。 However, I would like to specify the name of the column name. 但是,我想指定列名的名称。 Let's say saying do the if function and bring the values to the column which header is called "Values". 让我们说if do函数并将值带到哪个标题称为“Values”的列中。 Below you can find the original code: 您可以在下面找到原始代码:

Sub bbb()
Dim FormulaCol As Long
Dim LookupCol As Long
Dim TotalRows As Long
Dim TotalCols As Long
Dim i As Long

Sheets("Sheet1").Select
TotalRows = ActiveSheet.UsedRange.Rows.Count
TotalCols = ActiveSheet.UsedRange.Columns.Count

For i = 1 To TotalCols
    If Cells(1, i).Value = "Values" Then FormulaCol = i
    If Cells(1, i).Value = "Test" Then LookupCol = i
Next

ActiveCell.FormulaR1C1 = "=IF(RC[-1]=""United States"",1,0)"
Cells(2, FormulaCol).AutoFill Destination:=Range(Cells(2, FormulaCol), Cells(TotalRows, FormulaCol))
With Range(Cells(2, FormulaCol), Cells(TotalRows, FormulaCol))
    .Value = .Value
End With
End Sub

I know that this part of the code is responsible for static specified column: 我知道这部分代码负责静态指定列:

ActiveCell.FormulaR1C1 = "=IF(RC[-1]=""United States"",1,0)"

But I don't know how I am suppose to change it to make it work as described above. 但我不知道如何更改它以使其如上所述工作。 Could someone help me with this? 有人可以帮我吗?

EDIT 编辑

Here is the image showing what I want to achieve 这是显示我想要实现的图像 在此输入图像描述

Right now this macro works ok if the VALUE column is right to the TEST column. 现在,如果VALUE列适合TEST列,则此宏可以正常工作。 If the VALUE column is situated away from the test column, then it doesn't bring the proper values. 如果VALUE列远离测试列,则它不会带来正确的值。 I would like to make macro to look for the header "value" to identify where exactly value column is located (as for some files it won't be the same column) 我想使宏查找标题“值”以确定值列的确切位置(对于某些文件,它将不是同一列)

EDIT 2: Here is the result which I get using your current code. 编辑2:这是我使用您当前代码的结果。 Even if I moved the VALUE column next to the TEST column it behaves the same. 即使我移动了TEST列旁边的VALUE列,它的行为也是一样的。 When using my code values were populated down from C2 to C9. 使用我的代码时,值从C2填充到C9。 As you can see using your code it brings the value in D1 正如您所看到的,使用您的代码它会带来D1中的值

在此输入图像描述

I am not quite sure if I understood the question but subroutine which changes values in one column based on test column could look like: 我不太确定我是否理解了这个问题,但基于测试列更改一列中的值的子例程可能如下所示:

Sub bbb()
Dim FormulaCol As Long
Dim LookupCol As Long
Dim TotalRows As Long
Dim TotalCols As Long
Dim i As Long

Sheets("Sheet1").Select
TotalRows = ActiveSheet.UsedRange.Rows.Count
TotalCols = ActiveSheet.UsedRange.Columns.Count


For i = 1 To TotalCols
    If Cells(1, i).Value = "Test" Then
        LookupCol = i
        Exit For
    End If
Next

For i = 1 To TotalCols
    If Cells(1, i).Value = "Values" Then
        FormulaCol = i
        Range("A2").Activate
        Debug.Print "=IF(RC[" & CStr(FormulaCol - LookupCol - 1) & "]=""United States"",1,0)"
        ActiveCell.Offset(0, FormulaCol - 1).FormulaR1C1 = "=IF(RC[" & CStr(LookupCol - FormulaCol) & "]=""us"",1,0)"
        Cells(2, FormulaCol).AutoFill Destination:=Range(Cells(2, FormulaCol), Cells(TotalRows, FormulaCol))
        With Range(Cells(2, FormulaCol), Cells(TotalRows, FormulaCol))
        .Value = .Value
        End With
    End If
Next
End Sub

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM