0

Being new to excel, I'm not able to write a macro which will update data in one of the columns from a master data reference.

For Example: I have a sheet (Tran_Sheet) which has transaction data, along with the first name and last name of customers as separate columns.

I got another Sheet (Master_Data) which has all the first names and last names of the customers.

The data in Tran_Sheet , has noise and garbage values, hence some of the names have been misspelled.

I need to write a Macro such that the names will be updated in the Tran_Sheet based on the Master_Data as refernce.

I know this is just simple using an SQL query, but I need yto do it in Excel 2013.

Thank you!!!

Regards, Ravi

2
  • A "join" is really a pain in Excel. Is it an absolute must to make this in Excel? You could possibly import your tables to MS Access and do the join there.
    – SQL Police
    Commented Mar 20, 2016 at 16:01
  • Why don't you just insert a formula into the Tran_Sheet which pulls the data from the Master_Data sheet? The combination of INDEX / MATCH should work just fine. Once you have all the data on the Tran_Sheet you can remove the formulas with the respective values only. You can also do these 3 steps using VBA and it will be very fast.
    – Ralph
    Commented Mar 20, 2016 at 16:22

2 Answers 2

1

You could easily execute a query on the current workbook:

Sub SQL_Example()

  ' query to compute the average score for each group
  Const SQL_SUM_SCORES_BY_GROUP = _
    "SELECT ref.Group, AVG(data.Score) AS Score " & _
    "FROM [Sheet1$] data INNER JOIN [Sheet2$] ref " & _
    "ON data.Name = ref.Name " & _
    "GROUP BY ref.Group "

  ' execute the query and write the results in a new sheet
  SqlExec ThisWorkbook, SQL_SUM_SCORES_BY_GROUP

End Sub

''
' Executes a query on a workbook.
' @source {Workbook}  Workbook loaded by the SQL engine
' @target {Worksheet} Optional, Worksheet to display the result
' @sql {String} SQL query
''
Sub SqlExec(source As Workbook, sql As String, Optional Target As Worksheet)
  Static rs As Object
  If rs Is Nothing Then Set rs = CreateObject("ADODB.recordset")

  ' build the connection string
  Dim conn$
  conn = "Provider=Microsoft.Jet.OLEDB.4.0" _
       & ";Data Source=" & source.FullName _
       & ";Extended Properties=""Excel 8.0;HDR=Yes"";"

  ' insert a new worksheet if the target worksheet is null
  If Target Is Nothing Then
    Set Target = Worksheets.Add(After:=Worksheets(Worksheets.count))
  End If

  ' execute the query
  rs.Open sql, conn

  ' copy the headers to the target sheet
  Target.Cells.Clear
  For i = 1 To rs.Fields.count
    Target.Cells(1, i).value = rs.Fields(i - 1).name
  Next

  ' copy the values to the target sheet
  Target.Cells(2, 1).CopyFromRecordset rs

  ' dispose
  rs.Close
End Sub
0

This is by no means a simple task and can't be solved either in a SQL Query or Excel without more details towards the type of noise in your data. It ends up rollin a dice when it comes towards correct personal data as its highly unstructured and people tend to forget about the time component.

Not the answer you're looking for? Browse other questions tagged or ask your own question.