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

时间:2016-12-10 20:55:48

标签: python excel vba excel-vba

我很难格式化电子表格......也许你们可以帮助我。

我需要在aprox中替换部分公式。 1700个细胞。

每个单元格中的原始公式都遵循以下格式:

='2014-12'!$F$7

我有兴趣用每个列的第一行中的单元格值替换公式的'2014-12'部分。请注意,每个单元格中$F$7部分都会发生变化。

例:

enter image description here

正如您所看到的,我的电子表格的第一行包含我必须在一年中上面的单元格中写入的公式中替换的年份。

数据来自:

  • 第5行到第96行(并非列中的每个单元格都包含公式)
  • 第2至25栏

我的第一个想法是使用python实现这一点......但我无法使用pyoooosheetuno。所以我来到Excel并尝试编写一个执行以下操作的宏:

宏观想法:

  1. 列i = 2至25,
  2. 获取Cell(1, i).Value
  3. 对于行j = 5到96,
  4. 获取Cell(j, i).Formula
  5. 使用Cell(j, i).Formula
  6. 替换Cell(1, i).Value中的“2014-12”部分
  7. 完成
  8. enter image description here

    到目前为止我的代码:

    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
    

    我将不胜感激。

    Ps。:我可以使用pythonvba

2 个答案:

答案 0 :(得分:3)

您需要学习的主要内容是Mid函数或Replace函数的存在。修改后的代码如下:

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

答案 1 :(得分:2)

我没有安装Windows的计算机,但这在LibreOffice Calc中有效...

如果你在单元格A5中写 try{ String url = textBox1.Text; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); StreamReader sr = new StreamReader(response.GetResponseStream()); HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument(); doc.Load(sr); var aTags = doc.DocumentNode.SelectNodes("//a"); int counter = 1; if (aTags != null) { foreach (var aTag in aTags) { richTextBox1.Text += aTag.InnerHtml + "\n" ; counter++; } } sr.Close(); } catch (Exception ex) { MessageBox.Show("Failed to retrieve related keywords." + ex); } ,它(显然)会引用A1。如果您将其复制并粘贴到工作表上,则会将其粘贴到B列中的=A$1,C列中的=B$1等。因此,它始终会查找第一行。

如果第一行是格式化日期而不是文本,则可以使用=C$1将它们转换为文本以匹配工作表名称。

接下来,我们可以使用=Text(C$1, "YYYY-MM")函数在另一张纸上写入单元格的地址。

Address将生成文本 =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 ) Row()函数,Column()的输出将引用另一个工作表上的相同单元格:

Address将在单元格C5中生成=Address(Row(), Column(), 4, True, Text(C$1, "YYYY-MM"))

然后,您可以通过添加到'2015-12'!C5Row()来添加偏移量 - 希望这些偏移对于您正在查找的所有单元格都是相同的!

Column()将在单元格C5中生成=Address(Row() + 2, Column() + 3, 4, True, Text(C$1, "YYYY-MM"))

最后,使用'2015-12'!F7函数将文本从Address转换为查找:

Indirect

然后,您应该可以将此公式复制粘贴到工作表上。

相关问题