Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 36
Default Combining VBA code for columns

Hi-

I have the following code that I run for 8 consecutive
columns....columns I through P. As of now I am repeating this code 8
times (8 seperate blocks of code). This is obviously slowing the
process down quite a bit. My question is this: Is there a way to
combine all 8 columns into one block of code? Here is the code I am
using:

Range("I8").Select
Set rng = Range(ActiveCell.Address)
ActiveCell.Offset(1, -7).Range("A1").Select

Do Until ActiveCell.Value = ""
ActiveCell.Offset(0, 7).Range("A1").Select
If ActiveCell.Value < "" Then
l = ActiveCell.Value + l
ActiveCell.Offset(1, -7).Select
Else
rng.Activate
ActiveCell.Value = l
Selection.End(xlDown).Select
ActiveCell.Offset(1, 0).Select
Set rng = Range(ActiveCell.Address)
ActiveCell.Offset(1, -7).Select
l = 0

End If
Loop

rng.Activate
ActiveCell.Value = l
l = 0

Then I have this code for column J....

Range("J8").Select
Set rng = Range(ActiveCell.Address)
ActiveCell.Offset(1, -8).Range("A1").Select

Do Until ActiveCell.Value = ""
ActiveCell.Offset(0, 8).Range("A1").Select
If ActiveCell.Value < "" Then
l = ActiveCell.Value + l
ActiveCell.Offset(1, -8).Select
Else
rng.Activate
ActiveCell.Value = l
Selection.End(xlDown).Select
ActiveCell.Offset(1, 0).Select
Set rng = Range(ActiveCell.Address)
ActiveCell.Offset(1, -8).Select
l = 0

End If
Loop

rng.Activate
ActiveCell.Value = l
l = 0

TIA,
Chris

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Combining VBA code for columns

Figuring out what you are doing and rewriting it would be the way to improve
your code, but to reduce the repetative blocks:

for jj = 9 to 16
Cells(i,jj).Select
Set rng = Range(ActiveCell.Address)
ActiveCell.Offset(1, -7).Range("A1").Select

Do Until ActiveCell.Value = ""
ActiveCell.Offset(0, 7).Range("A1").Select
If ActiveCell.Value < "" Then
l = ActiveCell.Value + l
ActiveCell.Offset(1, -7).Select
Else
rng.Activate
ActiveCell.Value = l
Selection.End(xlDown).Select
ActiveCell.Offset(1, 0).Select
Set rng = Range(ActiveCell.Address)
ActiveCell.Offset(1, -7).Select
l = 0

End If
Loop

rng.Activate
ActiveCell.Value = l
l = 0
Next jj

--
Regards,
Tom Ogilvy


wrote in message
oups.com...
Hi-

I have the following code that I run for 8 consecutive
columns....columns I through P. As of now I am repeating this code 8
times (8 seperate blocks of code). This is obviously slowing the
process down quite a bit. My question is this: Is there a way to
combine all 8 columns into one block of code? Here is the code I am
using:

Range("I8").Select
Set rng = Range(ActiveCell.Address)
ActiveCell.Offset(1, -7).Range("A1").Select

Do Until ActiveCell.Value = ""
ActiveCell.Offset(0, 7).Range("A1").Select
If ActiveCell.Value < "" Then
l = ActiveCell.Value + l
ActiveCell.Offset(1, -7).Select
Else
rng.Activate
ActiveCell.Value = l
Selection.End(xlDown).Select
ActiveCell.Offset(1, 0).Select
Set rng = Range(ActiveCell.Address)
ActiveCell.Offset(1, -7).Select
l = 0

End If
Loop

rng.Activate
ActiveCell.Value = l
l = 0

Then I have this code for column J....

Range("J8").Select
Set rng = Range(ActiveCell.Address)
ActiveCell.Offset(1, -8).Range("A1").Select

Do Until ActiveCell.Value = ""
ActiveCell.Offset(0, 8).Range("A1").Select
If ActiveCell.Value < "" Then
l = ActiveCell.Value + l
ActiveCell.Offset(1, -8).Select
Else
rng.Activate
ActiveCell.Value = l
Selection.End(xlDown).Select
ActiveCell.Offset(1, 0).Select
Set rng = Range(ActiveCell.Address)
ActiveCell.Offset(1, -8).Select
l = 0

End If
Loop

rng.Activate
ActiveCell.Value = l
l = 0

TIA,
Chris



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 36
Default Combining VBA code for columns

Thanks Tom.

I am somewhat of a newbie and am trying to learn on the fly. I think
my main problem is the DoUntil Loop. I am going to change that to a
Nested For Loop. Do you think this is the best way to attack this? I
have a variable that stores the number of lines in the spreadsheet.

If you have any other advice, I could use it!

