VBA-创建变量倍数

时间:2014-08-26 20:55:27

标签: excel vba excel-vba

完全是编码的新手。这是问题:

如何创建在集合中找到数字倍数的代码。

实施例。我有一组数字:我想订购以第一个数字开头的数字,每一对都是14个部分。我能够弄清楚如何做到这一点(见下面的代码)但现在我想做另一个寻找14的倍数的代码所以..它会看x,然后找(x * 14),(x *( 2 * 14))等..任何帮助将不胜感激

A栏B栏

459 452 426 485 425

Sub GetPairs()

      Dim x, z As Single
      Dim lastrow, pasterow As Single
      Dim testMass, nomMass As Single
      lastrow = Worksheets(1).Cells(Rows.Count, 2).End(xlUp).Row

    pasterow = 2



    For x = 2 To lastrow
        nomMass = Cells(x, 2).Value
        testMass = Cells(x, 2) + 14



o

    r z = 2 To lastrow
          If Cells(z, 2).Value = testMass Then
            Cells(pasterow, 7).Value = nomMass
            Cells(pasterow, 8).Value = Cells(z, 2).Value
            pasterow = pasterow + 1
          End If


     Next z

      Next x


End Sub

2 个答案:

答案 0 :(得分:0)

实际上,它应该很简单。

multiple = Cells(x*14, 2)

我认为应该做你想做的事。

答案 1 :(得分:0)

是的,效果很好。 这是我提出的最终代码:

Sub GetPairs()  ``Dim x As Single,z As Single

Dim lastRow, pasterow As Single Dim testMass,nomMass As Single  ``Dim lastValue As Long  `Dim colCounter As Long

``Dim lookUpRange As Range

`lastRow = Worksheets(1).Cells(Rows.Count, 2).End(xlUp).Row
`lastValue = Worksheets(1).Cells(Rows.Count, 2).End(xlUp).Value
`Set lookUpRange = Worksheets(1).Range("B2:B" & lastRow)

``pasterow = 2

`对于x = 2到lastRow    nomMass = Cells(x,2).Value'基值     colCounter = 3

For z = Round((nomMass + 14), 0) To Round((lastValue + 14), 0) Step 14
    If Found(lookUpRange, z) Then
        'found
        Worksheets(1).Cells(x, colCounter) = z
        colCounter = colCounter + 1
    End If
Next z

下一个x

End Sub

找到私有函数(作为Range,valueToFind)作为布尔值     On Error GoTo errHandler

Dim v

v = WorksheetFunction.VLookup(valueToFind, rng, 1, 0)

Found = True