shonu1320

Board Regular
Joined
Mar 10, 2011
Messages
54
I want to combine my weekly reports into one master file. After I copy paste them into one sheet, I have to go through them manually to match all the rows. There can be 7000 plus rows and it takes all day to manually fix them. What can I do to automate it? V lookup only work on one column. I want to look up 3 values(3 columns). Can I use macros? Any advices? Thanks!
 
Last edited:

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Your explanation of your issue is to vague to respond with a solution. Suggest you explain in simple English exactly what you are trying to do. Showing examples of what you have and and what you wish to accomplish will help to get you a solution. We need more information.
 
Upvote 0
A quick and dirty solution would be to add a new column, and in it combine the three columns you are trying to look up. You can then use a VLOOKUP against the added column.

=A1&"-"&B1&"-"&C1

The added dashes are optional but are an easy way to keep track.

There's probably a more elegant way to do this, but I am sure some of the more experienced gurus can help you with that.
 
Upvote 0
I understand that it is hard to understand. I have a sample but I am not sure how to attach file in the forum.

So I have three spreadsheets. I want to combine them into 1 but need to make sure data aligns. For example, below I have copied data from all three files into 1 sheet. But I want value of column 4 & 5 of all 3 files match with the column 1,2and 3 AND on the same row. So in my master file. I will have columns State, DOC, Type, Begin1, end 1, begin 2, end 2, begin 3 and end 3. Where the data is not available, it should leave it blank.
Below Example shows my raw data that I just copy pasted next to each other.
StateDOCTypeBegin 1End 1StateDOCTypeBegin 2End 2StateDOCTypeBegin 3End 3
ChicagoPA177ChicagoPA166ChicagoPA154
ChicagoPA2 ChicagoPA210ChicagoPA210
ChicagoPA31212ChicagoPA32016ChicagoPA332
ChicagoPB355ChicagoPB399ChicagoPB355
ChicagoPC1 ChicagoPD1166158ChicagoPC322
ChicagoPC311ChicagoPD29185ChicagoPD1210204
ChicagoPD4 ChicagoPD3613611ChicagoPD2107101
ChicagoPD1158154ChicagoPE344ChicagoPD3658657

<tbody>
</tbody><colgroup><col><col><col><col><col><col><col><col><col><col><col><col><col><col><col><col><col></colgroup>




This is how the final result should be
StateDOCTypeBegin 1End 1Begin 2End 2Begin 3End 3
ChicagoPA1776654
ChicagoPA2 1010
ChicagoPA31212201632
ChicagoPB3559955
ChicagoPC1
ChicagoPC311 22
ChicagoPC4
ChicagoPD1158154166158210204


<colgroup><col width="64" style="width: 48pt;" span="9">
<tbody>



































































































</tbody>
 
Upvote 0
I have a sample but I am not sure how to attach file in the forum.
You can't, but see here: Attachments


So I have three spreadsheets.
Are they all in the same workbook? If so, what are their names? If not, how do we locate & identify the relevant sheets?


I want to combine them into 1 ..
Does the destination sheet already exist? If so, what is its name and does it already contain data that must be retained?
 
Upvote 0
In relation to my own questions, I have made these assumptions:
- All 3 data sheets are in the same workbook. Names are "Data1", "Data2" & "Data3", but can be easily changed in the code where indicated.
- The results go on sheet "Summary" in the same workbook (Again the name can be changed in the code) & this sheet has nothing in it that needs to be kept.

If those assumptions happen to be correct, then you could try this in a copy of your workbook.

Rich (BB code):
Sub Combine_And_Align()
  Dim d As Object
  Dim ws As Worksheet
  Dim a As Variant, b As Variant
  Dim i As Long, k As Long, rw As Long
  Dim SDT As String
  
  Set d = CreateObject("Scripting.Dictionary")
  d.CompareMode = 1
  ReDim b(1 To Rows.Count, 1 To 9)
  For Each ws In Sheets(Array("Data1", "Data2", "Data3")) '<- Data sheet names here
    a = ws.Range("A1").CurrentRegion.Value
    For i = 2 To UBound(a)
      SDT = a(i, 1) & "|" & a(i, 2) & "|" & a(i, 3)
      If Not d.exists(SDT) Then
        rw = rw + 1
        d(SDT) = rw
        b(rw, 1) = a(i, 1): b(rw, 2) = a(i, 2): b(rw, 3) = a(i, 3)
      End If
      b(d(SDT), 4 + k) = a(i, 4)
      b(d(SDT), 5 + k) = a(i, 5)
    Next i
    k = k + 2
  Next ws
  With Sheets("Summary")                                  '<- Summary sheet name here
    .UsedRange.ClearContents
    With .Range("A2").Resize(rw, UBound(b, 2))
      .Value = b
      .Cells(0, 1).Resize(, 9).Value = Array("State", "DOC", "Type", "Begin 1", "End 1", "Begin 2", "End 2", "Begin 3", "End 3")
      .CurrentRegion.EntireColumn.AutoFit
    End With
  End With
