PC Review


Reply
Thread Tools Rate Thread

Combine two ranges with different numbers of rows and columns

 
 
Ciprian
Guest
Posts: n/a
 
      25th Mar 2010
Hi,

I have two different ranges with different numbers of rows and columns. I
have to combine in a single range each row from the first range with each row
from the second range. I provide hereunder an example. Any idea? Thank you!

Range 1
A B C
D E F
G H I
J K L

Range 2

1 2 3 4
5 6 7 8

Result

A B C 1 2 3 4
A B C 5 6 7 8
D E F 1 2 3 4
D E F 5 6 7 8
G H I 1 2 3 4
G H I 5 6 7 8
J K L 1 2 3 4
J K L 5 6 7 8



 
Reply With Quote
 
 
 
 
Jacob Skaria
Guest
Posts: n/a
 
      25th Mar 2010
Try the below code...Assume that the two ranges mentioned are named ranges...

Sub Macro()

Dim lngRow As Long
Dim rng1 As Range, rng2 As Range
Dim rngRow1 As Range, rngRow2 As Range

Set rng1 = Range("myrange1")
Set rng2 = Range("myrange2")

For Each rngRow1 In rng1.Rows
For Each rngRow2 In rng2.Rows
lngRow = lngRow + 1
Sheets("Sheet2").Range("A" & lngRow).Resize(1, _
rngRow1.Columns.Count) = rngRow1.Value
Sheets("Sheet2").Range("A" & lngRow).Offset(, _
rngRow1.Columns.Count).Resize(1, rngRow2.Columns.Count) = rngRow2.Value
Next
Next

End Sub


--
Jacob


"Ciprian" wrote:

> Hi,
>
> I have two different ranges with different numbers of rows and columns. I
> have to combine in a single range each row from the first range with each row
> from the second range. I provide hereunder an example. Any idea? Thank you!
>
> Range 1
> A B C
> D E F
> G H I
> J K L
>
> Range 2
>
> 1 2 3 4
> 5 6 7 8
>
> Result
>
> A B C 1 2 3 4
> A B C 5 6 7 8
> D E F 1 2 3 4
> D E F 5 6 7 8
> G H I 1 2 3 4
> G H I 5 6 7 8
> J K L 1 2 3 4
> J K L 5 6 7 8
>
>
>

 
Reply With Quote
 
Rick Rothstein
Guest
Posts: n/a
 
      25th Mar 2010
Here is another macro for you to consider (less looping)...

Sub CombineRanges()
Dim X As Long, Rng1 As Range, Rng2 As Range, StartCell As Range
Set Rng1 = Range("Range1")
Set Rng2 = Range("Range2")
Set StartCell = Range("A1")
Rng2.Copy StartCell.Offset(, Rng1.Columns.Count).Resize( _
Rng1.Rows.Count * Rng2.Rows.Count, Rng2.Columns.Count)
For X = 1 To Rng2.Rows.Count * Rng2.Rows.Count
Rng1.Rows(X).Copy StartCell.Offset((X - 1) * _
Rng2.Rows.Count).Resize(Rng2.Rows.Count)
Next
End Sub

Like Jacob's macro, I'm assuming you named the two ranges that you want to
combine as indicated. Of course, you can set specific ranges, rather than
named ranges, in the two Set statements if desired.

--
Rick (MVP - Excel)


"Ciprian" <(E-Mail Removed)> wrote in message
news:629F5FFC-BD29-4B61-AF2D-(E-Mail Removed)...
> Hi,
>
> I have two different ranges with different numbers of rows and columns. I
> have to combine in a single range each row from the first range with each
> row
> from the second range. I provide hereunder an example. Any idea? Thank
> you!
>
> Range 1
> A B C
> D E F
> G H I
> J K L
>
> Range 2
>
> 1 2 3 4
> 5 6 7 8
>
> Result
>
> A B C 1 2 3 4
> A B C 5 6 7 8
> D E F 1 2 3 4
> D E F 5 6 7 8
> G H I 1 2 3 4
> G H I 5 6 7 8
> J K L 1 2 3 4
> J K L 5 6 7 8
>
>
>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Not just remove dup rows but combine columns Colin Microsoft Excel Worksheet Functions 1 14th Jan 2010 03:56 AM
need a macro to combine rows, columns mrsjcd3 Microsoft Excel Programming 5 17th Sep 2009 01:44 PM
Combine multiple columns into two long columns, Repeating rows in first column anasab@gmail.com Microsoft Excel Misc 2 31st Jul 2006 09:45 PM
Combine multiple columns into two long columns, Repeating rows in first column anasab@gmail.com Microsoft Excel Misc 0 31st Jul 2006 05:07 PM
Combine 2 columns of numbers (A+B=C) Ledge Microsoft Excel Programming 6 6th Apr 2006 10:24 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 02:31 PM.