+ Reply to Thread
Results 1 to 5 of 5

Efficient multiplication in VBA with macros

Hybrid View

  1. #1
    Registered User
    Join Date
    03-07-2019
    Location
    Argentina
    MS-Off Ver
    Windows 10
    Posts
    4

    Efficient multiplication in VBA with macros

    Hello friends, I am trying to create a code that is as efficient as possible.

    What I need to do is Multiply a range of cells by a fixed value and then replace the value that each cell had with that new value.

    For example:

    In the "D5: 25" range I have different numerical values in each of the cells. Then I multiply each cell by a fixed value and that new value is replaced in each cell (I mean in the range "D5: 25").

    I was able to solve it through two ways, but both are "inefficient", since when I run the code it shows that it takes a long time to perform all the multiplication in that range.

    And also as I then need to do the same in other ranges too, I know that this code will not be efficient because it will take a long time.

    I pass the two codes that do work but are inefficient.

    Code 1:

    Sub dolartopesos()
    'Routine to pass to pesos all the prices of the products of each supplier that are in dollars.
    
    dollars = Worksheets(3).Range("R1")
    
    '---------------------------------------------------
    
    With Worksheets(3)
    'Currency exchange
    
    For i = 5 To 25
    
    .Cells(i, 4) = .Cells(i, 4) * dollars
    
    Next i
    
    
    End With
    
    
    '---------------------------------------------------
    
    End Sub


    Code2:

    Sub dolartopesos()
    'Routine to pass to pesos all the prices of the products of each supplier that are in dollars.
    
    dollars = Worksheets(3).Range("R1")
    
    '---------------------------------------------------
    ''Currency exchange
    
    Dim rng As Range: Set rng = Worksheets(3).Range("D5:25")
    Dim cel As Range
    For Each cel In rng.Cells
    With cel
    cel = Application.WorksheetFunction.Product(cel, dollars)
    End With
    Next cel
    
    '---------------------------------------------------
    End Sub


    Both codes take too long to multiply in that range.

    What could I do to speed up the multiplication?

    Any contribution will be welcome. From already thank you very much.

    Sebastian.
    Last edited by purinqui; 04-25-2020 at 02:26 AM. Reason: tags

  2. #2
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Efficient multiplication in VBA with macros

    1)
    Please take a moment to add the tags. Posting code between tags makes your code much easier to read and copy for testing, and it also maintains VBA formatting.

    Please see Forum Rule #2 about code tags and adjust accordingly. Click on Edit to open your post, then highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here

    2) Few examples
    Sub test1()
        With Sheets(3)
            .[d5:d25] = .[d5:d25*r1]
        End With
    End Sub
    
    Sub test2()
        With Sheets(3)
            .[r1].Copy
            .[d5:d25].PasteSpecial , xlMultiply
        End With
    End Sub

  3. #3
    Registered User
    Join Date
    03-07-2019
    Location
    Argentina
    MS-Off Ver
    Windows 10
    Posts
    4

    Re: Efficient multiplication in VBA with macros

    Quote Originally Posted by jindon View Post

    2) Few examples
    Sub test1()
        With Sheets(3)
            .[d5:d25] = .[d5:d25*r1]
        End With
    End Sub
    
    Sub test2()
        With Sheets(3)
            .[r1].Copy
            .[d5:d25].PasteSpecial , xlMultiply
        End With
    End Sub

    Thank you very much for your fast answer.

    The both code works properly and so fast!

    Thanks a lot!.
    Last edited by purinqui; 04-25-2020 at 02:27 AM.

  4. #4
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Efficient multiplication in VBA with macros

    Thanks for the code tags.
    Last edited by jindon; 04-25-2020 at 02:41 AM.

  5. #5
    Forum Guru
    Join Date
    04-23-2012
    Location
    New Jersey, USA
    MS-Off Ver
    Excel 365
    Posts
    2,473

    Re: Efficient multiplication in VBA with macros

    This question was asked and answered in the MrExcel forum...

    https://www.mrexcel.com/board/thread.../#post-5474009

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Vba for multiplication
    By winmaxservices in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-17-2019, 10:28 AM
  2. [SOLVED] Sum of multiplication and add-up
    By Seifptv in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 12-19-2018, 07:42 PM
  3. [SOLVED] More Efficient: Calling smaller macros or one Big macro?
    By jakegordon97 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 07-19-2018, 05:40 PM
  4. what is the most efficient way to lookup in macros?
    By dwx in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-08-2015, 01:37 AM
  5. New to Excel Programming and need help making my macros more efficient
    By odoualex in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 05-01-2013, 11:01 AM
  6. Replies: 6
    Last Post: 04-21-2011, 01:12 PM
  7. Multiplication
    By kmarie630 in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 06-25-2009, 03:59 PM

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1