+ Reply to Thread
Results 1 to 16 of 16

Using Union to combine ranges in VBA

  1. #1
    Registered User
    Join Date
    05-16-2014
    MS-Off Ver
    Excel 2003
    Posts
    13

    Question Using Union to combine ranges in VBA

    Hi,

    I have to copy and paste number of ranges from one spreadsheet to another for which I have written the below VBA


    Private Sub CommandButton1_Click()

    LastRow = ActiveSheet.Range("B" & Rows.Count).End(xlUp).Row

    Dim R1, R2, R3, R4, R5, R6, R7, Multiplerange As Range

    For i = 6 To LastRow

    If Cells(i, 18) = "Open" And Cells(i, 53) = "Escalated to JLR Senior Leadership" Then

    Set R1 = Range(Cells(i, 2), Cells(i, 2))
    Set R2 = Range(Cells(i, 4), Cells(i, 4))
    Set R3 = Range(Cells(i, 6), Cells(i, 16))
    Set R4 = Range(Cells(i, 58), Cells(i, 58))
    Set R5 = Range(Cells(i, 56), Cells(i, 56))
    Set R6 = Range(Cells(i, 20), Cells(i, 20))
    Union(R1, R2,R3,R4, R5, R6).Select
    '
    Selection.Copy

    Workbooks.Open Filename:="C:\Documents and Settings\ssachde1\My Documents\extract.xlsx"

    Dim p As Integer, q As Integer

    p = Worksheets.Count

    For q = 1 To p

    If ActiveWorkbook.Worksheets(q).Name = "Risk Log" Then
    Worksheets("Risk Log").Select
    End If

    Next q

    erow = ActiveSheet.Cells(Rows.Count, 6).End(xlUp).Offset(1, 0).Row
    ActiveSheet.Cells(erow, 2).Select
    ActiveSheet.Paste
    ActiveWorkbook.Save
    ActiveWorkbook.Close
    Application.CutCopyMode = False

    End If

    Next i

    End Sub


    The entire code works fine but the only problem I have is that 'union' changes the order of selection to R1, R2,R3,R6,R5,R4.

    How can keep the range in the order that I want.

    Please help me.

    Thanks

  2. #2
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,606

    Re: Using Union to combine ranges in VBA

    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code.

    Posting code between [CODE]Please [url=https://www.excelforum.com/login.php]Login or Register [/url] to view this content.[/CODE] tags makes your code much easier to read and copy for testing, it also maintains VBA formatting.

    Highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here



    (This thread should receive no further responses until this moderation request is fulfilled, as per Forum Rule 7)

  3. #3
    Registered User
    Join Date
    05-16-2014
    MS-Off Ver
    Excel 2003
    Posts
    13

    Re: Using Union to combine ranges in VBA

    I am really very sorry Stephen, I have never posted a code before.

    Please let me know if what I have done below is correct

    Please Login or Register  to view this content.

  4. #4
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,606

    Re: Using Union to combine ranges in VBA

    That's it, thanks. I think you'd probably have to loop through each range and copy in turn. Union, in effect, creates a single range so the different elements are lost.

  5. #5
    Registered User
    Join Date
    05-16-2014
    MS-Off Ver
    Excel 2003
    Posts
    13

    Re: Using Union to combine ranges in VBA

    I tried to do it but it does not seem to work. Your help will be appreciated.

  6. #6
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,199

    Re: Using Union to combine ranges in VBA

    Hi, sksachdev,

    using
    Please Login or Register  to view this content.
    will define R1 to R6 as Variant and only Multiplerange as Range which you donīt set nor use.

    Tryx it like this (altjhough I doubt I would close the workbook after each loop if opened:
    Please Login or Register  to view this content.
    Ciao,
    Holger
    Use Code-Tags for showing your code: [code] Your Code here [/code]
    Please mark your question Solved if there has been offered a solution that works fine for you

  7. #7
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,606

    Re: Using Union to combine ranges in VBA

    I made a stab at tidying up your code a little
    Please Login or Register  to view this content.

  8. #8
    Registered User
    Join Date
    05-16-2014
    MS-Off Ver
    Excel 2003
    Posts
    13

    Re: Using Union to combine ranges in VBA

    Thanks for your help HaHobe,

    I tried to paste the code but it does not seem to work.
    All the data is pasted into the 8th Column in Risk log. Rather than being pasted into row as per my code.

  9. #9
    Registered User
    Join Date
    05-16-2014
    MS-Off Ver
    Excel 2003
    Posts
    13

    Re: Using Union to combine ranges in VBA

    Thanks StephenR

    Your logic is working correctly but the only problem it is pasting each range in column 2 one after the other whereas the first range should be pasted in Column 2, then second range should be pasted in column 3 the third range should be pasted column 4, the fourth range should be pasted in column 13.
    I don't know how to attach the extracted file because that would have shown you clearly how it looks.

  10. #10
    Registered User
    Join Date
    05-16-2014
    MS-Off Ver
    Excel 2003
    Posts
    13

    Re: Using Union to combine ranges in VBA

    Thanks Stephen I understood the logic and have made the changes to the code and it works Brilliantly Thanks for your help and also HaHobe I am using his code somewhere else.
    Please Login or Register  to view this content.

  11. #11
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,199

    Re: Using Union to combine ranges in VBA

    Hi, sksachdev,

    I donīt get the reasoin for Checking Column F and starting on Column B, I would have guessed to check Column B and Offset from there.

    All the data is pasted into the 8th Column in Risk log.
    Commands look for the first free row in Log, offset one down, stat in Column B in thge altered code, write via offset to the nbext columns - canīt find the pasting to one cell (especially Column H) in my code.

    Please Login or Register  to view this content.
    Ciao,
    Holger

  12. #12
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,060

    Re: Using Union to combine ranges in VBA

    FWIW, you could also use a loop to copy:
    Please Login or Register  to view this content.
    Everyone who confuses correlation and causation ends up dead.

  13. #13
    Registered User
    Join Date
    05-16-2014
    MS-Off Ver
    Excel 2003
    Posts
    13

    Re: Using Union to combine ranges in VBA

    Thanks Romperstomper the code works fine.

    Appreciate help from all of you.

  14. #14
    Registered User
    Join Date
    05-16-2014
    MS-Off Ver
    Excel 2003
    Posts
    13

    Re: Using Union to combine ranges in VBA

    Hi Romperstomper, Stephen

    The code works fine but some of my cells are formulas how can I paste values only.

    Thanks

  15. #15
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,060

    Re: Using Union to combine ranges in VBA

    This:
    Please Login or Register  to view this content.
    would become:
    Please Login or Register  to view this content.

  16. #16
    Registered User
    Join Date
    05-16-2014
    MS-Off Ver
    Excel 2003
    Posts
    13

    Re: Using Union to combine ranges in VBA

    Thanks Romperstomper.

+ 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. Union of Named Ranges
    By justinv in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 11-12-2012, 12:52 PM
  2. Union Ranges Fail
    By Mordred in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-19-2011, 11:29 AM
  3. Union named Ranges
    By MrHockey in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 05-08-2009, 01:03 AM
  4. With for multiple ranges (not using Union)
    By beeawwb in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-29-2008, 09:07 PM
  5. printing Union of Ranges
    By anny in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 01-26-2006, 06:25 AM

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