我正在尝试为个人预算工作表创建一个宏,其中有一个单元格繁琐的工作。
使用"录制宏"按钮我跟踪了我在单元格中进行的操作后面的编码,如下所示:
Sub calculo_otherex()
' calculo_otherex Macro
' Cálculo de otros gastos con tarjeta.
'
ActiveCell.FormulaR1C1 = _
"=Extractos!R[-89]C[-2]-('Home Expenses'!R[-105]C+'Home Expenses'!R[-60]C+'Home Expenses'!R[-16]C+Health!R[-105]C+Health!R[-67]C+Health!R[-29]C+Gifts!R[-99]C+'Daily Living'!R[-87]C+'Daily Living'!R[-29]C+'Daily Living'!R[20]C+'Daily Living'!R[59]C+'Daily Living'!R[102]C+Entertainment!R[-105]C+Entertainment!R[-67]C+Entertainment!R[-29]C+Entertainment!R[9]C+Transportation!R[-105]C)"
Range("H129").Select
End Sub
正如您所看到的,在公式中我指的是其他表格,以及反映特定月份(在这种情况下为7月份)和特定子集(向信用卡收费)的特定单元格。
我想要做的是插入一个循环,所以这个过程重复了几个月,但问题是表格中的数据" Extractos"出现在#34; Extractos!R [-89] C [-2]"右侧的每第7个单元格,因为公式中的其他数据出现在下一列中。
我怎么能解决这个问题? 非常感谢你。
答案 0 :(得分:0)
尝试以下内容......很难读取R1C1,所以我稍微改了一下,你需要将我所有的“A1”引用替换为第一个月的单元格引用。此外,for loop
只会循环12次 - 一年12个月。如果您愿意,可以手动更改此设置。最后,公式将写在activecell
中,然后下面的公式将写在右边的下一个单元格/列中。
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'instead of using R1C1, use ranges with offsets - it's much easier to read :) ''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'I used "A1" as a placeholder. Replace this with the actual cell reference for the first month
'The first loop will use the "A1" range. Then, the 2nd and following loops will move over 1 or 7 columns
'i = move over 1 column
'x = move over 7 columns
For i = 0 To 11 'this will loop through 12 times 0-11
ActiveCell.Offset(0, i).Formula = _
"=Extractos!" & Range("A1").Offset(0, x).Address _
& "-('Home Expenses'!" & Range("A1").Offset(0, i).Address _
& "+'Home Expenses'!" & Range("A1").Offset(0, i).Address _
& "+'Home Expenses'!" & Range("A1").Offset(0, i).Address _
& "+Health!" & Range("A1").Offset(0, i).Address _
& "+Health!" & Range("A1").Offset(0, i).Address _
& "+Health!" & Range("A1").Offset(0, i).Address _
& "+Gifts!" & Range("A1").Offset(0, i).Address _
& "+'Daily Living'!" & Range("A1").Offset(0, i).Address _
& "+'Daily Living'!" & Range("A1").Offset(0, i).Address _
& "+'Daily Living'!" & Range("A1").Offset(0, i).Address _
& "+'Daily Living'!" & Range("A1").Offset(0, i).Address _
& "+'Daily Living'!" & Range("A1").Offset(0, i).Address _
& "+Entertainment!" & Range("A1").Offset(0, i).Address _
& "+Entertainment!" & Range("A1").Offset(0, i).Address _
& "+Entertainment!" & Range("A1").Offset(0, i).Address _
& "+Entertainment!" & Range("A1").Offset(0, i).Address _
& "+Transportation!" & Range("A1").Offset(0, i).Address & ")"
'add 7 to x variable
x = x + 7
'loop and add 1 to i
Next i
'macro done, select H129
Range("H129").Select