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

Vba 将公式复制到变量目标单元格上的整列,vba,excel,Vba,Excel,我对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:

我对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”列):

我想使用与下面的脚本类似(或更简单)的东西,但不知道如何实现:

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

.Range(.Cells(2,14), .Cells(FinalRow, 14)).FormulaR1C1 =
"=RIGHT(" &  targetCol & "1, LEN(" & targetCol & "1)-10)"
我希望这个问题足够清楚,有人能给我指出正确的方向


谢谢。

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

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

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

也许是更灵活的方式,比如:

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
选项显式
公共子测试()
将wb作为工作簿、ws作为工作表、searchRange作为范围、targetColumn作为长度、lastRow作为长度
设置wb=ThisWorkbook
根据需要设置ws=wb.ActiveSheet的更改

Const header As String=“MyHeader”“这是在处理别人发送给你的模板吗?如果是的话,你有什么理由不简单地修改模板,这样当你发送模板时,这个公式就已经在模板中了吗?也许在一个隐藏的列中?诚然,这可能需要更多的错误处理,但我一直在玩弄一种更可重用的想法。任何提要请回来好吗?
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