VBA定义一个variables的属性

我是新来的论坛,也是VBA的新手(一个星期前才开始学习)。

是否有可能在variables中定义一个属性? 我有一条线,看起来像这样:

Cells(i, j).Formula = "=" & .Cells(i, j).Address(External:=True, RowAbsolute:=False, ColumnAbsolute:=False) 

我必须写几个单元格,所以我最终得到了许多这样的行; 如果可能的话,我想做一些事情:

 dim prop as string prop = "Address(External:=True, RowAbsolute:=False, ColumnAbsolute:=False)" Cells(i, j).Formula = "=" & .Cells(i, j).prop 

可以这样做吗?

你是这个意思吗?

 Function MyProp(ByVal r as Range) as String MyProp = r.Address(External:=True, RowAbsolute:=False, ColumnAbsolute:=False) End Function ... Cells(i,j).Formula = "="& MyProp( Cells(i,j) ) 

这当然会使单元格引用本身,这通常是一个坏主意,并可能导致循环引用。 最好的,如果你更详细地描述你想要得到更好的答案。

正如评论中提到的,这不能以你描述的方式来完成。 有两种select:

  1. 写一个私人的function,你调用你的input和期望的输出(更复杂)
  2. 编写一个只出现一次的代码循环。 (less代码)

要为所有单元格从A1写入循环到上次使用的行,最后使用的列:

 Dim LastRow As Long, LastCol As Long LastRow = Range("A" & Rows.Count).End(xlUp).Row LastCol = Cells(1, Columns.Count).End(xlToLeft).Column For i = 1 To LastRow For j = 1 to LastCol `Do your stuff here Next j Next i 

编辑:我假设你需要投资在这里find的Range.Offset()方法,因为现在,你设置单元格的公式等于自己…