Thanks Again,
Chris

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,119
Default Combining VBA code for columns

Changing the do until into a for loop will have almost no effect. Your
biggest issue is that you are using the active cell and selecting. Also you
do not show where you are declaring the variables. If undeclared then the
variables are defaulting to variant which is also extremely slow. What
exactly is your macro supposed to do (in simple english). We can take a stab
at cleaning it up for you by removing the selects and maybe adding some range
objects to avoid using the active cell.
--
HTH...

Jim Thomlinson


" wrote:

Thanks Tom.

I am somewhat of a newbie and am trying to learn on the fly. I think
my main problem is the DoUntil Loop. I am going to change that to a
Nested For Loop. Do you think this is the best way to attack this? I
have a variable that stores the number of lines in the spreadsheet.

If you have any other advice, I could use it!

Thanks Again,
Chris


  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 36
Default Combining VBA code for columns

First off....I changed the code to a For Loop. Here is what I have
now:

Range("I8").Select
Set rng = Range(ActiveCell.Address)

For Count = 0 To i
ActiveCell.Offset(1, 0).Select
If ActiveCell.Value < "" Then
l = ActiveCell.Value + l
Else
rng.Activate
ActiveCell.Value = l
Selection.End(xlDown).Select
ActiveCell.Offset(1, 0).Select
Set rng = Range(ActiveCell.Address)
l = 0
End If
Next Count

I have Parent and NonParent Values in a spreadsheet. The Parents sit
on top of the NonParents. What the code does is it stores the values
of the non-parents in a variable and then goes to the parents cell
address and pastes that value. Then it moves on to the next parent
value. (Every parent value is a blank cell to begin with.)

Essentially it is a subtotal in reverse order. I am trying to total
all of the nonparent values in put them in the parent cell above.

Example:

Parent - COSTCO
<This cell is blank
NonParent - COSTCO 124 A. St. Boston, MA
500
NonParent - COSTCO 45 Fish St. New York, NY
600

What the code does is it totals to 1100 and stores it in the variable
(l in code above) then goes to the parent cell and pastes that value.
It finds the next parent cell by jumping to the next blank cell and
clearing the variable l.

Hope this makes sense. Open to all suggestions.

Also...I select EVERYTHING. It is the only way I can visualize the
code. Is there a quick tutorial on "How to get away from selecting
cells"?

Thanks!!!

Chris



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,119
Default Combining VBA code for columns

This code is not a whole lot different from yours (mostly just translated).
It needs to have i defined...

Sub TotalParents()
On Error GoTo ErrorHandler
Dim rngParent As Range 'Hold the parent cell
Dim rngCurrent As Range 'Move this cell
Dim wks As Worksheet 'Define the worksheet we are on
Dim lngParentTotal As Long
Dim count As Long
Dim i As Long

Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual

Set wks = ActiveSheet
Set rngParent = wks.Range("I8")
Set rngCurrent = rngParent

For count = 0 To i
Set rngCurrent = rngCurrent.Offset(1, 0)
If lngParentTotal < "" Then
lngParentTotal = lngParentTotal + lngParentTotal
Else
rngParent.Value = lngParentTotal
lngParentTotal = 0
Set rngParent = rngParent.End(xlDown).Offset(1, 0)
End If
Next count
ErrorHandler:
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
End Sub
--
HTH...

Jim Thomlinson


" wrote:

First off....I changed the code to a For Loop. Here is what I have
now:

Range("I8").Select
Set rng = Range(ActiveCell.Address)

For Count = 0 To i
ActiveCell.Offset(1, 0).Select
If ActiveCell.Value < "" Then
l = ActiveCell.Value + l
Else
rng.Activate
ActiveCell.Value = l
Selection.End(xlDown).Select
ActiveCell.Offset(1, 0).Select
Set rng = Range(ActiveCell.Address)
l = 0
End If
Next Count

I have Parent and NonParent Values in a spreadsheet. The Parents sit
on top of the NonParents. What the code does is it stores the values
of the non-parents in a variable and then goes to the parents cell
address and pastes that value. Then it moves on to the next parent
value. (Every parent value is a blank cell to begin with.)

Essentially it is a subtotal in reverse order. I am trying to total
all of the nonparent values in put them in the parent cell above.

Example:

Parent - COSTCO
<This cell is blank
NonParent - COSTCO 124 A. St. Boston, MA
500
NonParent - COSTCO 45 Fish St. New York, NY
600

What the code does is it totals to 1100 and stores it in the variable
(l in code above) then goes to the parent cell and pastes that value.
It finds the next parent cell by jumping to the next blank cell and
clearing the variable l.

Hope this makes sense. Open to all suggestions.

Also...I select EVERYTHING. It is the only way I can visualize the
code. Is there a quick tutorial on "How to get away from selecting
cells"?

