1

有关如何解决此问题的任何建议?

(更新)

numerator,denominator是包含整数值的范围。

我想对这些范围的单元格进行划分,例如:

numerator(1)/denominator(1) numerator(2)/denominator(2) ... 

并将结果粘贴到quotient. 所有范围都具有相同的大小并且是行向量维度的。

我的解决方案尝试:

Sub DivRanges()
    Dim targetRng As Range, sourceRng1 As Range, sourceRng2 As Range    
    For i = 1 To targetRng.Cells.Count
         quotient.Cells(i).Value = numerator.Cells(i).Value / denominator.Cells(i).Value
    Next i
End Sub

问题是它DivRanges不会遍历范围的每个值,因为它们是行向量。相反,随着我的成长,它会水平下降一步。当范围的单元格不相邻时,会出现此问题。

这是基于来自 mehow 的代码的问题显示。结果是工作表中选择的单元格。

例子

4

1 回答 1

1

如果您的电子表格看起来像

之前

然后这段代码

Sub DivRanges()
    Dim quotient As Range, numerator As Range, denominator As Range
    
    Set numerator = Range("A1:D1")
    Set denominator = Range("A2:D2")
    Set quotient = Range("A3:D3")
    
    Dim i As Long
    For i = 1 To quotient.Count
        quotient.Cells(i).Value = numerator.Cells(i).Value / denominator.Cells(i).Value
    Next i
End Sub

给你

船尾


Update

不相邻的 Cells 解决方案

假设你的表

前

使用下面的代码

Sub DivRanges()
    Dim quotient As Range, numerator As Range, denominator As Range

    Set numerator = Range("A1, C1, D1, E1")
    Set denominator = Range("A2, C2, D2, E2")
    Set quotient = Range("A3, C3, D3, E3")
    
    Dim c As Range
    Dim i As Long
    i = 0
    For Each c In quotient
        Range(c.Address) = Range(getNum(numerator.Address, i)) / Range(getNum(denominator.Address, i))
        i = i + 1
    Next
    
End Sub

Function getNum(str As String, i As Long) As String
    getNum = Split(str, ",")(i)
End Function

结果是

结果

于 2013-07-25T11:02:33.263 回答