我试图在每个结束日期之后获得股票的回报并将其列在一列中。我的问题是,开始和结束日期会发生变化,任何未用于返回的单元格都必须完全清除内容。这是我到目前为止所拥有的。
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回报并不算太大。有没有办法清除不包含值但包含公式的单元格的内容?
答案 0 :(得分: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]),"""")"
' ========================================================
' ========================================================
End Sub