一次将多个单元格引用从相对($)更改为绝对($)

时间:2018-07-04 08:27:13

标签: excel excel-vba vba

我的Excel电子表格中具有以下值和公式:

          A                             
1      Shirt         =Sheet2!A2
2        50          =C1
3       350          =Sheet3!D7
4    Product B       =F8
5
6

我仅使用 相对单元格引用从 A列中的值从Excel文件的其他部分获取。

现在,我想将这些单元格引用相对于绝对进行更改,因此它们看起来像这样:

          A                             
1      Shirt         =Sheet2!$A$2
2        50          =$C$1
3       350          =Sheet3!$D$7
4    Product B       =$F$8
5
6

因此,我尝试从here开始使用VBA:

Sub Test()
ActiveCell.Formula = Application.ConvertFormula(ActiveCell.Formula, xlA1, xlA1, 1)
End Sub

但是,它仅适用于活动单元格,但我希望VBA贯穿整个工作表。因此,我正在尝试这样的事情:

Sub Test()
Sheet1.Formula = Application.ConvertFormula(Sheet1.Formula, xlA1, xlA1, 1)
End Sub

现在我得到错误“找不到对象或方法”。 我必须如何更改VBA才能使其正常工作?

4 个答案:

答案 0 :(得分:3)

无法一次更改。需要对单个公式单元格进行循环。

您可以使用Range.SpecialCells Method获得特殊单元格。因此,您可以获取所有公式单元格,然后遍历它们并将其转换为绝对值:

Sub convertAllSheetFormulasToAbsolute()
 Dim oCell As Range
 With ActiveSheet
  For Each oCell In .Cells.SpecialCells(Type:=xlCellTypeFormulas)
   oCell.Formula = Application.ConvertFormula(oCell.Formula, xlA1, xlA1, xlAbsolute)
  Next
 End With
End Sub

答案 1 :(得分:1)

子公式_To_AbsoluteReference() 将 oCell 调暗为范围 有选择 对于 .Cells.SpecialCells(Type:=xlCellTypeFormulas) 中的每个 oCell oCell.Formula = Application.ConvertFormula(oCell.Formula, xlA1, xlA1, xlAbsolute) 下一个 结束于 结束子

子公式_To_LockRowNumber_RelColumnLetter() 将 oCell 调暗为范围 有选择 对于 .Cells.SpecialCells(Type:=xlCellTypeFormulas) 中的每个 oCell oCell.Formula = Application.ConvertFormula(oCell.Formula, xlA1, xlA1, xlAbsRowRelColumn) 下一个 结束于 结束子

子公式To_LockColLetter_RelRowNum() 将 oCell 调暗为范围 有选择 对于 .Cells.SpecialCells(Type:=xlCellTypeFormulas) 中的每个 oCell oCell.Formula = Application.ConvertFormula(oCell.Formula, xlA1, xlA1, xlRelRowAbsColumn) 下一个 结束于 结束子

子公式To_AllRealative_Reference() 将 oCell 调暗为范围 有选择 对于 .Cells.SpecialCells(Type:=xlCellTypeFormulas) 中的每个 oCell oCell.Formula = Application.ConvertFormula(oCell.Formula, xlA1, xlA1, xlRelative) 下一个 结束于 结束子

答案 2 :(得分:0)

作为替代方案,ASAP Utilities for Excel加载项提​​供了一个菜单命令,该命令可将您选择的单元格范围转换为绝对引用,混合引用或相对引用。对于我来说,它多年来一直无懈可击。

一旦安装了外接程序,在“公式”菜单中,选择“更改公式引用样式(例如,将A1更改为$ A1等)。

答案 3 :(得分:0)

Sub Formulas_To_AbsoluteReference()
 Dim oCell As Range
 With Selection
  For Each oCell In .Cells.SpecialCells(Type:=xlCellTypeFormulas)
   oCell.Formula = Application.ConvertFormula(oCell.Formula, xlA1, xlA1, xlAbsolute)
  Next
 End With
End Sub


Sub Formulas_To_LockRowNumber_RelColumnLetter()
 Dim oCell As Range
 With Selection
  For Each oCell In .Cells.SpecialCells(Type:=xlCellTypeFormulas)
   oCell.Formula = Application.ConvertFormula(oCell.Formula, xlA1, xlA1, xlAbsRowRelColumn)
  Next
 End With
End Sub


Sub FormulasTo_LockColLetter_RelRowNum()
 Dim oCell As Range
 With Selection
  For Each oCell In .Cells.SpecialCells(Type:=xlCellTypeFormulas)
   oCell.Formula = Application.ConvertFormula(oCell.Formula, xlA1, xlA1, xlRelRowAbsColumn)
  Next
 End With
End Sub


Sub FormulasTo_AllRealative_Reference()
 Dim oCell As Range
 With Selection
  For Each oCell In .Cells.SpecialCells(Type:=xlCellTypeFormulas)
   oCell.Formula = Application.ConvertFormula(oCell.Formula, xlA1, xlA1, xlRelative)
  Next
 End With
End Sub


`'building on my friend above - use this, with SELECTION:

Sub Formulas_To_AbsoluteReference()
 Dim oCell As Range
 With Selection
  For Each oCell In .Cells.SpecialCells(Type:=xlCellTypeFormulas)
   oCell.Formula = Application.ConvertFormula(oCell.Formula, xlA1, xlA1, xlAbsolute)
  Next
 End With
End Sub


Sub Formulas_To_LockRowNumber_RelColumnLetter()
 Dim oCell As Range
 With Selection
  For Each oCell In .Cells.SpecialCells(Type:=xlCellTypeFormulas)
   oCell.Formula = Application.ConvertFormula(oCell.Formula, xlA1, xlA1, xlAbsRowRelColumn)
  Next
 End With
End Sub


Sub FormulasTo_LockColLetter_RelRowNum()
 Dim oCell As Range
 With Selection
  For Each oCell In .Cells.SpecialCells(Type:=xlCellTypeFormulas)
   oCell.Formula = Application.ConvertFormula(oCell.Formula, xlA1, xlA1, xlRelRowAbsColumn)
  Next
 End With
End Sub


Sub FormulasTo_AllRealative_Reference()
 Dim oCell As Range
 With Selection
  For Each oCell In .Cells.SpecialCells(Type:=xlCellTypeFormulas)
   oCell.Formula = Application.ConvertFormula(oCell.Formula, xlA1, xlA1, xlRelative)
  Next
 End With
End Sub