Results 1 to 5 of 5

Thread: [UNRESOLVED] variable range selection determined on cell value?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 2006
    Posts
    96

    [UNRESOLVED] variable range selection determined on cell value?

    Hi

    I have a bit of a complictaed problem (or at least complicated for a person with me novice VB programming skills)

    I have a report in Excel, generated from an OLAP-viewer. I need to restructure the data so it is presented as I would like it. The problem is determining the range which I need to copy to a new sheet.

    In my report I will be experiencing different amounts of rows in column B (Classic OLAP hierachical view - column A=group, cloumn B=subgroup, rest of the columns consists of the actual figures), as the number of subgroups may vary.

    Column A Column B

    BFORR GICS
    a
    b
    c
    d
    e
    f

    W3744 GICS
    a
    b
    c
    e

    SO - what i need is help with a code that will do something like this;

    If range("B5")=GISC then range BFORR= ("B5: next cell in column B="GISC" (this cell not included in the range)).
    After this I need to be able to copy it over in a new worksheet in a final structure.


    Is this by any means possible??

    /Nicolaj
    Last edited by direktoren; Feb 7th, 2006 at 02:26 AM.

  2. #2
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: variable range selection determined on cell value?

    Yes it shouldnt be a problem... but I need more information...
    or really a better example of what u need...

    basically using a for next loop... find the row with GCIS in it.. then save the range in a string


    VB Code:
    1. For x = 1 To 10
    2.      If range("B" & x) = "GCIS" Then
    3.           rangetosave = "B" & x
    4.      End If
    5. Next
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Feb 2006
    Posts
    96

    Re: variable range selection determined on cell value?

    I have attached a sample report from the OLAP-viewer just to clarify my problems a bit (dont mind the language - it is in danish)

    As you can see column A is the main group and column B then shows the subgroupings of column A. However, as you can see, these subgroups can change in number (from 1 to 7).

    What i need is a way to define a range, which in width is given as A to R, but in height should take whatever the size of the maingroup is. For instance my range for maingroup "BFORR" would in this example be ("A2:R8"), but next time I generate the report I might be missing the subgroup "private", then the range copied should be ("A2:R7")

    After this, I will need to copy some of the specific range into a new sheet in a desired order...

    hope this gave a bit of clarity...

    /Nicolaj
    Attached Files Attached Files

  4. #4
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: [UNRESOLVED] variable range selection determined on cell value?

    Excel question moved to Office Development

  5. #5
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: [UNRESOLVED] variable range selection determined on cell value?

    direktoren
    The following code will, I hope, give you what you need..


    VB Code:
    1. Sub SelectGroupReport()
    2. Dim wksReport As Worksheet
    3. Dim rngStart As Range
    4. Dim iStartCol As Integer
    5. Dim iStartRow As Integer
    6. Dim iEndCol As Integer
    7. Dim iEndRow As Integer
    8. Dim rngGroupReport As Range
    9. Dim iReportCount As Integer
    10. Dim iReportNumber As Integer
    11.    
    12.     Set wksReport = ThisWorkbook.Sheets("Sheet1")
    13.    
    14.     'rngStart is going to be the top-left cell in the subreport.
    15.     'I'm assuming that this will always being in A2.
    16.     Set rngStart = Range("A2")
    17.    
    18.     'The subreport range starts in column 1 (A)...
    19.     iStartCol = 1
    20.     '..and ends in column 18 (R)
    21.     iEndCol = 18
    22.    
    23.     'Determine how many subreports will be needed by counting
    24.     'non-blank cells in column A
    25.     iReportCount = Application.WorksheetFunction.CountA(wksReport.Range("A:A"))
    26.    
    27.     'Main loop to go through each subreport
    28.     For iReportNumber = 1 To iReportCount
    29.        
    30.         'Get the row number that will be the top(start) of the required subreport range
    31.         iStartRow = rngStart.Row
    32.        
    33.         'Now determine the bottom(end) of the required subreport range
    34.         If iReportNumber = iReportCount Then
    35.             'For the last report - End(xlDown) will go all the way to
    36.             'row 65536, so we look for the end of row B by using the offset
    37.            
    38.             iEndRow = rngStart.Offset(0, 1).End(xlDown).Row
    39.        
    40.         ElseIf rngStart.Offset(1, 0).Value = "" Then
    41.             'For cases where we have more that one row we use
    42.             'End(xlDown) to determin the position of the next header
    43.             'and set our ending row to the row above that.
    44.            
    45.             iEndRow = rngStart.End(xlDown).Offset(-1, 0).Row
    46.        
    47.         Else
    48.             'when the cell below our starting cell is populated
    49.             'the report should only have a single row
    50.             'so we set the end row to be the same as the start row
    51.            
    52.             iEndRow = iStartRow
    53.         End If
    54.        
    55.         'We can now set the sub report range
    56.         With wksReport
    57.             Set rngGroupReport = .Range(.Cells(iStartRow, iStartCol), .Cells(iEndRow, iEndCol))
    58.         End With
    59.        
    60.        
    61.         'insert your code here to handle each report
    62.         'the following is just to prove that the code is working
    63.         rngGroupReport.Select
    64.         MsgBox "Report " & CStr(iReportNumber)
    65.        
    66.         'Move to the next report
    67.         'using the same logic as above
    68.         If rngStart.Offset(1, 0).Value = "" Then
    69.             Set rngStart = rngStart.End(xlDown)
    70.         Else
    71.             Set rngStart = rngStart.Offset(1, 0)
    72.         End If
    73.     Next iReportNumber
    74.    
    75.     'Clear object variables
    76.     Set rngGroupReport = Nothing
    77.     Set wksReport = Nothing
    78. End Sub
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width