Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/15.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
VBA替换:处理当前单元地址,不激活_Vba_Excel - Fatal编程技术网

VBA替换:处理当前单元地址,不激活

VBA替换:处理当前单元地址,不激活,vba,excel,Vba,Excel,我有一大组多项式(例如:“X^2+3*X+7”)。它们汇编如下: 我的范围的第二列、第三列等(“多项式”)包含多项式,而第一列包含X值(以命名范围的形式) 我成功地编写了一些代码,可以在整个范围内切换,并用实际的x值替换任何“x”,无论是否大写 Private Sub Loop_Range() Dim mrngLoop As Range 'this it the specific range which will be looped through to "activate" all its

我有一大组多项式(例如:“X^2+3*X+7”)。它们汇编如下: 我的范围的第二列、第三列等(“多项式”)包含多项式,而第一列包含X值(以命名范围的形式)

我成功地编写了一些代码,可以在整个范围内切换,并用实际的x值替换任何“x”,无论是否大写

Private Sub Loop_Range()

Dim mrngLoop As Range 'this it the specific range which will be looped through to "activate" all its functions
Dim mrngCell As Range 'this is a cell within mrngLoop

Set mrngLoop = range("polynomials")

For Each mrngCell In mrngLoop.Cells
    mstrPolynomial = mrngCell.Value
    mstrPolynomial = Replace(mstrPolynomial, "X", "x")
    mstrPolynomial = Replace(mstrPolynomial, "x", Cells(mrngCell.Row, Range("polynomials").Column - 1).Value)
    mstrPolynomial = Replace(mstrPolynomial, ",", ".")
    mstrPolynomial = "=" & mstrPolynomial

    mrngCell.Value = strPolynomial

Next mrngCell 
End Sub
上面的代码有效。但是我试图通过实现一个数组来改进/缩短代码(@Scott Craner:谢谢你的建议!):


简短的回答是否定的。如果你想用一个不同的数字替换所有的X,那么你需要迭代。批量替换是为了用相同的值替换所有人。回答稍长一点,将值加载到数组中,循环遍历数组并进行替换,然后在现有数据上加载数组。如果只引用图纸两次,一次加载数组,一次赋值,速度会快得多。数据的循环将非常快。引用工作表的时间越少越好。@Scott Craner:谢谢您的快速回复!也可以查看有关数组的提示。这绝对是个问题。我得再深入一点。只是确定一下。我不能对数组使用
Replace What:=“X”,Replacement:=…
,对吗?我尝试了几个组合,但总是收到缺少对象的错误。您可以使用类似这样的方法
Arr(1,1)=replace(Arr(1,1),“x”,Arr(1,2))
Private Sub Loop_Array()

Dim marrLoop() As Variant 'this is an array which which will be looped through instead of the range to "activate" all its functions
Dim mintRow As Integer 'this is the row index of the array
Dim mintCol As Integer 'this is the column index of the array

marrLoop = Range("polynomial")
For mintRow = 1 To UBound(marrLoop, 1) ' First array dimension is rows.
    For mintCol = 2 To UBound(marrLoop, 2) ' Second array dimension is columns.

            marrLoop(mintRow, mintCol) = Replace(marrLoop(mintRow, mintCol), "X", "x")
            marrLoop(mintRow, mintCol) = Replace(marrLoop(mintRow, mintCol), ",", ".")
            marrLoop(mintRow, mintCol) = Replace(marrLoop(mintRow, mintCol), "x", marrLoop(mintRow, 1))
            marrLoop(mintRow, mintCol) = "=" & marrLoop(mintRow, mintCol)

    Next mintCol
Next mintRow
Range("polynomial") = marrLoop

End Sub