473,395 Members | 1,653 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Use of Range function

Hello Everyone,

I am using VBA for macros for use with an Excel spreadsheet I have been working on. The goal of the macro is to change 7th row's height to match that of a template.

In my first attempt I tried simply looping through the rows changing every seventh row's height and then moving on to the next one. Unfortunately, due to the size of the spreadsheet and other activities occurring at the same time, this became very inefficient. As an alternative I have already gathered a list of the rows to be changed in the following format into a string called Lst:
"1:1,8:8,15:15,...,n:n" where n is the last row with data in it
Then I use the string in the following line of code:
Expand|Select|Wrap|Line Numbers
  1. Range(Lst).RowHeight = Worksheets("Template").Range("A" & 1).RowHeight
Now this works until there is data in or past row 252. The sample I am using as a test has data until row 344. For this sample Lst should have the following value:
"1:1,8:8,15:15,22:22,29:29,36:36,43:43,50:50,57:57 ,64:64,71:71,78:78,85:85,92:92,99:99,106:106,113:1 13,120:120,127:127,134:134,141:141,148:148,155:155 ,162:162,169:169,176:176,183:183,190:190,197:197,2 04:204,211:211,218:218,225:225,232:232,239:239,246 :246,253:253,260:260,267:267,274:274,281:281,288:2 88,295:295,302:302,309:309,316:316,323:323,330:330 ,337:337,344:344"
To test the data I used MsgBox Lst, which does agree. To the contrary, the debugger which comes with Excel shows Lst to have the following value:
"1:1,8:8,15:15,22:22,29:29,36:36,43:43,50:50,57:57 ,64:64,71:71,78:78,85:85,92:92,99:99,106:106,113:1 13,120:120,127:127,134:134,141:141,148:148,155:155 ,162:162,169:169,176:176,183:183,190:190,197:197,2 04:204,211:211,218:218,225:225,232:232,239:239,246 :246,
Could someone help me resolve this issue? Thank you in advance.

SOLUTION - ** SNIP **
Jan 31 '12 #1
2 5400
NeoPa
32,556 Expert Mod 16PB
Although the string length can go into the millions, the string value passed as the Cell1 parameter to Range cannot exceed 256 without this failure occurring.

The following code will handle this if required, by breaking up the list of rows into strings which are each less than 256 long and setting the .RowHeight property for each one :
Expand|Select|Wrap|Line Numbers
  1. Option Explicit
  2.  
  3. Public Sub SetRowHeight(lngMaxRow As Long)
  4.     Dim strRows As String, strWork As String
  5.     Dim lngX As Long
  6.     Dim dblRowHeight As Double
  7.  
  8.     dblRowHeight = Worksheets("Template").Range("A1").RowHeight
  9.     For lngX = 1 To lngMaxRow Step 7
  10.         strRows = strRows & Replace(",R:R", "R", lngX)
  11.     Next lngX
  12.     strRows = Mid(strRows, 2)
  13.     Do While strRows > ""
  14.         If Len(strRows) > 256 Then
  15.             lngX = InStr(240, strRows, ",") - 1
  16.         Else
  17.             lngX = Len(strRows)
  18.         End If
  19.         strWork = Left(strRows, lngX)
  20.         Range(strWork).RowHeight = dblRowHeight
  21.         strRows = Mid(strRows, lngX + 2)
  22.     Loop
  23. End Sub
Jan 31 '12 #2
NeoPa
32,556 Expert Mod 16PB
I did try similar code but using the Union() method instead (See below), but it ran a great deal more slowly.

Expand|Select|Wrap|Line Numbers
  1. Option Explicit
  2.  
  3. Public Sub SetRowHeight(lngMaxRow As Long)
  4.     Dim strRows As String, strWork As String
  5.     Dim lngX As Long
  6.     Dim ranList As Range
  7.  
  8.     Set ranList = Range("1:1")
  9.     For lngX = 8 To lngMaxRow Step 7
  10.         strRows = strRows & Replace(",R:R", "R", lngX)
  11.     Next lngX
  12.     strRows = Mid(strRows, 2)
  13.     Do While strRows > ""
  14.         If Len(strRows) > 256 Then
  15.             lngX = InStr(240, strRows, ",") - 1
  16.         Else
  17.             lngX = Len(strRows)
  18.         End If
  19.         strWork = Left(strRows, lngX)
  20.         Set ranList = Union(ranList, Range(strWork))
  21.         strRows = Mid(strRows, lngX + 2)
  22.     Loop
  23.     ranList.RowHeight = Worksheets("Template").Range("A1").RowHeight
  24. End Sub
Jan 31 '12 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Harald Massa | last post by:
which one should I use? Recommendations? Harald
4
by: Adrian Albisser | last post by:
Hey to everybody, im just beginning to program in python. So i was trying some function but always when i try the range function i get a error message. for number in range(1,100): print number...
2
by: bwooster47 | last post by:
I'm a newcomer to python - what is the best way to convert a list into a function call agruments? For example: list = (2005, 5, 5) date = datetime.date( list ) fails with: TypeError:...
7
by: Xah Lee | last post by:
Today we'll be writing a function called Range. The Perl documentation is as follows. Perl & Python & Java Solutions will be posted in 48 hours. This is Perl-Python a-day. See...
5
by: Chris | last post by:
Hey all. Anyone who is familiar with Python programming knows that you can have code like this: list = This code puts all the items processed by the for loop in a list plus 1. Is there a way...
45
by: Summercoolness | last post by:
it seems that range() can be really slow: the following program will run, and the last line shows how long it ran for: import time startTime = time.time() a = 1.0
0
by: thattommyhallll | last post by:
i am doing the problems at http://www.mathschallenge.net/index.php?section=project one problem involved finding factors, i used def divisors(n): divisors = set() for i in range(1, math.ceil(n...
9
by: bibaudj | last post by:
I'm trying to write a function to be used in a query - because I don't think it can be compiled directly in a query (using Expression Builder). This is for a holiday DB. I would like to determine...
3
by: vimal | last post by:
hi all, i am new to python..... i just want to generate numbers in the form like: 1,2,4,8,16,32.....to a maximum of 1024 using a range function
50
by: John Salerno | last post by:
I know it's popular and very handy, but I'm curious if there are purists out there who think that using something like: for x in range(10): #do something 10 times is unPythonic. The reason I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.