To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here
VBForums  

VB Wire News
Part 10 of the Visual Basic .NET 2010 Express Tutorial Complete!
How to Use the Visual Studio Code Analysis Tool FxCop
Article :: Interview with Andrei Alexandrescu (Part 3 of 3)
Introducing Visual Studio LightSwitch
Visual Studio LightSwitch Beta 1 is Available



Go Back   VBForums > Visual Basic > Office Development

Reply Post New Thread
 
Thread Tools Display Modes
Old Apr 5th, 2007, 05:36 PM   #1
pgag45
Hyperactive Member
 
pgag45's Avatar
 
Join Date: Mar 07
Location: Colorado
Posts: 262
pgag45 is an unknown quantity at this point (<10)
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
pgag45 is offline   Reply With Quote
Old Apr 6th, 2007, 10:54 AM   #2
VBAhack
Fanatic Member
 
VBAhack's Avatar
 
Join Date: Dec 04
Location: Sector 000
Posts: 556
VBAhack will become famous soon enough (65+)
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
VBAhack is offline   Reply With Quote
Reply

Go Back   VBForums > Visual Basic > Office Development


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -5. The time now is 05:27 AM.





Acceptable Use Policy

Internet.com
The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers

Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.