简体   繁体   English

使用另一个单元格的值动态替换单元格公式的一部分

[英]Replace part of a cell formula dynamically using value of another cell

I'm having a hard time formatting a spreadsheet... maybe you guys can help me. 我在格式化电子表格时遇到了麻烦,也许你们可以帮助我。

I need to replace part of a formula in aprox. 我需要替换aprox中的部分公式。 1700 cells. 1700格。

The original formula present in each of these cells respects the following format: 这些单元格中每个单元格中存在的原始公式遵循以下格式:

='2014-12'!$F$7

I'm interested in replacing the '2014-12' part of the formula with the value of the cell in the first row of each colum. 我有兴趣将公式的'2014-12'部分替换为每个列第一行中单元格的值。 Note that the $F$7 part changes in each cell. 请注意, $F$7部分在每个单元格中都会更改。

Ex.: 例如:

在此处输入图片说明

As you can see, the first row of my spreadsheet contains the year that I must replace inside the formulas that are written in the cells right above the year. 如您所见,电子表格的第一行包含我必须替换的年份,该年份必须在年份上方的单元格中编写的公式内。

Data goes from: 数据来自:

  • row 5 to 96 (not every cell in the column contains formulas) 第5到96行(并非该列中的每个单元格都包含公式)
  • column 2 to 25 第2至25栏

My first idea was to use python to achieve this... but I couldn't make it work with pyoo , oosheet or uno . 我的第一个想法是使用python来实现这一目标...但是我无法使其与pyoooosheetuno So I came to Excel and was trying to write a macro that does the following: 因此,我来​​到Excel并试图编写一个执行以下操作的宏:

Macro Idea: 宏观思路:

  1. for column i = 2 to 25, 对于第i列= 2至25,
  2. get Cell(1, i).Value , 获取Cell(1, i).Value
  3. for row j = 5 to 96, 对于第j行= 5至96,
  4. get Cell(j, i).Formula 得到Cell(j, i).Formula
  5. replace the '2014-12' part in Cell(j, i).Formula , with Cell(1, i).Value Cell(1, i).Value替换Cell(j, i).Formula的'2014-12'部分
  6. done 完成

在此处输入图片说明


My code so far: 到目前为止,我的代码:

Sub replace_content_of_formula()

Dim i, j As Integer

For i = 2 To 25
    year = Cells(1, i).Value

    For j = 5 To 96
        prev_formula = Cells(j, i).Formula
        new_formula = prev_formula[:2] & year & prev_formula[9:] <<< I DONT KNOW HOW TO DO THIS
        Cells(j, i).Select
        ActiveCell.FormulaR1C1 = new_formula <<< I DONT KNOW HOW TO DO THIS

    Next j
Next i
End Sub

I would appreciate any help. 我将不胜感激任何帮助。

Ps.: I can use python or vba . 附言:我可以使用pythonvba

The main thing you needed to learn was the existence of the Mid function, or the Replace function. 您需要学习的主要内容是Mid函数或Replace函数的存在。 Revised code is as follows: 修改后的代码如下:

Sub replace_content_of_formula()
    Dim i As Integer, j As Integer
    Dim prev_formula As String
    Dim new_formula As String
    Dim YearValue As String

    For i = 2 To 25
        YearValue = Cells(1, i).Value

        For j = 5 To 96
            prev_formula = Cells(j, i).FormulaR1C1
            new_formula = Replace(prev_formula, "2014-12", YearValue)
            'Or
            'new_formula = Mid(prev_formula, 1, 2) & YearValue & Mid(prev_formula, 10)
            Cells(j, i).FormulaR1C1 = new_formula
        Next j
    Next i
End Sub

I'm not at a computer with Windows installed, but this is working in LibreOffice Calc... 我不在安装Windows的计算机上,但是可以在LibreOffice Calc中使用...