Thanks!!!

Chris


  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 573
Default Combining VBA code for columns


Tom Ogilvy wrote:
Figuring out what you are doing and rewriting it would be the way to improve
your code, but to reduce the repetative blocks:

for jj = 9 to 16
Cells(i,jj).Select
Set rng = Range(ActiveCell.Address)
ActiveCell.Offset(1, -7).Range("A1").Select

Do Until ActiveCell.Value = ""
ActiveCell.Offset(0, 7).Range("A1").Select
If ActiveCell.Value < "" Then
l = ActiveCell.Value + l
ActiveCell.Offset(1, -7).Select
Else
rng.Activate
ActiveCell.Value = l
Selection.End(xlDown).Select
ActiveCell.Offset(1, 0).Select
Set rng = Range(ActiveCell.Address)
ActiveCell.Offset(1, -7).Select
l = 0

End If
Loop

rng.Activate
ActiveCell.Value = l
l = 0
Next jj

--
Regards,
Tom Ogilvy


wrote in message
oups.com...
Hi-

I have the following code that I run for 8 consecutive
columns....columns I through P. As of now I am repeating this code 8
times (8 seperate blocks of code). This is obviously slowing the
process down quite a bit. My question is this: Is there a way to
combine all 8 columns into one block of code? Here is the code I am
using:

Range("I8").Select
Set rng = Range(ActiveCell.Address)
ActiveCell.Offset(1, -7).Range("A1").Select

Do Until ActiveCell.Value = ""
ActiveCell.Offset(0, 7).Range("A1").Select
If ActiveCell.Value < "" Then
l = ActiveCell.Value + l
ActiveCell.Offset(1, -7).Select
Else
rng.Activate
ActiveCell.Value = l
Selection.End(xlDown).Select
ActiveCell.Offset(1, 0).Select
Set rng = Range(ActiveCell.Address)
ActiveCell.Offset(1, -7).Select
l = 0

End If
Loop

rng.Activate
ActiveCell.Value = l
l = 0

Then I have this code for column J....

Range("J8").Select
Set rng = Range(ActiveCell.Address)
ActiveCell.Offset(1, -8).Range("A1").Select

Do Until ActiveCell.Value = ""
ActiveCell.Offset(0, 8).Range("A1").Select
If ActiveCell.Value < "" Then
l = ActiveCell.Value + l
ActiveCell.Offset(1, -8).Select
Else
rng.Activate
ActiveCell.Value = l
Selection.End(xlDown).Select
ActiveCell.Offset(1, 0).Select
Set rng = Range(ActiveCell.Address)
ActiveCell.Offset(1, -8).Select
l = 0

End If
Loop

rng.Activate
ActiveCell.Value = l
l = 0

TIA,
Chris


I have a question on this one. What is "i"? Since it's not defined
anywhere, does XL default to some value?

  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Combining VBA code for columns

i should have been 8. Thought I had corrected that.

--
Regards,
Tom Ogilvy

"davegb" wrote in message
ups.com...

Tom Ogilvy wrote:
Figuring out what you are doing and rewriting it would be the way to

improve
your code, but to reduce the repetative blocks:

for jj = 9 to 16
Cells(i,jj).Select
Set rng = Range(ActiveCell.Address)
ActiveCell.Offset(1, -7).Range("A1").Select

Do Until ActiveCell.Value = ""
ActiveCell.Offset(0, 7).Range("A1").Select
If ActiveCell.Value < "" Then
l = ActiveCell.Value + l
ActiveCell.Offset(1, -7).Select
Else
rng.Activate
ActiveCell.Value = l
Selection.End(xlDown).Select
ActiveCell.Offset(1, 0).Select
Set rng = Range(ActiveCell.Address)
ActiveCell.Offset(1, -7).Select
l = 0

End If
Loop

rng.Activate
ActiveCell.Value = l
l = 0
Next jj

--
Regards,
Tom Ogilvy


wrote in message
oups.com...
Hi-

I have the following code that I run for 8 consecutive
columns....columns I through P. As of now I am repeating this code 8
times (8 seperate blocks of code). This is obviously slowing the
process down quite a bit. My question is this: Is there a way to
combine all 8 columns into one block of code? Here is the code I am
using:

Range("I8").Select
Set rng = Range(ActiveCell.Address)
ActiveCell.Offset(1, -7).Range("A1").Select

Do Until ActiveCell.Value = ""
ActiveCell.Offset(0, 7).Range("A1").Select
If ActiveCell.Value < "" Then
l = ActiveCell.Value + l
ActiveCell.Offset(1, -7).Select
Else
rng.Activate
ActiveCell.Value = l
Selection.End(xlDown).Select
ActiveCell.Offset(1, 0).Select
Set rng = Range(ActiveCell.Address)
ActiveCell.Offset(1, -7).Select
l = 0

