Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 80
Default Looping: Selecting elements from a named range to change calcs

Hi All.

I hope you can help.

I have a worksheet which is used to provide a number of reports in the same
format.

The values calculated depend elements selected in a cell from a validated
list, which is contained within a names range "Cost_Centre_Description".
There are 26 elements within the list.

When these selections are made manually and the worksheet only is
recalculated the figures are updated for the new cost centres data.

A print macro collapses grouped rows before printing the individual sheet.

I have generated the following Macro, by first recording and then editing.
I think that it can be shortened by first defining the named range as an
array and then using a for next loop to iterate the macro - but I cant get my
head around the syntax.

If someone could give me a couple hint I think that I could develop the
final code myself.

Thanks for looking
Regards
Phil

An extract of the code I have used is given below. 4 out of 26 elements are
shown.

Sub PrintPLDepartments()
'
' PrintPLDepartments Macro
' This macro is to sequentially print each of the elements
' in the list of cost centres
'Section 1 - Recalculates the Workbook and Selects the Report Type
Calculate
Application.Goto Reference:="ChosenReportType"
ActiveCell.FormulaR1C1 = "Profit Centre"
Range("C4").Select
'Section 2 - Repeated for each element in the report type.
'Element 1
Application.Goto Reference:="ChosenElementType"
ActiveCell.FormulaR1C1 = "Birmingham Retail"
Range("C6").Select
Application.MaxChange = 0.001
ActiveWorkbook.PrecisionAsDisplayed = False
ActiveSheet.Calculate
Application.Run "'Mgt accounts Master 2006
NEW.xls'!CollapseRowsB4Printing"
'Element 2
Application.Goto Reference:="ChosenElementType"
ActiveCell.FormulaR1C1 = "Birmingham Corporate Finance"
Range("C6").Select
Application.MaxChange = 0.001
ActiveWorkbook.PrecisionAsDisplayed = False
ActiveSheet.Calculate
Application.Run "'Mgt accounts Master 2006
NEW.xls'!CollapseRowsB4Printing"
'Element 3
Application.Goto Reference:="ChosenElementType"
ActiveCell.FormulaR1C1 = "East Lancs Retail"
Range("C6").Select
Application.MaxChange = 0.001
ActiveWorkbook.PrecisionAsDisplayed = False
ActiveSheet.Calculate
Application.Run "'Mgt accounts Master 2006
NEW.xls'!CollapseRowsB4Printing"
'Element 4
Application.Goto Reference:="ChosenElementType"
ActiveCell.FormulaR1C1 = "Cardiff 2 Retail"
Range("C6").Select
Application.MaxChange = 0.001
ActiveWorkbook.PrecisionAsDisplayed = False
ActiveSheet.Calculate
Application.Run "'Mgt accounts Master 2006
NEW.xls'!CollapseRowsB4Printing"
'Element 5

  #2   Report Post  
Posted to microsoft.public.excel.programming
Udo Udo is offline
external usenet poster
 
Posts: 48
Default Looping: Selecting elements from a named range to change calcs

Hi Philip,

it seems that you have always 4 loops to run. So I would do the
following:

Dim counter as Integer 'defines a simple variable which is used later
to
steer the loop

Application.goto reference:="Chosen Element Type"
For counter = 1 to 4
Select Case counter
Case 1
ActiveCell.FormulaR1C1="Profit Centre"
Case 2
ActiveCell.FormulaR1C1 = "Birmingham Corporate Finance"
Case 3
......
Case 4
.....
End Select 'this closes the Select Case part
'now comes what has to be done obviously every time in the same way
Range("C6").Select
Application.MaxChange = 0.001
ActiveWorkbook.PrecisionAsDisplayed = False
ActiveSheet.Calculate
Application.Run "'Mgt accounts Master 2006
NEW.xls'!CollapseRowsB4Printing"

Next counter 'this closes the for-loop

By the way: is it necessary to recalculate always or can you do that at
the end?

Hope this will help you

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 80
Default Looping: Selecting elements from a named range to change calcs

Thanks UDO, this will help for the 26 cases, but how do I select the items
from a list which may be variable?

"Udo" wrote:

Hi Philip,

it seems that you have always 4 loops to run. So I would do the
following:

Dim counter as Integer 'defines a simple variable which is used later
to
steer the loop