End Sub

The resultant 'Summary' sheet for me is ..


Excel 2016 (Windows) 32 bit
ABCDEFGHI
1StateDOCTypeBegin 1End 1Begin 2End 2Begin 3End 3
2ChicagoPA1776654
3ChicagoPA21010
4ChicagoPA31212201632
5ChicagoPB3559955
6ChicagoPC1
7ChicagoPC31122
8ChicagoPD4
9ChicagoPD1158154166158210204
10ChicagoPD29185107101
11ChicagoPD3613611658657
12ChicagoPE344
Summary
 
Upvote 0
Another macro (has some similarity to Peter's code...)

Code:
Sub aTest()
    Dim dic As Object, sh As Variant, strKey As String
    Dim vData As Variant, i As Long, lCounter As Long, vAux As Variant
    
    Set dic = CreateObject("Scripting.Dictionary")
    dic.CompareMode = vbTextCompare
    
    lCounter = 0
    For Each sh In Array("Sheet1", "Sheet2", "Sheet3") '<--Adjust sheet names
        With Sheets(sh)
            vData = .Range("A2:E" & .Cells(.Rows.Count, "A").End(xlUp).Row)
            For i = LBound(vData, 1) To UBound(vData, 1)
                strKey = vData(i, 1) & "|" & vData(i, 2) & "|" & vData(i, 3)
                If dic.exists(strKey) Then
                    vAux = dic(strKey)
                    vAux(lCounter) = vData(i, 4)
                    vAux(lCounter + 1) = vData(i, 5)
                    dic(strKey) = vAux
                Else
                    Select Case lCounter
                        Case 0
                        dic(strKey) = Array(vData(i, 4), vData(i, 5), "", "", "", "")
                        Case 2
                        dic(strKey) = Array("", "", vData(i, 4), vData(i, 5), "", "")
                        Case 4
                        dic(strKey) = Array("", "", "", "", vData(i, 4), vData(i, 5))
                    End Select
                End If
            Next i
        End With
        lCounter = lCounter + 2
    Next sh
    
    With Sheets("Master") '<--adjust sheet name
        .Columns("A:I").ClearContents
        .Range("A1:I1").Value = Array("State", "DOC", "Type", "Begin 1", "End 1", "Begin 2", "End 2", "Begin 3", "End 3")
        .Range("A2").Resize(dic.Count).Value = Application.Transpose(dic.keys)
        For i = 2 To dic.Count + 1
            .Range("D" & i).Resize(, 6) = dic(.Range("A" & i).Value)
        Next i
        .Range("A2").Resize(dic.Count).TextToColumns Destination:=Range("A2"), DataType:=xlDelimited, _
                OtherChar:="|", FieldInfo:=Array(Array(1, 1), Array(2, 1), Array(3, 1))
    End With
End Sub

M.
 
Upvote 0
Thanks Pete.

You assumptions are accurate. So the code is for Macro? Where will I copy it in my excel file?
 
Upvote 0
Thank you. Could you please advice on where I would need to add this macro in my worksheet? Thanks!
 
Upvote 0
So the code is for Macro? Where will I copy it in my excel file?
1. With your workbook active press Alt+F11 to bring up the vba window.
2. In the Visual Basic window use the menu to Insert|Module
3. Copy and Paste the code into the main right hand pane that opens at step 2.
4. Close the Visual Basic window.
5. Press Alt+F8 to bring up the Macro dialog, select the macro & click ‘Run’ **
6. Your workbook will need to be saved as a macro-enabled workbook (*.xlsm)

** There are other ways to instigate the macro but this will do to see if it works for you.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,119
Members
451,399
Latest member
alchavar

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