End If
Loop

rng.Activate
ActiveCell.Value = l
l = 0

Then I have this code for column J....

Range("J8").Select
Set rng = Range(ActiveCell.Address)
ActiveCell.Offset(1, -8).Range("A1").Select

Do Until ActiveCell.Value = ""
ActiveCell.Offset(0, 8).Range("A1").Select
If ActiveCell.Value < "" Then
l = ActiveCell.Value + l
ActiveCell.Offset(1, -8).Select
Else
rng.Activate
ActiveCell.Value = l
Selection.End(xlDown).Select
ActiveCell.Offset(1, 0).Select
Set rng = Range(ActiveCell.Address)
ActiveCell.Offset(1, -8).Select
l = 0

End If
Loop

rng.Activate
ActiveCell.Value = l
l = 0

TIA,
Chris


I have a question on this one. What is "i"? Since it's not defined
anywhere, does XL default to some value?



  #9   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 573
Default Combining VBA code for columns


Tom Ogilvy wrote:
i should have been 8. Thought I had corrected that.


Thanks, Tom!


--
Regards,
Tom Ogilvy

"davegb" wrote in message
ups.com...

Tom Ogilvy wrote:
Figuring out what you are doing and rewriting it would be the way to

improve
your code, but to reduce the repetative blocks:

for jj = 9 to 16
Cells(i,jj).Select
Set rng = Range(ActiveCell.Address)
ActiveCell.Offset(1, -7).Range("A1").Select

Do Until ActiveCell.Value = ""
ActiveCell.Offset(0, 7).Range("A1").Select
If ActiveCell.Value < "" Then
l = ActiveCell.Value + l
ActiveCell.Offset(1, -7).Select
Else
rng.Activate
ActiveCell.Value = l
Selection.End(xlDown).Select
ActiveCell.Offset(1, 0).Select
Set rng = Range(ActiveCell.Address)
ActiveCell.Offset(1, -7).Select
l = 0

End If
Loop

rng.Activate
ActiveCell.Value = l
l = 0
Next jj

--
Regards,
Tom Ogilvy


wrote in message
oups.com...
Hi-

I have the following code that I run for 8 consecutive
columns....columns I through P. As of now I am repeating this code 8
times (8 seperate blocks of code). This is obviously slowing the
process down quite a bit. My question is this: Is there a way to
combine all 8 columns into one block of code? Here is the code I am
using:

Range("I8").Select
Set rng = Range(ActiveCell.Address)
ActiveCell.Offset(1, -7).Range("A1").Select

Do Until ActiveCell.Value = ""
ActiveCell.Offset(0, 7).Range("A1").Select
If ActiveCell.Value < "" Then
l = ActiveCell.Value + l
ActiveCell.Offset(1, -7).Select
Else
rng.Activate
ActiveCell.Value = l
Selection.End(xlDown).Select
ActiveCell.Offset(1, 0).Select
Set rng = Range(ActiveCell.Address)
ActiveCell.Offset(1, -7).Select
l = 0

End If
Loop

rng.Activate
ActiveCell.Value = l
l = 0

Then I have this code for column J....

Range("J8").Select
Set rng = Range(ActiveCell.Address)
ActiveCell.Offset(1, -8).Range("A1").Select

Do Until ActiveCell.Value = ""
ActiveCell.Offset(0, 8).Range("A1").Select
If ActiveCell.Value < "" Then
l = ActiveCell.Value + l
ActiveCell.Offset(1, -8).Select
Else
rng.Activate
ActiveCell.Value = l
Selection.End(xlDown).Select
ActiveCell.Offset(1, 0).Select
Set rng = Range(ActiveCell.Address)
ActiveCell.Offset(1, -8).Select
l = 0

End If
Loop

rng.Activate
ActiveCell.Value = l
l = 0

TIA,
Chris


I have a question on this one. What is "i"? Since it's not defined
anywhere, does XL default to some value?


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
combining two columns together tadwestie Excel Discussion (Misc queries) 2 January 7th 10 10:21 AM
combining columns jason2444 Excel Discussion (Misc queries) 4 February 6th 09 06:47 PM
Combining Text from 2 Columns into 1 then Deleting the 2 Columns sleepindogg Excel Worksheet Functions 5 September 19th 08 12:36 AM
combining columns all the way down sparkroms Excel Discussion (Misc queries) 3 July 8th 05 08:28 PM
combining 2+ wkbks into 1. Code needs tweaking please ian123[_31_] Excel Programming 12 January 3rd 04 11:20 PM


All times are GMT +1. The time now is 05:37 PM.

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

About Us

"It's about Microsoft Excel"