If you write, in cell A5, =A$1 , it will (obviously) refer to A1. 如果在单元格A5中写入=A$1 ,它将(显然)引用A1。 If you copy and paste this over your sheet, it will be pasted as =B$1 in column B, =C$1 in column C etc. So it will always look up the first row. 如果将其复制并粘贴到工作表上,它将在B列中粘贴为=B$1=C$1列中粘贴为=C$1 ,依此类推。因此,它将始终查找第一行。

If the first row is formatted dates rather than text, they can be converted to text using =Text(C$1, "YYYY-MM") to match the sheet names. 如果第一行是格式化的日期而不是文本,则可以使用=Text(C$1, "YYYY-MM")将其转换为文本以匹配工作表名称。

Next we can write the address of the cell on the other sheet using the Address function. 接下来,我们可以使用“ Address功能将单元格的地址写在另一张纸上。

=Address(1, 1, 4, True, Text(C$1, "YYYY-MM")) will produce the text '2015-12'!A1 =Address(1, 1, 4, True, Text(C$1, "YYYY-MM"))将产生文本 '2015-12'!A1

=Address(1, // the row
         1, // the column
         4, // absolute or relative format. A number from 1 to 4. 4 is relative row & relative column
         True, // A1 or R1C1 format. True is A1
         Text(C$1, "YYYY-MM") // the sheet name
         )

If we use the Row() and Column() functions for the first two arguments the output of Address will refer to the same cell on another sheet: 如果我们对前两个参数使用Row()Column()函数,则Address的输出将引用另一张纸上的同一单元格:

=Address(Row(), Column(), 4, True, Text(C$1, "YYYY-MM")) will produce '2015-12'!C5 in cell C5. =Address(Row(), Column(), 4, True, Text(C$1, "YYYY-MM"))将在单元格C5中生成'2015-12'!C5 C5。

You can then add the offsets by adding to Row() and Column() - hopefully these offsets are the same for all the cells you're looking up! 然后,您可以通过添加到Row()Column()来添加偏移量-希望这些偏移量对于您要查找的所有单元格都相同!

=Address(Row() + 2, Column() + 3, 4, True, Text(C$1, "YYYY-MM")) will produce '2015-12'!F7 in cell C5. =Address(Row() + 2, Column() + 3, 4, True, Text(C$1, "YYYY-MM"))将在单元格C5中生成'2015-12'!F7

Finally, turn the text from Address into a lookup using the Indirect function: 最后,使用Indirect功能将文本从“ Address转换为查找:

=Indirect(Address(Row() + 2, Column() + 3, 4, True, Text(C$1, "YYYY-MM")))

You should then be able to copy-paste this formula over your sheet. 然后,您应该可以将此公式复制粘贴到工作表上。

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

相关问题 用 openpyxl 用空白替换单元格值的一部分 - Replace part of a cell value with blank with openpyxl openpyxl():读取单元格值而不是单元格公式 - openpyxl(): Reading the cell value not the cell formula 使用openpyxl将公式从一个单元格复制到另一个单元格 - Copy formula from one cell to another using openpyxl 是否可以使用python复制单元格的Excel公式而不是值? - Is it possible to copy the Excel formula of a cell instead of the value using python? 使用xlrd时,读取包含公式的单元格值将返回0.0 - Reading a cell value that contains a formula returns 0.0 when using xlrd 使用 Python 读取单元格值,而不是 Excel 工作表中的公式 - Read cell value, NOT the formula in Excel sheet using Python 将单元格中的 id 列表替换为匹配值 id 另一个 dataframe (pandas) - Replace list of id in cell by matching value id another dataframe (pandas) 如果满足单独单元格的条件,则将单元格的内容替换为另一个单元格 - Replace contents of cell with another cell if condition on a separate cell is met 如何使用python更改一个Excel单元格值并使用公式读取与此单元格相关的列值? - How to change one Excel cell value using python and read a column values related to this cell with formula? 从另一个单元格给一个单元格赋值 - Assign a value to a cell from another cell
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM