Consulting

Results 1 to 6 of 6

Thread: Solved: Fastest way to divide all rows in column C by a fixed number, put results in column D

  1. #1

    Solved: Fastest way to divide all rows in column C by a fixed number, put results in column D

    I have a simple calculation that I need to perform MANY times (upwards of 30-40K).

    I have whole numbers in rows (30-40k rows) of column C, the numbers can be anything from 0 to 20k.

    I need to divide that number by a number stored in another variable (MyRows). This number will be a whole number between 0 and 40 k. I want the value result of that division in column d.

    So if:

    C1=10 and MyRows=100 then D1=0.1
    C2=20 and MyRows=100 then D2=0.2

    Example data layout attached. Let me know if you have any questions or need clarification.

    Thanks

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]

    Dim LastRow As Long
    Dim tmp As Variant

    LastRow = Cells(Rows.Count, "C").End(xlUp).Row
    tmp = Range("IV1").Value
    Range("IV1").Value = myrows
    Range("IV1").Copy
    Range("C1").Resize(LastRow).PasteSpecial Paste:=xlPasteAll, Operation:=xlDivide
    Range("IV1").Value = tmp
    Application.CutCopyMode = False
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    Moderator VBAX Guru Simon Lloyd's Avatar
    Joined
    Sep 2005
    Location
    UK
    Posts
    3,003
    Location
    I believe the fastest way as you asked would be to put your division formula in column D select it then Ctrl+Shift+End then EDIT>FILL>DOWN

    EDIT: If you have a variable stored you could create a UDF and do the same.
    Regards,
    Simon
    Please read this before cross posting!
    In the unlikely event you didn't get your answer here try Microsoft Office Discussion @ The Code Cage
    If I have seen further it is by standing on the shoulders of giants.
    Isaac Newton, Letter to Robert Hooke, February 5, 1675 English mathematician & physicist (1642 - 1727)

  4. #4
    xld,

    The division is correct and it's fast. My version would have been a slow and clunky loop.

    I'm having one minor issue. Currently it pastes over column C. How do I paste/post the results of the division in D, while preserving the original values in column C?

  5. #5
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]

    Dim LastRow As Long
    Dim tmp As Variant

    LastRow = Cells(Rows.Count, "C").End(xlUp).Row
    Columns("C").Copy Columns("D")
    With Range("IV1")

    tmp = .Value
    .Value = MyRows
    .Copy
    Range("D1").Resize(LastRow).PasteSpecial Paste:=xlPasteAll, Operation:=xlDivide
    .Value = tmp
    End With
    Application.CutCopyMode = False
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  6. #6
    Nailed it

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •