VBA-issues refrencing multiple range selections

ddoctor

New Member
Joined
Aug 30, 2017
Messages
27
I'm trying to create a workbook where a range consisting of 4 columns is selected, and a macro is run to to duplicate information in the second column according to the amount of information input into the other three columns. For example:

I want to turn something like this:

<tbody>
</tbody>
8/29/2017ZacharyTest1Test1
8/30/2017AprilTest2Test2
8/31/2017JustinTest3Test3

<tbody>
</tbody>

Into This:
9/1/17ZacharyTest1Test1
9/2/17ZacharyTest2Test2
9/3/17ZacharyTest3Test3
9/1/17AprilTest1Test1
9/2/17AprilTest2Test2
9/3/17AprilTest3Test3
9/1/17JustinTest1Test1
9/2/17JustinTest1Test1
9/3/17JustinTest1Test1

<tbody>
</tbody>

As of right now using the code provide below, I'm only getting this:

9/1/17Zachary9/1/179/1/17
9/2/17Zachary9/2/179/2/17
9/3/17Zachary9/3/179/3/17
9/1/17April9/1/179/1/17
9/2/17April9/2/179/2/17
9/3/17April9/3/179/3/17
9/1/17Justin9/1/179/1/17
9/2/17Justin9/2/179/2/17
9/3/17Justin9/3/179/3/17

<tbody>
</tbody>

I've gotten this far because of the following code that someone from here gave me to play with. I have been messing with it and attempting to make it work for what I need because originally it only referenced 2 columns. After I figure this problem out, I'm going to be looking into ways to make it all happen on the same tab. I can probably figure that out myself, but I'm open to input on all things considered.

Code:
Public Sub CrossJoinSelection()
  Dim avntCartesian() As Variant
  Dim wksOutput As Worksheet
  Dim rngSelection As Range
  Dim lngCounter As Long
  Dim c1 As Range
  Dim c2 As Range
  Dim c3 As Range
  Dim c4 As Range
  
 
 Dim r1, r2, r3, myMultipleRange As Range
 Set r1 = Sheets("ss1").Range("c4")
 Set r2 = Sheets("ss1").Range("c3")
 Set r3 = Sheets("ss1").Range("c1")
 Set myMultipleRange = Union(r1, r2, r3)
 myMultipleRange.Font.Bold = True

  
  On Error GoTo ErrHandler
  If Not TypeOf Selection Is Range Then
    MsgBox "Selection must be a range.", vbExclamation
    GoTo ExitProc
  End If
  
  Set rngSelection = Intersect(Selection, Selection.Parent.UsedRange)
  If Not rngSelection Is Nothing Then
    For Each myMultipleRange In rngSelection.Columns(1).Cells
      If Not IsEmpty(myMultipleRange.Value) Then
        For Each c2 In rngSelection.Columns(2).Cells
            If Not IsEmpty(c2.Value) Then
                
               
          
            lngCounter = lngCounter + 1
            ReDim Preserve avntCartesian(1 To 4, 1 To lngCounter)
            avntCartesian(1, lngCounter) = myMultipleRange.Value
            avntCartesian(2, lngCounter) = c2.Value
            avntCartesian(3, lngCounter) = myMultipleRange.Value
            avntCartesian(4, lngCounter) = myMultipleRange.Value
                  End If
                Next c2
              End If
            Next myMultipleRange
       
    
  End If
  
  If lngCounter > 0 Then
    avntCartesian = TranposeArray(avntCartesian)
    Set wksOutput = ThisWorkbook.Sheets.Add
    wksOutput.Range("C13:F13").Value = Array("Column1", "Column2", "Column3", "Column4")
    wksOutput.Range("C13:F13").Resize(lngCounter).Value = avntCartesian
  Else
    MsgBox "No values found in selection.", vbExclamation
  End If
  
ExitProc:
  Set rngSelection = Nothing
  Set wksOutput = Nothing
  Set c1 = Nothing
  Set c2 = Nothing
  Exit Sub
  
ErrHandler:
  MsgBox Err.Description, vbExclamation
  Resume ExitProc
End Sub

Private Function TranposeArray(avntSource() As Variant) As Variant()
  Dim avntTarget() As Variant
  Dim intLower1 As Integer
  Dim intUpper1 As Integer
  Dim lngLower2 As Long
  Dim lngUpper2 As Long
  Dim i As Integer
  Dim j As Long
  
  intLower1 = LBound(avntSource, 1)
  intUpper1 = UBound(avntSource, 1)
  lngLower2 = LBound(avntSource, 2)
  lngUpper2 = UBound(avntSource, 2)
  
  ReDim avntTarget(lngLower2 To lngUpper2, intLower1 To intUpper1)
  For j = lngLower2 To lngUpper2
    For i = intLower1 To intUpper1
      avntTarget(j, i) = avntSource(i, j)
    Next i
  Next j
  
  TranposeArray = avntTarget
End Function

I appreciate what you guys do here! Thanks any help.
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
How do you get from the initial data in the table you posted to the result you want?

Is there any logic, patterns, rules?
 
Upvote 0
How do you get from the initial data in the table you posted to the result you want?

Is there any logic, patterns, rules?

I suppose that would be better answered by the person that provided it to me, but after working with the code it appears that this section is what generates the pattern needed.
Code:
 ReDim avntTarget(lngLower2 To lngUpper2, intLower1 To intUpper1)
  For j = lngLower2 To lngUpper2
    For i = intLower1 To intUpper1
      avntTarget(j, i) = avntSource(i, j)
    Next i
  Next j

Originally it looked at only the first to columns, so it appeared to make sure each individual date would only replicate itself according the to amount of names entered into the 2nd column.

Originally when I began playing with it to include 4 columns, I would end up with an extremely long list because it would list every possible arrangement.

The code I provided above was an attempt to group columns 1,3,and 4 together. As of right now it assigns the dates correctly, but doesn't include the data from columns 3,4.

I hope that answers your question, I'm pretty new to all of this so I'm pretty sure there was a better way to describe what I'm saying lol
 
Upvote 0

Forum statistics

Threads
1,215,518
Messages
6,125,293
Members
449,218
Latest member
Excel Master

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top