目标:开发一个宏,我在sheet1中选择,将每第十二行的值除以1.35,并将它们转移到sheet2中选定的单元格范围内。
我开始很简单并且有效:
Worksheets("Sheet2").Cells(114, 12).Formula = Worksheets("Sheet1").Cells(58, 14) / 1.35
Worksheets("Sheet2").Cells(115, 12).Formula = Worksheets("Sheet1").Cells(70, 14) / 1.35
Worksheets("Sheet2").Cells(116, 12).Formula = Worksheets("Sheet1").Cells(82, 14) / 1.35
Worksheets("Sheet2").Cells(117, 12).Formula = Worksheets("Sheet1").Cells(94, 14) / 1.35
Worksheets("Sheet2").Cells(118, 12).Formula = Worksheets("Sheet1").Cells(106, 14) / 1.35
Worksheets("Sheet2").Cells(119, 12).Formula = Worksheets("Sheet1").Cells(118, 14) / 1.35
Worksheets("Sheet2").Cells(120, 12).Formula = Worksheets("Sheet1").Cells(130, 14) / 1.35
Worksheets("Sheet2").Cells(121, 12).Formula = Worksheets("Sheet1").Cells(142, 14) / 1.35
然后我想通过使用循环来细分代码,但无法正确使用它。
这就是我所拥有的:
Dim i As Integer
Dim j As Integer
Dim k As Integer
Dim l As Integer
Do
i = 12
j = 14
For l = 114 To 121
For k = 58 To 142 Step 12
Worksheets("Sheet2").Cells(l, i).Formula = Worksheets("Sheet1").Cells(k, j) / 1.35
Next k
Next l
Loop Until l = 121
我的最终目标是在数据进入时在不同的列上重复此循环。
答案 0 :(得分:0)
我会使用index来执行此操作=INDEX(Sheet1!$N$58:$N$142,(ROWS($A$114:$A114)-1)*12)/1.35
请将您想要划分的列设为N,只需将其放入您尝试在sheet2中填充的单元格中
答案 1 :(得分:0)
诀窍是取出" do - 循环L"。这导致无限循环,因为你永远不会增加" L"。 (使用相同命名变量的for循环,只有在自己的范围内变量...然后它不再是无限的。
答案 2 :(得分:0)
尝试如下:
Dim k, l As Integer
'Set before row
k = 46
For l = 114 To 121 Step 1
Sheets("Sheet2").Cells(l, 12) = Sheets("Sheet1").Cells(k + 12, 14) / 1.35
Next l
我不清楚您的要求,意味着,不知道您要插入什么(公式或价值)。
如果要将公式设置为Sheet2的单元格,请修改设置行,如下所示:
Sheets("Sheet2").Cells(l, 12).Formula = "=" & Sheets("Sheet1").Cells(k + 12, 14).Address & "/1.35"
答案 3 :(得分:0)
这应该有效:
Sub Temp2()
Dim X As Long, ReadCol As Long, UpdateCode As Long
ReadCol = 14
UpdateCol = 12
For X = 114 To 121
Worksheets("Sheet2").Cells(X, UpdateCol).Formula = Worksheets("Sheet1").Cells(58 + ((X - 114) * 12), ReadCol) / 1.35
Next
End Sub
我没有要测试的任何样本数据,所以这里是一个调试版本,它将输出到即时窗口(CTRL-G)整行编译到的内容:
Sub Temp2()
Dim X As Long, ReadCol As Long, UpdateCode As Long
ReadCol = 14
UpdateCol = 12
For X = 114 To 121
Debug.Print "Worksheets(""Sheet2"").Cells(" & X & ", " & UpdateCol & ").Formula = Worksheets(""Sheet1"").Cells(" & 58 + ((X - 114) * 12) & ", " & ReadCol & ") / 1.35"
Next
End Sub
结果:
Worksheets("Sheet2").Cells(114, 12).Formula = Worksheets("Sheet1").Cells(58, 14) / 1.35
Worksheets("Sheet2").Cells(115, 12).Formula = Worksheets("Sheet1").Cells(70, 14) / 1.35
Worksheets("Sheet2").Cells(116, 12).Formula = Worksheets("Sheet1").Cells(82, 14) / 1.35
Worksheets("Sheet2").Cells(117, 12).Formula = Worksheets("Sheet1").Cells(94, 14) / 1.35
Worksheets("Sheet2").Cells(118, 12).Formula = Worksheets("Sheet1").Cells(106, 14) / 1.35
Worksheets("Sheet2").Cells(119, 12).Formula = Worksheets("Sheet1").Cells(118, 14) / 1.35
Worksheets("Sheet2").Cells(120, 12).Formula = Worksheets("Sheet1").Cells(130, 14) / 1.35
Worksheets("Sheet2").Cells(121, 12).Formula = Worksheets("Sheet1").Cells(142, 14) / 1.35