简体   繁体   中英

if function macro - results in a specific column

I have a macro which uses if function. It places the results (1 or 0) to the column specified. 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". 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. If the VALUE column is situated away from the test column, then it doesn't bring the proper values. 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. Even if I moved the VALUE column next to the TEST column it behaves the same. When using my code values were populated down from C2 to C9. As you can see using your code it brings the value in 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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