+ Reply to Thread
Results 1 to 4 of 4

Need help modifying multiple range code to include LastRow ("B2:B10",..) -> ("B2:B" & LR.)

Hybrid View

  1. #1
    Registered User
    Join Date
    03-03-2016
    Location
    Baltimore, USA
    MS-Off Ver
    2007
    Posts
    15

    Need help modifying multiple range code to include LastRow ("B2:B10",..) -> ("B2:B" & LR.)

    Hi
    If I use multiple range selection as ("B2:B1000, G2:G1000, L2:L1000, Q2:Q1000"), the macro works fine. However, when I try to use ("B2:B" & LastRow, "G2:G" & LastRow.....) I get an error on that line. LastRow was correctly defined. I also tried using the Union.

    The code will create a list from given ranges in "Sheet2" and write to "Result2" Column A along with a count of duplicates in column B My knowledge in VBA is minimal. Any help is appreciated. Many thanks in advance.
    Note: I'm not sure of the original poster of the code, bad of me.

    Sub Testing()
        Dim vCell As Range
        Dim vRng() As Variant
        Dim i As Integer
    '    Dim xRng As Range
    '    Dim r1 As Range, r2 As Range, r3 As Range, r4 As Range
        
        ReDim vRng(0 To 0) As Variant
    
        Sheets("Result2").Cells.Delete
        Sheets("Sheet2").Select
        LastRow = ActiveSheet.UsedRange.Rows(ActiveSheet.UsedRange.Rows.Count).Row
        Cells(15, 1) = LastRow   ' to see LastRow is generated
    
          For Each vCell In Sheets("Sheet2").Range("B2:B10,G2:G10,L2:L10,Q2:Q10")
    
            If vCell.Value <> "" Then
                ReDim Preserve vRng(0 To i) As Variant
                vRng(i) = vCell.Value
                i = i + 1
            End If
        Next
    
        vRng = CountDuplicates(vRng)
    
        Sheets("Result2").Select
        Range(Cells(1, 1), Cells(UBound(vRng), UBound(vRng, 2))) = vRng
        Rows(1).Insert
        Range("A1:B1") = Array("Name", "Entries")
        ActiveSheet.UsedRange.Sort Range("B1"), xlDescending
    End Sub

  2. #2
    Forum Guru Winon's Avatar
    Join Date
    02-20-2007
    Location
    East Rand, R.S.A.
    MS-Off Ver
    2010
    Posts
    6,113

    Re: Need help modifying multiple range code to include LastRow ("B2:B10",..) -> ("B2:B" &

    Hello sureng20,

    Normally you would get much faster responses if you attach a sample workbook, as we do not usually like to re-event the wheel, so to speak.

    Make sure there is just enough data to demonstrate your need.

    Include a BEFORE sheet and an AFTER sheet in the workbook if needed to show the process you're trying to complete or automate. Make sure your desired results are shown, mock them up manually if necessary.

    Remember to desensitize the data.

    Click on GO ADVANCED and then scroll down to Manage Attachments to open the upload window.

    Regards.
    Please consider:

    Be polite. Thank those who have helped you. Then Click on the star icon in the lower left part of the contributor's post and add Reputation. Cleaning up when you're done. If you are satisfied with the help you have received, then Please do Mark your thread [SOLVED] .

  3. #3
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Need help modifying multiple range code to include LastRow ("B2:B10",..) -> ("B2:B" &

    Hi,

    You need the commas inside the address string
    ("B2:B" & LastRow & ",G2:G" & LastRow & ",.....")
    rather than
    ("B2:B" & LastRow, "G2:G" & LastRow.....)
    Don
    Please remember to mark your thread 'Solved' when appropriate.

  4. #4
    Registered User
    Join Date
    03-03-2016
    Location
    Baltimore, USA
    MS-Off Ver
    2007
    Posts
    15

    Re: Need help modifying multiple range code to include LastRow ("B2:B10",..) -> ("B2:B" &

    Thank you very much, Xinitwit. Placement of the commas made the difference.
    Winon, thanks for your suggestion. Next time I'll attach a sample workbook.
    Many thanks again.

+ 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. Replies: 5
    Last Post: 02-05-2019, 12:03 AM
  2. Replies: 2
    Last Post: 07-24-2017, 02:19 AM
  3. [SOLVED] Column X-Ref list - Sheet1 Col A "pages", Col B:FL "Req" to Sheet2 ColA "req", ColB "page"
    By excel-card-pulled in forum Excel Formulas & Functions
    Replies: 10
    Last Post: 04-07-2017, 09:30 AM
  4. Replies: 4
    Last Post: 11-17-2013, 12:05 PM
  5. Replies: 2
    Last Post: 06-06-2013, 12:45 PM
  6. Help Needed to get Filters Using "Include" and "Exclude" multiple values on one column.
    By alfykunable in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-23-2012, 04:03 AM
  7. Macro Error with code "Range("A65536").End(xlUp).EntireRow.Insert"
    By lukasj13 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 09-30-2010, 08:48 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