VBA如何循环,直到最后使用的单元格

我试图在每个收盘日之后获得股票的回报,并将其列入列中。 我的问题是开始和结束date改变,任何不用于退货的单元必须完全清除内容。 这是我迄今为止。

Sheets("returns").Range("a2").FormulaR1C1 = _ "=IFERROR(returns(Imported!RC[4],Imported!R[1]C[4]),"""")" Sheets("returns").Range("a2").Select Selection.AutoFill Destination:=Range("a2:a937") Range("a2:a937").Select Sheets("returns").Range("c2").FormulaR1C1 = _ "=IFERROR(returns(Imported!RC[10],Imported!R[1]C[10]),"""")" Sheets("returns").Range("C2").Select Selection.AutoFill Destination:=Range("c2:c937") Range("C2:C937").Select 

这适用于我所需要的,但是在空白单元格中留下了一个公式,我不能为我的项目的下一个步骤。 当数据用完时,还会在最后一行中返回-1。 如果不能解决这个问题,-1的回报不算太大。 有没有办法清除不包含值但包含公式的单元格的内容?

这是我想你想要的…

  • 工作表中有“导入”
  • 您希望工作表中的公式“返回”工作表“已导入”中存在的相同数量的行
 Sub addFormulasBasedOnRecordCount() ' ======================================================== ' jdoxey ' Version 1.0 ' ======================================================== Dim wsWithData As Worksheet ' the name of the worksheet that has the data Dim wsFormulas As Worksheet ' the name of the worksheet that you want the formulas in Set wsWithData = Worksheets("imported") ' change the "name" to be what you want Set wsFormulas = Worksheets("returns") ' change the "name" to be what you want Dim activeRows As Long ' this will be the number of rows that have data ' gets the number of rows in "wsWithData", ' assumes that the data starts in "A1" ' and there are no empty rows activeRows = wsWithData.Range("A1").CurrentRegion.Rows.Count ' puts the formula into column A starting with row 2 though the number of rows in "wsWithData" wsFormulas.Range("A2:A" & activeRows). _ FormulaR1C1 = "=IFERROR(returns(Imported!RC[4],Imported!R[1]C[4]),"""")" ' puts the formula into column C starting with row 2 though the number of rows in "wsWithData" wsFormulas.Range("C2:C" & activeRows). _ FormulaR1C1 = "=IFERROR(returns(Imported!RC[10],Imported!R[1]C[10]),"""")" ' ======================================================== ' ======================================================== 

结束小组