VBA - Paste to Next Empty Column from Column Q onwards

sk2018

New Member
Joined
Jul 7, 2018
Messages
26
Office Version
  1. 2016
Platform
  1. Windows
I have a Master File and Source File.
Master File: Multiple worksheets, Column A to Q
Source File: Multiple Worksheets, Column A to Q

I am a VBA newbie. But after referring forums and etc, I managed to
1) prompt window to select Source File
2) with the help of users in MrExcel, I managed to have the looping of worksheets done.

Now,
I need to copy from Column Q of Source File from each worksheet that has the same name with worksheet in Master File by matching value in Column A

And Paste it to next available columns in Master File, starting from Column R. If R is not empty, then paste to S and it goes on.

Code:
Sub CommandButton2_Click()


 Dim fileDialog As fileDialog
   Dim strPathFile As String
   Dim dialogTitle As String
   Dim wbSource As Workbook, Mwb As Workbook
   Dim Ws As Worksheet, Mws As Worksheet
   Dim Cl As Range
   Dim FR As Long
   Dim emptyColumn As Long


   Set Mwb = ThisWorkbook
   dialogTitle = "Navigate to and select required file."
   Set fileDialog = Application.fileDialog(msoFileDialogFilePicker)
   With fileDialog
      .InitialFileName = "C:\Users\User\Documents"
      .AllowMultiSelect = False
      .Filters.Clear
      .Title = dialogTitle
      If .Show = False Then
         MsgBox "File not selected to import. Process Terminated"
         Exit Sub
      End If
      strPathFile = .SelectedItems(1)
   End With
   Application.ScreenUpdating = False
   Set wbSource = Workbooks.Open(Filename:=strPathFile)
   
   For Each Ws In wbSource.Worksheets
      If ShtExists(Ws.Name, Mwb) Then
         Set Mws = Mwb.Sheets(Ws.Name)
         
        emptyColumn = Mws.Cells(3, Mws.Columns.Count).End(xlToLeft).Column
        If emptyColumn > 1 Then
        emptyColumn = emptyColumn + 1
        End If
        
         For Each Cl In Ws.Range("A2", Ws.Range("A" & Rows.Count).End(xlUp))
            FR = 0
            On Error Resume Next
            FR = Application.Match(Cl.Value, Ws.Columns(1), 0)
            On Error GoTo 0
            If FR <> 0 Then Mws.Range("Q" & FR).Value = Cl.Offset(, emptyColumn)
  
         Next Cl
      End If
      Set Mws = Nothing
   Next Ws


   wbSource.Close SaveChanges:=False
End Sub
Public Function ShtExist(ShtName As String, Optional Wbk As Workbook) As Boolean
    If Wbk Is Nothing Then Set Wbk = ActiveWorkbook
    On Error Resume Next
    ShtExist = (LCase(Wbk.Sheets(ShtName).Name) = LCase(ShtName))
    On Error GoTo 0
End Function
 
