繁体   English   中英

如何使用VB.net保留公式并清除Excel中的内容?

[英]How to keep the formulas and clear the content in Excel using VB.net?

实现目标:

  • 我有一系列要替换为新数据的数据。

  • 因此,我想清除旧数据并将其替换为新数据。

  • 但是,旧数据中的公式也必须适用于新数据。

我做了以下事情:

Private Sub updateData(ByRef sheet As Excel.Worksheet, ByVal dataRow As String, ByRef data As Object)
    Dim range As Excel.Range
    downRange(sheet, dataRow).ClearContents()  // Note this !!
    // Both data and formulas are lost due to this.
    // What shall I use instead of this to retain the formulas in the first row.
    range = sheet.Range(dataRow.Substring(0, dataRow.Length - 1) + (data.GetLength(0) + 1).ToString())
    range.Value2 = data
    // If the 1st row still has the formulas, then I can do AutoFill for this new data.
End Sub

Private Function downRange(ByRef sheet As Excel.Worksheet, ByVal rangeString As String)
    Dim range As Excel.Range
    range = sheet.Range(rangeString)
    range = sheet.Range(range, range.End(Excel.XlDirection.xlDown))
    Return range
End Function

但是,问题是:

  • 公式迷路了。 (明显)
  • 我想将公式保留在第一行中,以便随后可以执行自动填充
  • 您能提出一些解决方案吗?

所需结果的演示:

旧数据:

  |   A      |  B  |  C  |
1 |   H1     |  H2 |  H3 |
2 |   =B2+C2 |  5  |  9  |   Therefore, A2 = 14
3 |   =B3+C3 |  7  |  2  |   Therefore, A3 = 9

我想替换数据并保留公式,以便(新数据):

  |   A      |  B  |  C  |
1 |   H1     |  H2 |  H3 |
2 |   =B2+C2 |  4  |  6  |   Therefore, A2 = 10
3 |   =B3+C3 |  3  |  5  |   Therefore, A3 = 8

给定的调用是updateData(sheet, "A2:C2", dataFromDB)

如何保存公式但不更改函数调用即可更改数据?

我有一个类似的问题,在清除单元格时使用了此逻辑。 下面的代码段清除了第5行(第5行)中第2-1000行的AC和F列中的单元格,当您希望对要清除的内容进行精细控制时,请使用此功能。

Const clearCells = "A?:C?,F?"

Sub clearCells()
For i = 2 To 1000
If i <> 5 Then
Range(Replace(clearCells, "?", i)).ClearContents
End If
Next i
End Sub

一种更简单的方法是从2-2000行清除AC和F列:

    Range ("A2:C2000,F2:F2000").ClearContents

最后,要重写程序,您可以在范围内循环以实例化新范围,并仅在没有以下公式的情况下才将单元格添加到范围中:

Private Function downRange(ByRef sheet As Excel.Worksheet, ByVal rangeString As String)
Dim inRange As Excel.range
Dim outRange As range
Set inRange = sheet.range(rangeString)
Set inRange = sheet.range(inRange, inRange.End(Excel.XlDirection.xlDown))
Dim r As range
For Each r In inRange
  If Left(r.Formula, 1) <> "=" Then
    If outRange Is Nothing Then
    Set outRange = r
    Else
    Set outRange = Application.Union(outRange, r)
    End If
  End If
Next r
downRange = outRange
End Function

如果要将公式保留在第一行中,请不要选择要删除的第一行。 "A3:F3"传递给函数。

对于自动填充,请使用Range.FormulaR1C1属性。 从第一个单元格读取FormulaR1C1 ,然后在其余单元格中将相同的值写入FormulaR1C1

我认为您已经快到了-好像您只需要从顶部向下清除一行即可。

假设您可以访问Range.Offset方法(我没有一种简单的方法可以从Vb.Net进行测试,但我认为没有理由不应该使用它),那么您应该能够移动起始单元格:

Private Function downRange(ByRef sheet As Excel.Worksheet, ByVal rangeString As String)
    Dim range As Excel.Range
    range = sheet.Range(rangeString).Offset(1, 0) // Here's the extra piece
    range = sheet.Range(range, range.End(Excel.XlDirection.xlDown))
    Return range
End Function

暂无
暂无

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

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