Application.goto reference:="Chosen Element Type"
For counter = 1 to 4
Select Case counter
Case 1
ActiveCell.FormulaR1C1="Profit Centre"
Case 2
ActiveCell.FormulaR1C1 = "Birmingham Corporate Finance"
Case 3
......
Case 4
.....
End Select 'this closes the Select Case part
'now comes what has to be done obviously every time in the same way
Range("C6").Select
Application.MaxChange = 0.001
ActiveWorkbook.PrecisionAsDisplayed = False
ActiveSheet.Calculate
Application.Run "'Mgt accounts Master 2006
NEW.xls'!CollapseRowsB4Printing"

Next counter 'this closes the for-loop

By the way: is it necessary to recalculate always or can you do that at
the end?

Hope this will help you


  #4   Report Post  
Posted to microsoft.public.excel.programming
Udo Udo is offline
external usenet poster
 
Posts: 48
Default Looping: Selecting elements from a named range to change calcs

If your data are given in a consistent list (i.e. no empty lines
between them), then you could apply the following function:
Function LastRow _
(objSheet As Worksheet, _
strStartCell As String) As Range
Dim objRange As Range
Dim lngLastRow As Long
Dim lngLastCol As long
Set objRange = objSheet _
.Range(strStartCell).CurrentRegion
lngLastRow = objrange.Row + _
objRange.rows.count - 1
lngLastCol = objRange.Column + _
objRange.columns.Count - 1
With objSheet
Set Lastrow = .Range _
(.Cells(lngLastRow, objRange.Column), _
.Cells(lngLastRow, lngLastCol))
End with
End Function

You call the function from within your previous code by just entering
its name followed by the required information like
Rows = LastRow(Sheets("Variable List"), "C3").select

That would require, that you have that variable list copied into a
sheet within your active workbook named "Variable List". If this list
is in another workbook (assume it is ListFile.xls stored in c:\Temp),
the code would be something like:
Dim ListSource as Workbook
<main code here
set ListSource = workbooks.Open("c:\temp\ListFile.xls")
rows = LastRow(ListSource.Worksheets("Variable List"), "C3").select

Then, in the code we had established before, you would write:

For counter = 1 to Rows
......

With that you should be able to cope with your problem.

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 80
Default Looping: Selecting elements from a named range to change calcs

Udo.
Thanks for this: I'll study the syntax and adapt as necessary.
REgards
Phil

"Udo" wrote:

If your data are given in a consistent list (i.e. no empty lines
between them), then you could apply the following function:
Function LastRow _
(objSheet As Worksheet, _
strStartCell As String) As Range
Dim objRange As Range
Dim lngLastRow As Long
Dim lngLastCol As long
Set objRange = objSheet _
.Range(strStartCell).CurrentRegion
lngLastRow = objrange.Row + _
objRange.rows.count - 1
lngLastCol = objRange.Column + _
objRange.columns.Count - 1
With objSheet
Set Lastrow = .Range _
(.Cells(lngLastRow, objRange.Column), _
.Cells(lngLastRow, lngLastCol))
End with
End Function

You call the function from within your previous code by just entering
its name followed by the required information like
Rows = LastRow(Sheets("Variable List"), "C3").select

That would require, that you have that variable list copied into a
sheet within your active workbook named "Variable List". If this list
is in another workbook (assume it is ListFile.xls stored in c:\Temp),
the code would be something like:
Dim ListSource as Workbook
<main code here
set ListSource = workbooks.Open("c:\temp\ListFile.xls")
rows = LastRow(ListSource.Worksheets("Variable List"), "C3").select

Then, in the code we had established before, you would write:

For counter = 1 to Rows
......

With that you should be able to cope with your problem.




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
Problem selecting a named range kkknie[_180_] Excel Programming 0 August 11th 04 08:30 PM
Deleting named ranges by looping through range collection agarwaldvk[_11_] Excel Programming 3 August 3rd 04 01:00 AM
Further Help Req'd - Looping thru cells in named range Michael Beckinsale Excel Programming 4 September 2nd 03 08:18 PM
Looping thru cells in a named range Michael Beckinsale Excel Programming 4 September 2nd 03 02:07 PM
looping cells though a named range Jo[_4_] Excel Programming 1 August 20th 03 12:32 AM


All times are GMT +1. The time now is 05:21 AM.

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"