插入公式w / FOR NEXT循环

找不到可以回答我的问题的线程,我在这里。 如果有一个我没有find,请提供一个链接。

使用Excel 2010评论应该足以显示我正在尝试做什么。 我得到了应用程序定义或对象定义的错误(运行时错误1004)我只是想出来。 任何帮助,将不胜感激。 注意:当公式如= if(A1 = B1,“”,3)时,我可以使循环工作如果实际将公式放置在单元格中,并在条件满足时在每个单元格中显示3。 它只是添加列,行,我已经试过&范围($,C)&,各种各样的组合,所以,告诉我多less我胸部和帮助我逃避我的简单修复。

提前致谢。

Private Sub CommandButton1_Click()

Dim R, C As Integer Dim Frmla1, Frmla2, Frmla3 As String R = 4 'Initial Row # C = 2 'Initial Column # Frmla1 = "=IF('Log Sheet'!" '1st half of the formula Frmla2 = "="""","""",'Log Sheet'!" '2nd half of the formula Frmla3 = ")" 'Closing Parenthesis ' The Cells should have incremental Column,Row Identifiers. ' The following is what I want in each cell. ' The problem is trying to get the B4 and B5 into the formula. ' Formula "=IF('Log Sheet'!B4="","",'Log Sheet'!B4) ' Formula "=IF('Log Sheet'!B5="","",'Log Sheet'!B5) For R = 4 To 301 ActiveSheet.Cells(R, C).Value = Frmla1 & R & C & Frmla2 & R & C & Frmla3 R = R + 2 Next R 

结束小组

它看起来像你应该使用RC来引用一个Range.Cells属性 ,你可以返回一个Range.Address属性 。

  For R = 4 To 301 Step 2 ActiveSheet.Cells(R, C).Formula = _ Frmla1 & Cells(R, C).Address(0, 0) & Frmla2 & Cells(R, C).Address(0, 0) & Frmla3 Next R 

我也删除了你的R = R + 2并改变了For … Next的步骤 ,以2递增。你不应该在循环内自增一个For … Next。

我将Range.Value属性赋值改为Range.Formula属性 。 虽然你的方法经常起作用,但它是不正确的,如果单元格被格式化为文本,公式将以text-that-looks = like-a-formula的forms进入单元格。

你的variables声明应该更像下面的内容。

 Dim R As Long, C As Long Dim Frmla1 As String, Frmla2 As String, Frmla3 As String 

每个声明都应该带有一个variablestypes。 没有它,它们被声明为对象/变体types,