+ Reply to Thread
Results 1 to 16 of 16

Using Union to combine ranges in VBA

Hybrid View

  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] [/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

    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

  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
    Dim R1, R2, R3, R4, R5, R6, R7, Multiplerange As Range
    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:
    Private Sub CommandButton1_Click()
    
    Dim ws As Worksheet
    
    Set ws = ActiveSheet
    LastRow = ws.Range("B" & Rows.Count).End(xlUp).Row
    
    For i = 6 To LastRow
    
      If ws.Cells(i, 18) = "Open" And ws.Cells(i, 53) = "Escalated to JLR Senior Leadership" Then
      
        Workbooks.Open Filename:="C:\Documents and Settings\ssachde1\My Documents\extract.xlsx"
        
        On Error Resume Next
        Worksheets("Risk Log").Select
        If Err <> 0 Then
          MsgBox "could not find sheet 'Risk Log'!"
          ActiveWorkbook.Close
          End
        End If
        On Error GoTo 0
      
        With ActiveSheet.Cells(Rows.Count, 6).End(xlUp).Offset(1, 0)
          .Value = ws.Cells(i, 2).Value
          .Offset(0, 1).Value = ws.Cells(i, 4)
          .Offset(0, 2).Value = ws.Cells(i, 16)
          .Offset(0, 3).Value = ws.Cells(i, 58)
          .Offset(0, 4).Value = ws.Cells(i, 56)
          .Offset(0, 5).Value = ws.Cells(i, 20)
        End With
        ActiveWorkbook.Close True
      End If
    
    Next i
    
    End Sub
    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
    Private Sub CommandButton1_Click()
    
    Dim R1 As Range, R2 As Range, R3 As Range, R4 As Range, R5 As Range, R6 As Range, R7 As Range
    Dim LastRow As Long, wb1 As Workbook, ws As Worksheet, i As Long
    
    Set ws = ThisWorkbook.ActiveSheet
    LastRow = ws.Range("B" & Rows.Count).End(xlUp).Row
    
    Set wb1 = Workbooks.Open(Filename:="C:\Documents and Settings\ssachde1\My Documents\extract.xlsx")
    
    For i = 6 To LastRow
        If ws.Cells(i, 18) = "Open" And ws.Cells(i, 53) = "Escalated to JLR Senior Leadership" Then
            With ws
                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))
            End With
            
            With wb1.Worksheets("Risk Log")
                R1.Copy .Cells(.Rows.Count, 2).End(xlUp)(2)
                R2.Copy .Cells(.Rows.Count, 2).End(xlUp)(2)
                R3.Copy .Cells(.Rows.Count, 2).End(xlUp)(2)
                R4.Copy .Cells(.Rows.Count, 2).End(xlUp)(2)
                R5.Copy .Cells(.Rows.Count, 2).End(xlUp)(2)
                R6.Copy .Cells(.Rows.Count, 2).End(xlUp)(2)
            End With
        End If
    Next i
    
    wb1.Save
    wb1.Close
    
    End Sub

  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
    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.

    Private Sub CommandButton1_Click()
    
    Dim ws As Worksheet, i As Long
    
    Set ws = ActiveSheet
    
    For i = 6 To ws.Range("B" & Rows.Count).End(xlUp).Row
    
      If ws.Cells(i, 18) = "Open" And ws.Cells(i, 53) = "Escalated to JLR Senior Leadership" Then
      
        Workbooks.Open Filename:="C:\Documents and Settings\ssachde1\My Documents\extract.xlsx"
        
        On Error Resume Next
        Worksheets("Risk Log").Select
        If Err <> 0 Then
          MsgBox "could not find sheet 'Risk Log'!"
          ActiveWorkbook.Close
          End
        End If
        On Error GoTo 0
      
        With ActiveSheet.Cells(Rows.Count, 2).End(xlUp).Offset(1, 0)
          .Value = ws.Cells(i, 2).Value
          .Offset(0, 1).Value = ws.Cells(i, 4)
          .Offset(0, 2).Value = ws.Cells(i, 16)
          .Offset(0, 3).Value = ws.Cells(i, 58)
          .Offset(0, 4).Value = ws.Cells(i, 56)
          .Offset(0, 5).Value = ws.Cells(i, 20)
        End With
        ActiveWorkbook.Close True
      End If
    
    Next i
    
    End Sub
    Ciao,
    Holger

  11. #11
    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.
     
                R1.Copy .Cells(.Rows.Count, 2).End(xlUp)(2)
                R2.Copy .Cells(.Rows.Count, 3).End(xlUp)(2)
                R3.Copy .Cells(.Rows.Count, 4).End(xlUp)(2)
                R4.Copy .Cells(.Rows.Count, 15).End(xlUp)(2)
                R5.Copy .Cells(.Rows.Count, 16).End(xlUp)(2)
                R6.Copy .Cells(.Rows.Count, 17).End(xlUp)(2)

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

    Re: Using Union to combine ranges in VBA

    FWIW, you could also use a loop to copy:
    Private Sub CommandButton1_Click()
    
        Dim R(1 To 6)             As Range
        Dim LastRow               As Long
        Dim wb1                   As Workbook
        Dim ws                    As Worksheet
        Dim i                     As Long
        Dim lngPasteRow           As Long
        Dim lngPasteColumn        As Long
        Dim n                     As Long
    
        Set ws = ThisWorkbook.ActiveSheet
        LastRow = ws.Range("B" & Rows.Count).End(xlUp).Row
    
        Set wb1 = Workbooks.Open(Filename:="C:\Documents and Settings\ssachde1\My Documents\extract.xlsx")
    
        For i = 6 To LastRow
            If ws.Cells(i, 18).Value = "Open" And ws.Cells(i, 53).Value = "Escalated to JLR Senior Leadership" Then
                With ws
                    Set R(1) = .Range(.Cells(i, 2), .Cells(i, 2))
                    Set R(2) = .Range(.Cells(i, 4), .Cells(i, 4))
                    Set R(3) = .Range(.Cells(i, 6), .Cells(i, 16))
                    Set R(4) = .Range(.Cells(i, 58), .Cells(i, 58))
                    Set R(5) = .Range(.Cells(i, 56), .Cells(i, 56))
                    Set R(6) = .Range(.Cells(i, 20), .Cells(i, 20))
                End With
    
                With wb1.Worksheets("Risk Log")
                    lngPasteColumn = 1
                    lngPasteRow = .Cells(.Rows.Count, 6).End(xlUp).Row + 1
                    For n = 1 To 6
                        R(n).Copy .Cells(lngPasteRow, lngPasteColumn)
                        lngPasteColumn = lngPasteColumn + R(n).Count
                    Next n
                End With
            End If
        Next i
    
        wb1.Save
        wb1.Close
    
    End Sub
    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,063

    Re: Using Union to combine ranges in VBA

    This:
                    For n = 1 To 6
                        R(n).Copy .Cells(lngPasteRow, lngPasteColumn)
                        lngPasteColumn = lngPasteColumn + R(n).Count
                    Next n
    would become:
                    For n = 1 To 6
                        R(n).Copy 
                        .Cells(lngPasteRow, lngPasteColumn).PasteSpecial xlPasteValues
                        Application.CutCopyMode = False
                        lngPasteColumn = lngPasteColumn + R(n).Count
                    Next n

  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