代码为,如果单元格不为空,则在偏移位置插入几个方程式

时间:2015-09-16 21:12:20

标签: excel excel-vba vba

我在许多Google搜索中找到了不同的答案块,我在下面的代码中将它们拼凑在一起,但我收到了错误和其他问题。

我需要扫描col P,如果col P中有值,则偏移一行和-12 col并粘贴到公式中。 (在记录的宏中,P中的第一个值在P43中)。因此,在我的代码中,我将范围作为单元格而不是偏移参考(如果P43是活动单元,则第一个方程应该进入D44或R1C-12等)。我的if cell <> ""声明中一直出现错误。此代码必须重复,直到它到达最后一行(行值存储在单元格&#34; AU1&#34;因为数据中有许多间隙)。

Sub DropEq()

Dim dlen2 As String

Application.ScreenUpdating = False
dlen2 = Worksheets("HR-Calc").Range("AS1")

If Cells(lRow, "P") <> "" Then ActiveCell.Select
ActiveCell.Offset(1, -12).FormulaR1C1 = "=OFFSET(R[-1]C[-3],-R[-1]C[30],0)"

'first instance thats true for statement above  is P43, which would then throw the equations below into those cells
    'Range("d44").ActiveCell.FormulaR1C1 = "=OFFSET(R[-1]C[-3],-R[-1]C[30],0)"

'Range("E44").ActiveCell.FormulaR1C1 = _
    "=COUNTA(R[-1]C[-2]:OFFSET(R[-1]C[-2],-R[-2]C[29],0))/2"

    'Range("F44").ActiveCell.FormulaR1C1 = _
    "=SUMIF(R[-1]C[-4]:OFFSET(R[-1]C[-4],-R[-2]C[28],0),R1C21,R[-1]C[8]:OFFSET(R[-1]C[8],-R[-2]C[28],0))"

'Range("G44").ActiveCell.FormulaR1C1 = _
    "=SUMIF(R[-1]C[-5]:OFFSET(R[-1]C[-5],-R[-2]C[27],0),R2C21,R[-1]C[7]:OFFSET(R[-1]C[7],-R[-2]C[27],0))"

'Range("H44").ActiveCell.FormulaR1C1 =    "=SUM(R[-1]C[7]:OFFSET(R[-1]C[7],-R[-2]C[26],0))"

'Range("I44").ActiveCell.FormulaR1C1 = _
    "= COUNTA(R[-1]C[6]:OFFSET(R[-1]C[6],-R[-2]C[25],0))-COUNTBLANK(R[-1]C[6]:OFFSET(R[-1]C[6],-R[-2]C[25],0))"

'   I'd like a msgbox asking single or double fuse? then ask for fuse rating and place input value that into "E45 relative to P43"
'    Range("E45").ActiveCell.FormulaR1C1 = "30A STRING"
'    Range("F45").ActiveCell.FormulaR1C1 = "POS"
'    Range("G45").ActiveCell.FormulaR1C1 = "NEG"
'    Range("H45").ActiveCell.FormulaR1C1 = "MAX SPL"
'    Range("I45").ActiveCell.FormulaR1C1 = "# SPL"
 Next lRow


Application.ScreenUpdating = True

End Sub

2 个答案:

答案 0 :(得分:0)

假设P列中的值是常量,您可以尝试使用specialcells运行以下代码行,并完全避免循环。

columns("P").specialCells(xlcelltypeconstants).offset(1,-12).formular1c1="=OFFSET(R[-1]C[-3],-R[-1]C[30],0)"

有关特殊单元格的更多信息,请参阅here

答案 1 :(得分:0)

您的代码如下:

Sub DropEq()

Dim dlen2 As String
Application.ScreenUpdating = False
dlen2 = Worksheets("HR-Calc").Range("AS1")

For lrow = 1 To Cells.Range("AU1").Value
    If Cells(lrow, "P") <> "" Then
        Cells(lrow, "P").Select
        ActiveCell.Offset(1, -12).FormulaR1C1 = "=OFFSET(R[-1]C[-3],-R[-1]C[30],0)"
    End If

'first instance thats true for statement above  is P43, which would then throw the equations below into those cells
    'Range("d44").ActiveCell.FormulaR1C1 = "=OFFSET(R[-1]C[-3],-R[-1]C[30],0)"

'Range("E44").ActiveCell.FormulaR1C1 = _
    "=COUNTA(R[-1]C[-2]:OFFSET(R[-1]C[-2],-R[-2]C[29],0))/2"

    'Range("F44").ActiveCell.FormulaR1C1 = _
    "=SUMIF(R[-1]C[-4]:OFFSET(R[-1]C[-4],-R[-2]C[28],0),R1C21,R[-1]C[8]:OFFSET(R[-1]C[8],-R[-2]C[28],0))"

'Range("G44").ActiveCell.FormulaR1C1 = _
    "=SUMIF(R[-1]C[-5]:OFFSET(R[-1]C[-5],-R[-2]C[27],0),R2C21,R[-1]C[7]:OFFSET(R[-1]C[7],-R[-2]C[27],0))"

'Range("H44").ActiveCell.FormulaR1C1 =    "=SUM(R[-1]C[7]:OFFSET(R[-1]C[7],-R[-2]C[26],0))"

'Range("I44").ActiveCell.FormulaR1C1 = _
    "= COUNTA(R[-1]C[6]:OFFSET(R[-1]C[6],-R[-2]C[25],0))-COUNTBLANK(R[-1]C[6]:OFFSET(R[-1]C[6],-R[-2]C[25],0))"

'   I'd like a msgbox asking single or double fuse? then ask for fuse rating and place input value that into "E45 relative to P43"
'    Range("E45").ActiveCell.FormulaR1C1 = "30A STRING"
'    Range("F45").ActiveCell.FormulaR1C1 = "POS"
'    Range("G45").ActiveCell.FormulaR1C1 = "NEG"
'    Range("H45").ActiveCell.FormulaR1C1 = "MAX SPL"
'    Range("I45").ActiveCell.FormulaR1C1 = "# SPL"
Next lrow

Application.ScreenUpdating = True

End Sub