将公式复制到可变目标单元格上的整个列

时间:2018-04-13 18:30:20

标签: excel vba excel-vba

我对VBA很新,有很多方法可以引用一个单元格,我在这里丢失了。

我的excel表附带了交换的列,并且无法保证列将位于上次的位置,但我知道列的总数和列标题名称是一致的。

所以我找到了我的列号:

Dim target As Range
Dim ws As Worksheet

Set ws = ThisWorkbook.ActiveSheet

With ws
   Set target = .Range("A1:M1").Find(What:="Target_Column", LookIn:= xlValues, LookAt: = xlWhole, _ 
MatchCase:=False, SeaarchFormat:=False)

targetCol = target.Column

这为我提供了目标列的索引号。

现在我想将以下功能应用于列" N"在我的工作表中(以下公式假设目标列是列" G":

"=RIGHT(G1,LEN(G1)-10)"

我想使用与下面的脚本类似(或更简单)的内容,但不知道如何实现这一目标:

FinalRow = .Cells(.Rows.Count,1).End(xlUp).Row

.Range(.Cells(2,14), .Cells(FinalRow, 14)).FormulaR1C1 =
"=RIGHT(" &  targetCol & "1, LEN(" & targetCol & "1)-10)"

我希望这个问题足够明确,有人可以指出我正确的方向。

感谢。

3 个答案:

答案 0 :(得分:0)

根据您的描述,您可以尝试这样的事情...... 请记住,根据您的描述,第一个公式将在N2中,将引用G1,N2将引用G2,依此类推。确保公式正确。

Range("N2:N" & FinalRow).Formula = "=RIGHT(" & Cells(1, TargetCol).Address(0, 0) & ",LEN(" & Cells(1, TargetCol).Address(0, 0) & ")-10)"

答案 1 :(得分:0)

您可以使用

.Range(.Cells(2,14), .Cells(FinalRow, 14)).FormulaR1C1 ="=RIGHT(RC" &  targetCol & ", LEN(RC" & targetCol & ")-10)"

答案 2 :(得分:0)

可能会更灵活一些:

Option Explicit

Public Sub testing()
    Dim wb As Workbook, ws As Worksheet, searchRange As Range, targetColumn As Long, lastRow As Long
    Set wb = ThisWorkbook
    Set ws = wb.ActiveSheet                      'change as appropriate

    Const header As String = "MyHeader"          '<====Change to header name trying to find
    Const startFormulaRow As Long = 2            '<=== change for column to start applying formula at.  Assume not 1 as contains header
    Const formulaColumn As Long = 14             '<==== change for column you want to apply formula in
    Const charsToRemove As Long = 10             '<=== change to different number of characters to remove from len
    With ws
        Set searchRange = .Range("A1:M1")        '<===Change to alternative search range
        targetColumn = FindTargetColumn(header, searchRange)

        If targetColumn > 0 Then
            lastRow = GetLastRow(ws, targetColumn, startFormulaRow)
            .Range(.Cells(startFormulaRow, formulaColumn), .Cells(lastRow, formulaColumn)).FormulaR1C1 = "=IFERROR(RIGHT(RC" & targetColumn & ",LEN(RC" & targetColumn & ")-" & charsToRemove & "),"""")"
        End If
    End With
End Sub

Public Function FindTargetColumn(ByVal header As String, ByVal searchRange As Range) As Long
    Dim target As Range
    Set target = searchRange.Find(What:=header, LookIn:=xlValues, LookAt:=xlWhole, _
                                  MatchCase:=False, SearchOrder:=xlRows, SearchFormat:=False)
    If Not target Is Nothing Then
        FindTargetColumn = target.Column
    Else
        FindTargetColumn = -1
    End If
End Function

Public Function GetLastRow(ByVal ws As Worksheet, ByVal targetColumn, ByVal startFormulaRow As Long) As Long
    If Not Application.WorksheetFunction.Subtotal(103, ws.UsedRange) = 0 Then
        GetLastRow = ws.Columns(targetColumn).SpecialCells(xlCellTypeLastCell).Row
    Else
        MsgBox "No data in " & ws.Name & " or last row is < than required formula start row of " & startFormulaRow
        End
    End If
End Function