Reputation: 667
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
Upvotes: 0
Views: 550
Reputation: 359
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.
Upvotes: 0
Reputation: 42518
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
Upvotes: 1