简体   繁体   English

列中每个单元格的公式中的单元格引用增量

[英]Increment Cell Ref in Formula for each Cell within a Column

Forgive me for my ignorance but I need help with the following code 原谅我的无知,但我需要以下代码的帮助

Sub D3()
Dim currentRow As String
Dim sTemp As String

sTemp = Range("D3").Formula 'cell in sheet that needs reference to be increased

Do While (IsNumeric(Right(sTemp, 1)))

    currentRow = Right(sTemp, 1) & currentRow
    sTemp = Mid(sTemp, 1, Len(sTemp) - 1)

Loop

currentRow = CLng(currentRow) + 1 'Number to increment cell reference by

Range("D3").Formula = sTemp & currentRow

End Sub

As I've forgotten most of my coding and for what I thought was a simple solution I went to google and got the code above. 由于我忘记了大部分编码,并且我认为这是一个简单的解决方案,因此我去了google,并获得了上面的代码。 It works excellently on a single cell but I want to be able to update the formula in a range of cells from D3 to D29 and this is where i've become stuck. 它在单个单元格上表现出色,但我希望能够在从D3到D29的一系列单元格中更新公式,这就是我陷入困境的地方。

I've tried 我试过了

sTemp = Range("D3:D29").Formula

with no luck. 没有运气。

The only way I can currently get what I want is to create 120ish Macros and run them within a Master Macro - which I'm sure you'll agree isn't very efficent. 我目前所能获得的唯一方法是创建120ish宏并在主宏中运行它们-我敢肯定,您会认为效率不高。

I want to be able to apply the solution easily to other columns within the sheet. 我希望能够轻松地将解决方案应用于工作表中的其他列。

Thanks 谢谢

Ignoring the reason why you want to do this. 忽略您要执行此操作的原因。 I guess what you are missing is you just need to loop your code. 我想您所缺少的只是您需要循环代码。

I guess you know the range that is D3:D29. 我猜您知道范围是D3:D29。 Modifying your code as below 如下修改代码

Sub D3()
Dim currentRow As String
Dim sTemp As String
Dim i as Integer


For i = 3 to 29

currentRow = ""

sTemp = Range("D" & i).Formula 'cell in sheet that needs reference to be increased

Do While (IsNumeric(Right(sTemp, 1)))

    currentRow = Right(sTemp, 1) & currentRow
    sTemp = Mid(sTemp, 1, Len(sTemp) - 1)

Loop

currentRow = CLng(currentRow) + 1 'Number to increment cell reference by

Range("D" & i).Formula = sTemp & currentRow

Next i
End Sub

If I have understood your intent correctly, the following code should do the trick. 如果我正确理解了您的意图,则可以使用以下代码来解决问题。 It presumes that the formulas in the target range are single cell references (eg, "=CC95").* 假定目标范围内的公式是单个单元格引用(例如,“ = CC95”)。*

I've eliminated the type conversion errors that prevented the code from compiling. 我已经消除了导致代码无法编译的类型转换错误。 In addition, I've rewritten the somewhat confused code to increment the row numbers. 另外,我还重写了有些混乱的代码以增加行号。

To run the procedure on other columns, just change the values of the targetColumn, firstTargetRow, and lastTargetRow variables. 要在其他列上运行该过程,只需更改targetColumn,firstTargetRow和lastTargetRow变量的值即可。

Sub IncrementFormulaRows()
   Dim formulaString As String
   Dim targetColumn As String
   Dim firstTargetRow As Long
   Dim lastTargetRow As Long
   Dim i As Long
   Dim j As Long

   targetColumn = "D"
   firstTargetRow = 3
   lastTargetRow = 29

   For i = firstTargetRow To lastTargetRow
      If Range(targetColumn & i).HasFormula Then          '// Skip cell if it does not 
         formulaString = Range(targetColumn & i).Formula  '// contain a formula
         j = Len(formulaString)
         While (j > 0) And IsNumeric(Mid(formulaString, j, 1))  '// Walk formula backwards 
            j = j - 1                                           '// to find last char of  
         Wend                                                   '// column reference
         Range(targetColumn & i).Formula = Left(formulaString, j) _
            & (Val(Mid(formulaString, j + 1, Len(formulaString) - j)) + 1)
      End If
   Next i
End Sub

*The formula will actually run when a more complicated formula is used, as long as it ends in a single cell reference. *只要使用更复杂的公式,该公式实际上就会运行,只要它以单个单元格引用结尾即可。 However, I assume the result would be undesirable. 但是,我认为结果不理想。

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

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