简体   繁体   中英

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. 1700 cells.

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. Note that the $F$7 part changes in each cell.

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)
  • column 2 to 25

My first idea was to use python to achieve this... but I couldn't make it work with pyoo , oosheet or uno . So I came to Excel and was trying to write a macro that does the following:

Macro Idea:

  1. for column i = 2 to 25,
  2. get Cell(1, i).Value ,
  3. for row j = 5 to 96,
  4. get Cell(j, i).Formula
  5. replace the '2014-12' part in Cell(j, i).Formula , with Cell(1, i).Value
  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 .

The main thing you needed to learn was the existence of the Mid function, or the Replace function. 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...

If you write, in cell A5, =A$1 , it will (obviously) refer to 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.

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.

Next we can write the address of the cell on the other sheet using the Address function.

=Address(1, 1, 4, True, Text(C$1, "YYYY-MM")) will produce the text '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:

=Address(Row(), Column(), 4, True, Text(C$1, "YYYY-MM")) will produce '2015-12'!C5 in cell 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!

=Address(Row() + 2, Column() + 3, 4, True, Text(C$1, "YYYY-MM")) will produce '2015-12'!F7 in cell C5.

Finally, turn the text from Address into a lookup using the Indirect function:

=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.

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