简体   繁体   中英

VBA- creating variable multiple

So new to coding completely. here is question :

How do I make a code that finds a multiple of a number within a set.

Ex. I have a set of number: I want to order the number beginning with the first number with every pair that is 14 a part. I was able to figure out how to do this (See code below) But now I want to do another code looking for multiples of 14 so.. It would look at x, and then find (x*14), (x*(2*14)), etc.. Any help would be appreciated

Column A Column 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

Actually, it should be that simple.

multiple = Cells(x*14, 2)

I think that should do what you want.

Yes That worked perfectly. Here is the final code I came up with :

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

`For x = 2 To lastRow nomMass = Cells(x, 2).Value ' base 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

Next x

End Sub

Private Function Found(rng As Range, valueToFind) As Boolean On Error GoTo errHandler

Dim v

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

Found = True

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM