Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/blackberry/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 编码为,若单元格不为空,则在偏移位置插入几个等式_Excel_Vba - Fatal编程技术网

Excel 编码为,若单元格不为空,则在偏移位置插入几个等式

Excel 编码为,若单元格不为空,则在偏移位置插入几个等式,excel,vba,Excel,Vba,我在许多谷歌搜索中发现了不同的答案块,我在下面的代码中将它们拼凑在一起,但我发现了错误和其他问题 我需要扫描p列,如果p列中有一个值,则偏移一行和-12列,然后粘贴到公式中。在记录的宏中,P中的第一个值在P43中。因此,在我的代码中,如果P43是活动单元格,则范围作为单元格,而不是偏移量参考。第一个方程式应进入D44或R1C-12等。我的if cell语句中不断出现错误。此代码必须重复,直到它到达最后一行,因为数据中存在许多间隙,所以行值存储在单元格AU1中 Sub DropEq() Dim

我在许多谷歌搜索中发现了不同的答案块,我在下面的代码中将它们拼凑在一起,但我发现了错误和其他问题

我需要扫描p列,如果p列中有一个值,则偏移一行和-12列,然后粘贴到公式中。在记录的宏中,P中的第一个值在P43中。因此,在我的代码中,如果P43是活动单元格,则范围作为单元格,而不是偏移量参考。第一个方程式应进入D44或R1C-12等。我的if cell语句中不断出现错误。此代码必须重复,直到它到达最后一行,因为数据中存在许多间隙,所以行值存储在单元格AU1中

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

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

columns("P").specialCells(xlcelltypeconstants).offset(1,-12).formular1c1="=OFFSET(R[-1]C[-3],-R[-1]C[30],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

列P值为正数,为空。但我会尝试这个方法,让你知道这是否适用于meThat,它的常数与公式,所以正数是可以的。这非常有效,我所需要做的就是调整P列中活动单元格的参考位置。谢谢