Link to home
Start Free TrialLog in
Avatar of Wilder1626
Wilder1626Flag for Canada

asked on

VBA - Transfer Data from one sheet to another

Hi

I have an Excel file with 2 sheets in it.
"Template" and "Header".

Starting at row 7 of sheets "Template", i would like to transfer single value from column D to the other sheets.

The value from column D would go in column A and B on sheets "Header" and the value from Column E from sheet "Template" would go in column G on sheets "Header".
Ex:

sheets "Template"
User generated image

Result on Sheets "Header"
User generated image
The problem i have is that i cannot move the value from Column E from sheet "Template" to the column G on sheets "Header".

Private Sub CommandButton1_Click()
   Dim dict As Object, rCell As Range
    
    Set dict = CreateObject("Scripting.Dictionary")
    dict.CompareMode = vbTextCompare
        
    With Sheets("Template")
        For Each rCell In .Range("D7:D" & .Cells(.Rows.Count, "D").End(xlUp).Row)
            dict.Item(rCell.Value) = Empty
        Next rCell
    End With

    For Each V In dict.Keys()
        With Sheets("Header")
            .Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Value = V
            .Cells(Rows.Count, 2).End(xlUp).Offset(1, 0).Value = V
            .Cells(Rows.Count, 3).End(xlUp).Offset(1, 0).Value = "Test"

            .Cells(Rows.Count, 7).End(xlUp).Offset(1, 0).Value = Sheets("Template").Range("E" & .Cells(Rows))
     
        End With
    Next V
End Sub

Open in new window

How can i do that?

Thanks for your help.

Macro Transfer single value to other sheet.xlsm
Avatar of Martin Liss
Martin Liss
Flag of United States of America image

Try this.
Private Sub CommandButton1_Click()
   Dim dict As Object, rCell As Range
    Dim lngLastRow As Long
    
    Set dict = CreateObject("Scripting.Dictionary")
    dict.CompareMode = vbTextCompare
        
    With Sheets("Template")
        For Each rCell In .Range("D7:D" & .Cells(.Rows.Count, "D").End(xlUp).Row)
            dict.Item(rCell.Value) = Empty
        Next rCell
    End With

    For Each V In dict.Keys()
        With Sheets("Header")
            lngLastRow = .UsedRange.Rows.Count
            .Cells(lngLastRow, 1).End(xlUp).Offset(1, 0).Value = V
            .Cells(lngLastRow, 2).End(xlUp).Offset(1, 0).Value = V
            .Cells(lngLastRow, 3).End(xlUp).Offset(1, 0).Value = "Test"

            .Cells(lngLastRow, 7).End(xlUp).Offset(1, 0).Value = Sheets("Template").Range("E" & Sheets("Template").UsedRange.Rows.Count)
     
        End With
    Next V
End Sub

Open in new window

Avatar of Wilder1626

ASKER

Hi Martin, it's been a long time since.

Unfortunately, it just gives me the last record of column D "Template" and the other column does not match
User generated image

what it should be without talking about the column 3
User generated image
Please describe in words what should happen after the button is clicked.
After the click:
It should transfer single values that are in column D from Sheet "Template" into Sheet "Header" Column A and B. Then, also take the value from Column "E" of Sheet "Template"  of the same same row, and put this into Sheet "Header" Column "G".

No duplicate in sheet "Header" should be found.


Should any data in either sheet be cleared as the first step in the macro?
Just the sheet "Header" from row 2 going done would be clear before the macro transferred the data. The row one will contained the headers for each columns.
The Sheet "Template" must remained intact.
OK and I assume when you said "Then, also take the value from Column "E" of Sheet "Template"..." that you meant G and not E.
It's Column "E" from sheet "Template"
User generated imageGoes in Column "G" of sheet "Header"
User generated image
Oh, of course.
Hi,

This can be easilly solved by taking adventage of a collection instead of a dictionary (since collection keys must be unique), and a Pair class:

First, the Pair class:
    '// Pair class
Option Explicit

Private mFirst As Variant
Private mSecond As Variant

Friend Sub Construct(ByVal First As Variant, ByVal Second As Variant)
    mFirst = First
    mSecond = Second
End Sub

Public Property Get First() As Variant
    First = mFirst
End Property

Public Property Get Second() As Variant
    Second = mSecond
End Property

Open in new window

Second, a Factory module with an helper function to instanciate a Pair:
    '// Factory module
Option Explicit

Public Function Create_Pair(ByVal First As Variant, ByVal Second As Variant) As Pair
    Set Create_Pair = New Pair
    Create_Pair.Construct First, Second
End Function

Open in new window

Finally, the event handler attached to your button as well as a second helper function:
Option Explicit

Private Sub CommandButton1_Click()
    Dim Uniques As Collection
    Set Uniques = GetUniques
    
    Dim Wb As Excel.Workbook
    Set Wb = ThisWorkbook
    
    Dim Ws As Excel.Worksheet
    Set Ws = Wb.Worksheets("Header")
    
    Dim Rng As Excel.Range
    Set Rng = Ws.Range("A2")
    
    Dim Pair As Pair
    For Each Pair In Uniques
        Rng.Value = Pair.First
        Rng.Offset(ColumnOffset:=1).Value = Pair.First
        Rng.Offset(ColumnOffset:=6).Value = Pair.Second
        Set Rng = Rng.Offset(RowOffset:=1)
    Next
End Sub

    '// Build a collection of uniques items
Private Function GetUniques() As Collection
    Dim Uniques As Collection
    Set Uniques = New Collection

    Dim Rng As Excel.Range
    For Each Rng In Me.Range("D7:D" & Me.Cells(Me.Rows.Count, "D").End(xlUp).Row)
        Dim Pair As Pair
        Set Pair = Factory.Create_Pair(Rng.Value, Rng.Offset(ColumnOffset:=1).Value)
        On Error Resume Next
            '// attempt to insert a pair in the collection
            '// if it fail, ignore and move to the next
        Uniques.Add Pair, CStr(Pair.First)
        On Error GoTo 0
    Next
    Set GetUniques = Uniques
End Function

Open in new window


Hi Fabrice, Would you know why i'M getting this below error?
User generated image
ASKER CERTIFIED SOLUTION
Avatar of Martin Liss
Martin Liss
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I’m glad I was able to help. It's also nice when I can learn something new, which in this case was a better understanding of dictionaries.

If you expand the “Full Biography" section of my profile you’ll find links to some articles I’ve written that may interest you.

Marty - Microsoft MVP 2009 to 2017
              Experts Exchange Most Valuable Expert (MVE) 2015, 2017
              Experts Exchange Distinguished Expert in Excel 2018
              Experts Exchange Top Expert Visual Basic Classic 2012 to 2020
              Experts Exchange Top Expert VBA 2018 to 2020
Thanks for your help. It was also easy for me to tweak the macro as well to transfer an extra column.
Hi Fabrice, Would you know why i'M getting this below error?
You did not rename the required Pair class module, neither the required Factory module....
User generated image
Oh! I see. It works. This is interesting. Thanks