+ Reply to Thread
Results 1 to 6 of 6

Dynamic Ranges - Create Using VBA as looping through Sheets

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    07-11-2009
    Location
    NYC,USA
    MS-Off Ver
    Excel 2007
    Posts
    135

    Dynamic Ranges - Create Using VBA as looping through Sheets

    Hello,

    I’m attempting to grasp the concepts of working with arrays.
    • Is it possible to loop through a series of worksheets containing the same types of data
    • Select all the data on each sheet
    • Declare the data as a range dynamically ( if this is necessary)
    • Load that range into an array (nRows, 5Col)
    • And then perform actions based upon items in that array?

    Sub TestArray()
    
    Dim SheetData As Variant
    Dim lngRow As Long
    Dim lngCol As Long
    Dim SrcRng As Range
    
    SheetData = ThisWorkbook.Names("SrcRng").RefersToRange
        For lngRow = LBound(SheetData) To UBound(SheetData)
        For lngCol = LBound(SheetData, 2) To UBound(SheetData, 2)
        
     Debug.Print "Item ("; lngRow; ","; lngCol; ")", SheetData(lngRow, lngCol)
        
        Next
        Next
    Debug.Print
    
    End Sub
    I’ve attached a workbook with sample data and the code that I’ve managed to cobble together at this point.
    It doesn't loop through the worksheets nor dynamically create the data ranges.

    Thanks for your assistance.
    Mark
    Attached Files Attached Files

  2. #2
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Dynamic Ranges - Create Using VBA as looping through Sheets

    I wrote the attached code for another thread. It gives you some direction. I have put comment on each line. No every single line may be relevant to your case, it is just a guide.
    Attached Files Attached Files

  3. #3
    Forum Contributor
    Join Date
    07-11-2009
    Location
    NYC,USA
    MS-Off Ver
    Excel 2007
    Posts
    135

    Thumbs up Re: Dynamic Ranges - Create Using VBA as looping through Sheets

    Thanks a lot AB.
    It's an elegant piece of code.

    I had a problem with a "Subscript out of range" on this line.
    I commented out that particular "If" statement and the code ran fine.
     Sub ConsolidateData()
    Dim ws As Worksheet, wsMaster As Worksheet, x, z, i As Long, ii&, k&
    'Application.ScreenUpdating = 0
    Set wsMaster = Sheets("Master")
      x = Range("A1:E" & Cells.Find("*", , , , xlByRows, xlPrevious).Row) 'Load the data in to an array
         ReDim z(1 To UBound(x) * Sheets.Count - 1, 1 To UBound(x, 2)) 'output array
     For Each ws In Worksheets  'Loop through each sheet
        If ws.Name <> wsMaster.Name Then  ' if the sheet name is not "Master"
            x = ws.Range("A1:E" & ws.Cells.Find("*", , , , xlByRows, xlPrevious).Row)
            For i = 2 To UBound(x, 1)  'Loop through each array for each sheet at a time.
                 If Trim((x(i, 7))) = vbNullString Then
                
                     k = k + 1
                    For ii = 1 To UBound(x, 2) ' If the condition is true,loop through columns and copy data in to output array
                        z(k, ii) = x(i, ii)
                    Next
                 End If
            Next i
        End If
    Next ws
     With wsMaster
          .UsedRange.Offset(1).ClearContents
          .Range("A1").Resize(k, UBound(x, 2)) = z ' The adjusted array is copied back in to range.
          .Columns.AutoFit
     End With
    End Sub
    I was wondering what the intention was for this bit of code?
    When I changed the number from 7 to 5, I received the value of the date in column five.
    The problem was, it wouldn't output at all to the Master Worksheet.

    I'd be interested in understanding the mechanics of this.

    Thanks again.

  4. #4
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Dynamic Ranges - Create Using VBA as looping through Sheets

    If Trim((x(i, 7))) = vbNullString Then
    Simply testing if column G is nil. It is one of the condition for that particular task.

    The reason forgetting the error is this line.

     x = ws.Range("A1:E" & ws.Cells.Find("*", , , , xlByRows, xlPrevious).Row)
    The max column loop the X array can take is 5, which is E. If you change it to G, you would not get an error.

  5. #5
    Forum Contributor
    Join Date
    07-11-2009
    Location
    NYC,USA
    MS-Off Ver
    Excel 2007
    Posts
    135

    Re: Dynamic Ranges - Create Using VBA as looping through Sheets

    Thanks for the explanation.
    I didn't think to look back in the code to find the problem.
    A very useful bit of information.

    Thanks again.

  6. #6
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Dynamic Ranges - Create Using VBA as looping through Sheets

    I was wondering what the intention was for this bit of code?

    The person who asked the question wants to copy all columns if column E is blank. So, the condition is "If statement". Not all rows in column E are blank.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] To use name manager or create dynamic ranges in VBA
    By chrisjames25 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-19-2013, 03:37 PM
  2. How to create dynamic ranges, better alternative to OFFSET
    By JamesGoulding85 in forum Excel General
    Replies: 2
    Last Post: 06-04-2013, 03:27 PM
  3. [SOLVED] Create Multiple Dynamic Named Ranges with a Macro
    By bennank in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-09-2012, 12:55 PM
  4. Looping through dynamic ranges from multiple worksheets
    By TechWrangler in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 05-08-2006, 03:20 PM
  5. [SOLVED] Looping with Hidden and Protected Sheets/Ranges
    By ExcelMonkey in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-08-2005, 02:07 PM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1