Try
Code:
   For Each Ws In wbSource.Worksheets
      If ShtExists(Ws.name, Mwb) Then
         Set Mws = Mwb.Sheets(Ws.name)
         If Mws.ProtectContents Then Mws.Unprotect Password:="[COLOR=#ff0000]password[/COLOR]", UserInterfaceOnly:=True
                  
         emptyColumn = Mws.Cells(3, Mws.Columns.Count).End(xlToLeft).Column
         If emptyColumn > 1 Then
         emptyColumn = emptyColumn + 1
         End If
        
         For Each Cl In Ws.Range("A2", Ws.Range("A" & Rows.Count).End(xlUp))
            FR = 0
            On Error Resume Next
            FR = Application.Match(Cl.Value, Mws.Columns(1), 0)
            On Error GoTo 0
            If FR <> 0 Then [COLOR=#0000ff]Cl.Offset(, 16).Copy Mws.Cells(FR, emptyColumn)[/COLOR]
         Next Cl
      End If
      Set Mws = Nothing
   Next Ws
Change the part in red to the actual password.
The part in blue will copy the contents & format
 
Upvote 0

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Try
Code:
   For Each Ws In wbSource.Worksheets
      If ShtExists(Ws.name, Mwb) Then
         Set Mws = Mwb.Sheets(Ws.name)
         If Mws.ProtectContents Then Mws.Unprotect Password:="[COLOR=#ff0000]password[/COLOR]", UserInterfaceOnly:=True
                  
         emptyColumn = Mws.Cells(3, Mws.Columns.Count).End(xlToLeft).Column
         If emptyColumn > 1 Then
         emptyColumn = emptyColumn + 1
         End If
        
         For Each Cl In Ws.Range("A2", Ws.Range("A" & Rows.Count).End(xlUp))
            FR = 0
            On Error Resume Next
            FR = Application.Match(Cl.Value, Mws.Columns(1), 0)
            On Error GoTo 0
            If FR <> 0 Then [COLOR=#0000ff]Cl.Offset(, 16).Copy Mws.Cells(FR, emptyColumn)[/COLOR]
         Next Cl
      End If
      Set Mws = Nothing
   Next Ws
Change the part in red to the actual password.
The part in blue will copy the contents & format


Blue part ie copy contents & format: working absolutely good!! Thanks
But as for the red part, prompted error "Named argument not found" and highlighted at "UserInterfaceOnly:="
 
Upvote 0
Oops it should be
Code:
If mws.ProtectContents Then mws.[COLOR=#0000ff]Protect [/COLOR]Password:="Password", UserInterfaceOnly:=True
 
Upvote 0
Oops it should be
Code:
If mws.ProtectContents Then mws.[COLOR=#0000ff]Protect [/COLOR]Password:="Password", UserInterfaceOnly:=True


It should be Unprotect. It works if I remove "UserInterfaceOnly:=True"


But anyway looking into my workbook structure, I think it doesn't help by unprotecting the worksheets.
As those protected worksheets consist of merged cells.

After thinking about it, is that possible to code something like
if worksheet name is "Version Control", "Overall Input" and some other specific names, then do not execute the copy-paste in these worksheets.
Only execute copy-paste in other worksheets.

I'm so sorry to trouble you on this.
Been trying to amend here and there by referring to forum, but it just failed to work as I wanted to.

Code:
Sub CommandButton2_Click()


 Dim fileDialog As fileDialog
   Dim strPathFile As String
   Dim dialogTitle As String
   Dim wbSource As Workbook, Mwb As Workbook
   Dim Ws As Worksheet, Mws As Worksheet
   Dim Cl As Range
   Dim FR As Long
   Dim emptyColumn As Long
   Dim NewemptyColumn As Long
   


   Set Mwb = ThisWorkbook
   dialogTitle = "Navigate to and select required file."
   Set fileDialog = Application.fileDialog(msoFileDialogFilePicker)
   With fileDialog
      .InitialFileName = "C:\Users\User\Documents"
      .AllowMultiSelect = False
      .Filters.Clear
      .Title = dialogTitle
      If .Show = False Then
         MsgBox "File not selected to import. Process Terminated"
         Exit Sub
      End If
      strPathFile = .SelectedItems(1)
   End With
   Application.ScreenUpdating = False
   Set wbSource = Workbooks.Open(Filename:=strPathFile)
   
   For Each Ws In wbSource.Worksheets
      If ShtExists(Ws.Name, Mwb) Then
         Set Mws = Mwb.Sheets(Ws.Name)
        
        emptyColumn = Mws.Cells(4, Mws.Columns.Count).End(xlToLeft).Column
        If emptyColumn > 1 Then
        NewemptyColumn = emptyColumn + 1
      End If
         For Each Cl In Ws.Range("A2", Ws.Range("A" & Rows.Count).End(xlUp))
            FR = 0
            On Error Resume Next
            FR = Application.Match(Cl.Value, Mws.Columns(1), 0)
            On Error GoTo 0
            If FR <> 0 Then Cl.Offset(, 15).Copy Mws.Cells(FR, NewemptyColumn)
            
            Next Cl
        End If
      Set Mws = Nothing
    Next Ws


   wbSource.Close SaveChanges:=False
End Sub
Public Function ShtExist(ShtName As String, Optional Wbk As Workbook) As Boolean
    If Wbk Is Nothing Then Set Wbk = ActiveWorkbook
    On Error Resume Next
    ShtExist = (LCase(Wbk.Sheets(ShtName).Name) = LCase(ShtName))
    On Error GoTo 0
End Function
 
Upvote 0
With the sheets you want to exclude, will there be other similar sheets that should be included.
For instance, you want to exclude "Version Control", could you also have a sheet that includes "Version Control" like "Version Control 1"?
 
Upvote 0
With the sheets you want to exclude, will there be other similar sheets that should be included.
For instance, you want to exclude "Version Control", could you also have a sheet that includes "Version Control" like "Version Control 1"?


As for the sheets I want to exclude, all of the sheets have specific names.
Below is the list of all sheets I want to exclude.
Version Control
Legend & Instructions
Overall Input
Overall Score
Functional Summary
Technical Summary
Imp Services Summary
Ppt
 
Upvote 0
Try
Code:
Sub CommandButton2_Click()


   Dim fileDialog As fileDialog
   Dim strPathFile As String
   Dim dialogTitle As String
   Dim wbSource As Workbook, Mwb As Workbook
   Dim Ws As Worksheet, Mws As Worksheet
   Dim Cl As Range
   Dim FR As Long
   Dim emptyColumn As Long
   Dim NewemptyColumn As Long
   Dim ary As Variant

   ary = ary("Version Control", "Legend & Instructions", "Overall Input", "Overall Score", "Functional Summary", "Technical Summary", "Imp Services Summary", "ppt")
   Set Mwb = ThisWorkbook
   dialogTitle = "Navigate to and select required file."
   Set fileDialog = Application.fileDialog(msoFileDialogFilePicker)
   With fileDialog
      .InitialFileName = "C:\Users\User\Documents"
      .AllowMultiSelect = False
      .Filters.Clear
      .Title = dialogTitle
      If .Show = False Then
         MsgBox "File not selected to import. Process Terminated"
         Exit Sub
      End If
      strPathFile = .SelectedItems(1)
   End With
   Application.ScreenUpdating = False
   Set wbSource = Workbooks.Open(FileName:=strPathFile)
   
   For Each Ws In wbSource.Worksheets
      If ShtExists(Ws.name, Mwb) And Not UBound(Filter(ary, Ws.name, True, vbTextCompare)) >= 0 Then
         Set Mws = Mwb.Sheets(Ws.name)
        
        emptyColumn = Mws.Cells(4, Mws.Columns.Count).End(xlToLeft).Column
        If emptyColumn > 1 Then
        NewemptyColumn = emptyColumn + 1
      End If
         For Each Cl In Ws.Range("A2", Ws.Range("A" & Rows.Count).End(xlUp))
            FR = 0
            On Error Resume Next
            FR = Application.Match(Cl.Value, Mws.Columns(1), 0)
            On Error GoTo 0
            If FR <> 0 Then Cl.Offset(, 15).Copy Mws.Cells(FR, NewemptyColumn)
            
            Next Cl
        End If
      Set Mws = Nothing
    Next Ws


   wbSource.Close SaveChanges:=False
End Sub
 
Upvote 0
Try
Code:
Sub CommandButton2_Click()


   Dim fileDialog As fileDialog
   Dim strPathFile As String
   Dim dialogTitle As String
   Dim wbSource As Workbook, Mwb As Workbook
   Dim Ws As Worksheet, Mws As Worksheet
   Dim Cl As Range
   Dim FR As Long
   Dim emptyColumn As Long
   Dim NewemptyColumn As Long
   Dim ary As Variant

   ary = ary("Version Control", "Legend & Instructions", "Overall Input", "Overall Score", "Functional Summary", "Technical Summary", "Imp Services Summary", "ppt")
   Set Mwb = ThisWorkbook
   dialogTitle = "Navigate to and select required file."
   Set fileDialog = Application.fileDialog(msoFileDialogFilePicker)
   With fileDialog
      .InitialFileName = "C:\Users\User\Documents"
      .AllowMultiSelect = False
      .Filters.Clear
      .Title = dialogTitle
      If .Show = False Then
         MsgBox "File not selected to import. Process Terminated"
         Exit Sub
      End If
      strPathFile = .SelectedItems(1)
   End With
   Application.ScreenUpdating = False
   Set wbSource = Workbooks.Open(FileName:=strPathFile)
   
   For Each Ws In wbSource.Worksheets
      If ShtExists(Ws.name, Mwb) And Not UBound(Filter(ary, Ws.name, True, vbTextCompare)) >= 0 Then
         Set Mws = Mwb.Sheets(Ws.name)
        
        emptyColumn = Mws.Cells(4, Mws.Columns.Count).End(xlToLeft).Column
        If emptyColumn > 1 Then
        NewemptyColumn = emptyColumn + 1
      End If
         For Each Cl In Ws.Range("A2", Ws.Range("A" & Rows.Count).End(xlUp))
            FR = 0
            On Error Resume Next
            FR = Application.Match(Cl.Value, Mws.Columns(1), 0)
            On Error GoTo 0
            If FR <> 0 Then Cl.Offset(, 15).Copy Mws.Cells(FR, NewemptyColumn)
            
            Next Cl
        End If
      Set Mws = Nothing
    Next Ws


   wbSource.Close SaveChanges:=False
End Sub


Sorry for the late. Was busy with other stuff.

Prompted type mismatch error at this line "ary = ary("Version Control", "Legend & Instructions", "Overall Input", "Overall Score", "Functional Summary", "Technical Summary", "Imp Services Summary", "ppt")"
 
Upvote 0
Oops should be
Code:
ary = [COLOR=#0000ff]array[/COLOR]("Version Control", "Legend & Instructions", "Overall Input", "Overall Score", "Functional Summary", "Technical Summary", "Imp Services Summary", "ppt")
 
Upvote 0

Forum statistics

Threads
1,214,918
Messages
6,122,249
Members
449,075
Latest member
staticfluids

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