Add columns and delete columns based on the another workbook in VBA

mayaa_mmm

Board Regular
Joined
Jul 30, 2014
Messages
54
Office Version
  1. 2010
Platform
  1. Windows
I have workbook A with sheet1 contains like below header

IDClassMarkGrade
XXAABBCC
XXAABBCC
XXAABBCC
XXAABBCC

In the another workbook B contains like below but



IDClassMarkAttended
XXAABBDD
XXAABBAB
XXAABBAC
XXAABBCA

In workbook A should contains ID, Class , Mark
Grade column deleted and Attended got added.

Basically column should get added and delete as per workbook B

IDClassMarkAttended
XXAABBDD
XXAABBAB
XXAABBAC
XXAABBCA
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
With Power Query, Merge the two tables.

Mcode in PQ follows:
VBA Code:
let
    Source = Table.NestedJoin(Sheet1, {"Column1"}, Sheet2, {"Column1"}, "Sheet2", JoinKind.LeftOuter),
    #"Expanded Sheet2" = Table.ExpandTableColumn(Source, "Sheet2", {"Column4"}, {"Column4.1"}),
    #"Removed Columns" = Table.RemoveColumns(#"Expanded Sheet2",{"Column4"}),
    #"Promoted Headers" = Table.PromoteHeaders(#"Removed Columns", [PromoteAllScalars=true]),
    #"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"ID", type text}, {"Class", type text}, {"Mark", type text}, {"Attended", type text}})
in
    #"Changed Type"
 
Upvote 0
1. Considering that file Workbook B is name as Attended and have all the records in sheet1
2. Also consider that the workbook B is not open
3. Code will ask to open workbook B and well do rest of the required task
4. Ensuring both Workbook A and B have the records in sheet 1 respectively



VBA Code:
Sub text()
Workbooks("A.xlsx").Sheets("Sheet1").Activate
With Application.FileDialog(msoFileDialogFilePicker)
        'Makes sure the user can select only one file
        .AllowMultiSelect = False
        'Filter to just the following types of files to narrow down selection options
        .Filters.Add "Excel Files", "*.xlsx; *.xlsm; *.xls; *.xlsb", 1
        'Show the dialog box
        .Show
        
        'Store in fullpath variable
        fullpath = .SelectedItems.Item(1)
    End With
    Workbooks.Open (fullpath) '' workbook B is open
    'It's a good idea to still check if the file type selected is accurate.
    'Quit the procedure if the user didn't select the type of file we need.
    If InStr(fullpath, ".xls") = 0 Then
        Exit Sub
    End If

Workbooks("A.xlsx").Sheets("Sheet1").Activate

ActiveSheet.Range("E1").Value = "Attende"
With Worksheets("Sheet1")
    With .Range("E2:E" & .Range("A" & .Rows.Count).End(xlUp).Row)
        .Formula = "=VLOOKUP(A2,[B.xlsx]Sheet1!$A:$D,4,0)" ''-- can change the formula here
        .Value = .Value
     End With
End With
ActiveSheet.Columns("D:D").Select
    Selection.Delete Shift:=xlToLeft
End Sub
 
Upvote 0
With Power Query, Merge the two tables.

Mcode in PQ follows:
VBA Code:
let
    Source = Table.NestedJoin(Sheet1, {"Column1"}, Sheet2, {"Column1"}, "Sheet2", JoinKind.LeftOuter),
    #"Expanded Sheet2" = Table.ExpandTableColumn(Source, "Sheet2", {"Column4"}, {"Column4.1"}),
    #"Removed Columns" = Table.RemoveColumns(#"Expanded Sheet2",{"Column4"}),
    #"Promoted Headers" = Table.PromoteHeaders(#"Removed Columns", [PromoteAllScalars=true]),
    #"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"ID", type text}, {"Class", type text}, {"Mark", type text}, {"Attended", type text}})
in
    #"Changed Type"


Sir
thank you for your update.
But I am new to Power Query.
Link which you provided shows database error for inserting the power query
 
Upvote 0
1. Considering that file Workbook B is name as Attended and have all the records in sheet1
2. Also consider that the workbook B is not open
3. Code will ask to open workbook B and well do rest of the required task
4. Ensuring both Workbook A and B have the records in sheet 1 respectively



VBA Code:
Sub text()
Workbooks("A.xlsx").Sheets("Sheet1").Activate
With Application.FileDialog(msoFileDialogFilePicker)
        'Makes sure the user can select only one file
        .AllowMultiSelect = False
        'Filter to just the following types of files to narrow down selection options
        .Filters.Add "Excel Files", "*.xlsx; *.xlsm; *.xls; *.xlsb", 1
        'Show the dialog box
        .Show
       
        'Store in fullpath variable
        fullpath = .SelectedItems.Item(1)
    End With
    Workbooks.Open (fullpath) '' workbook B is open
    'It's a good idea to still check if the file type selected is accurate.
    'Quit the procedure if the user didn't select the type of file we need.
    If InStr(fullpath, ".xls") = 0 Then
        Exit Sub
    End If

Workbooks("A.xlsx").Sheets("Sheet1").Activate

ActiveSheet.Range("E1").Value = "Attende"
With Worksheets("Sheet1")
    With .Range("E2:E" & .Range("A" & .Rows.Count).End(xlUp).Row)
        .Formula = "=VLOOKUP(A2,[B.xlsx]Sheet1!$A:$D,4,0)" ''-- can change the formula here
        .Value = .Value
     End With
End With
ActiveSheet.Columns("D:D").Select
    Selection.Delete Shift:=xlToLeft
End Sub

Thank you so much
It works awesome
 
Upvote 0
Link which you provided shows database error for inserting the power query
Thanks for letting me know. Look at the new link in my signature.
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

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