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

时间:2011-08-02 06:04:44

标签: .net vb.net excel excel-vba export-to-excel vba

旨在实现:

  
      
  • 我想要用新数据替换一系列数据。

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

  •   
  • 但旧数据中的公式也需要处理新数据。

  •   

我做了以下事情:

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)

     

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

3 个答案:

答案 0 :(得分:1)

当我清除细胞时,我遇到了类似的问题并使用了这个逻辑。下面的片段清除了柱子A-C& C中的细胞。除了行5之外的行2-1000中的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行清除A-C和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

答案 1 :(得分:0)

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

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

答案 2 :(得分:0)

我认为你几乎就在那里 - 看起来你只需要从顶部开始清理一行。

假设您可以访问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