Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 41
Default VBA Code Optimization

Hey all,

I am currently trying to optimize a set of code i wrote which creates
new spreadsheets, and fills their cells with data and equations pulled
from other source sheets. It is a very long set of code (some 869
lines) with alot of repeating cell-by-cell operations. Is there a way
to make the whole thing recursive, and possibly use objects instead of
lots of copied/pasted repeated code using nothing but simple
variables. If more detail or a sample of the code is necessary, i can
provide.

For example, part of my code searches for the location of a named cell
(column header), and assigns a variable its column position to use
later on with copy/paste operations when the new spreadsheet is
generated. This code is repeated MANY times, with different variable
names being subbed in....Any way to create and object out of this
process and just import the new variable names and set them in a
recursive way, instead of just duplicating this code:

Set Rcc = Rows(1).Find(what:="Rcc", LookIn:=xlValues)
If Not Rcc Is Nothing Then
myaddress = Mid(Rcc.Address, 2)
Rcccol = Left(myaddress, InStr(myaddress, "$") - 1)
End If

Any ideas or suggestions would be more than welcome. Thanks!

-Pogster
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,939
Default VBA Code Optimization

I have no idea how you would use recursive code here but a simple functoin
would possibly help for what you posted...

public Function GetCol(byval rngRow as range, byval strValue as string) as
long
dim rngFound as range

set rngfound = rngrow.find(What:=strvalue,
LookAt:=xlWhole, LookIn:=xlValue, MatchCase:=False)
if rngfound is nothing then
GetCol = -1
else
getcol = rngfound.column
end if
end function

Now when you need to find a column just call this function suppling the
Range to be searched and the value to be located.
--
HTH...

Jim Thomlinson


" wrote:

Hey all,

I am currently trying to optimize a set of code i wrote which creates
new spreadsheets, and fills their cells with data and equations pulled
from other source sheets. It is a very long set of code (some 869
lines) with alot of repeating cell-by-cell operations. Is there a way
to make the whole thing recursive, and possibly use objects instead of
lots of copied/pasted repeated code using nothing but simple
variables. If more detail or a sample of the code is necessary, i can
provide.

For example, part of my code searches for the location of a named cell
(column header), and assigns a variable its column position to use
later on with copy/paste operations when the new spreadsheet is
generated. This code is repeated MANY times, with different variable
names being subbed in....Any way to create and object out of this
process and just import the new variable names and set them in a
recursive way, instead of just duplicating this code:

Set Rcc = Rows(1).Find(what:="Rcc", LookIn:=xlValues)
If Not Rcc Is Nothing Then
myaddress = Mid(Rcc.Address, 2)
Rcccol = Left(myaddress, InStr(myaddress, "$") - 1)
End If

Any ideas or suggestions would be more than welcome. Thanks!

-Pogster

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 41
Default VBA Code Optimization

Thats a great start. Thanks for that code suggestion Jim. Maybe
recursive is not the right word, its just that alot of this macro is
the same exact code copied over and over, with slight changes in
variable names, or cell positions. Isnt there a way to shorten the
code by using loops, or objects, or something besides simple
variables.
I might not be savvy enough in VBA to figure out that code on my own,
but i know it must exist.

As for your function, thats exactly what i am talking about. I guess i
meant functions, and not "objects", im thinking in terms of Java i
guess.
I will try to implement that and see if it simplifies (SHORTENS!!!) my
code at all.

The only reason i would want to shorten it, btw, is because i will be
leaving a position i now hold at a company, and i need to be able to
pass my work on to someone else, who will probably not understand VBA
very well. The less code they have to deal with, the better. That is
the atmosphere here.

Btw, what does the function call for that function look like? Is it
just Getcol(1, "Rcc") ?
The row will always be row 1, also.

Thanks again Jim.

-Pogster
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,588
Default VBA Code Optimization

Something like this could serve as a general-purpose "find column" function:


Function GetCol(sVal as string) as long
dim f as range
Set f = Rows(1).Find(what:=sVal, LookIn:=xlValues)

if not f is nothing then
GetCol=f.column
else
GetCol=0
end if

end function



wrote in message
...
Hey all,

I am currently trying to optimize a set of code i wrote which creates
new spreadsheets, and fills their cells with data and equations pulled
from other source sheets. It is a very long set of code (some 869
lines) with alot of repeating cell-by-cell operations. Is there a way
to make the whole thing recursive, and possibly use objects instead of
lots of copied/pasted repeated code using nothing but simple
variables. If more detail or a sample of the code is necessary, i can
provide.

For example, part of my code searches for the location of a named cell
(column header), and assigns a variable its column position to use
later on with copy/paste operations when the new spreadsheet is
generated. This code is repeated MANY times, with different variable
names being subbed in....Any way to create and object out of this
process and just import the new variable names and set them in a
recursive way, instead of just duplicating this code:

Set Rcc = Rows(1).Find(what:="Rcc", LookIn:=xlValues)
If Not Rcc Is Nothing Then
myaddress = Mid(Rcc.Address, 2)
Rcccol = Left(myaddress, InStr(myaddress, "$") - 1)
End If

Any ideas or suggestions would be more than welcome. Thanks!

-Pogster



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Excel code optimization deepika :excel help[_2_] Excel Discussion (Misc queries) 5 January 29th 08 01:02 PM
VBA Code Optimization for Array Formulas [email protected] Excel Programming 0 October 11th 07 06:46 PM
Seo , Search Engine Optimizer , Seo Search engine Optimization , search engine optimization services, SEO Consulting Se0 Guy Excel Discussion (Misc queries) 0 March 8th 07 04:08 AM
Code for optimization, ReDim Preserve not handling it well, HELP please! [email protected] Excel Programming 0 November 2nd 05 08:18 AM
Pivot Table - Code Optimization kaon[_35_] Excel Programming 3 August 20th 04 02:24 AM


All times are GMT +1. The time now is 02:20 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"