Results 1 to 2 of 2

Thread: Converting an entire sheet to Metric and back

  1. #1

    Thread Starter
    Hyperactive Member pgag45's Avatar
    Join Date
    Mar 2007
    Location
    Colorado
    Posts
    262

    Converting an entire sheet to Metric and back

    Hi all,

    I would like to convert an entire Excel sheet, or at least most of it, to metric units (multiplying by 1.12 for this particular unit). The entire range I wish to convert can be anywhere from 8 cols x 30 rows to 8 cols x 10k rows. What would be the most efficient way to solve this problem? Just a huge loop over the range of the cells I wish to convert, multiplying (or dividing if going back to English) each cell value by 1.12? Or is there a more efficient approach?

    Thanks Much

  2. #2
    Fanatic Member VBAhack's Avatar
    Join Date
    Dec 2004
    Location
    Sector 000
    Posts
    617

    Re: Converting an entire sheet to Metric and back

    Looping through individual cells in a range can be slow. It is better to read the range directly into an array, modify the array, and write it back out:

    vb Code:
    1. Sub RangeMod()
    2.     Dim nrow As Integer, ncol As Integer, i As Integer, j As Integer
    3.     Dim mf As Double
    4.     Dim a As Variant
    5.    
    6.     mf = 1.15
    7.     nrow = 27
    8.     ncol = 8
    9.    
    10.     a = Range("D5").Resize(nrow, ncol)      'read range into array
    11.    
    12.     For i = 1 To nrow                       'loop through array
    13.         For j = 1 To ncol                   'loop through array
    14.             a(i, j) = mf * a(i, j)          'modify array elements
    15.         Next j
    16.     Next i
    17.    
    18.     Range("D5").Resize(nrow, ncol) = a      'write array back to range
    19. End Sub

Posting Permissions

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



Click Here to Expand Forum to Full Width