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
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
try
Code:
FR = Application.Match(Cl.Value, Mws.Columns(1), 0)
On Error GoTo 0
If FR <> 0 Then Mws.Cells(FR, emptycolumn).Value = Cl.Offset(, 16).Value
 
Upvote 0
try
Code:
FR = Application.Match(Cl.Value, Mws.Columns(1), 0)
On Error GoTo 0
If FR <> 0 Then Mws.Cells(FR, emptycolumn).Value = Cl.Offset(, 16).Value


Prompted Run-time error '1004': Application-defined or object-defined error.

With " Mws.Cells(FR, emptyColumn).Value = Cl.Offset(, 16).Value" highlighted.
 
Upvote 0
Are the sheets protected?
If not what are the values of FR & emptycolumn when you get the error
 
Upvote 0
emptyColumn should contain a number not a letter. What is the number?
Also do you have any merged cells in either workbook?
 
Upvote 0
emptyColumn should contain a number not a letter. What is the number?
Also do you have any merged cells in either workbook?



EmptyColumn is 16
Merged cells yes. There are a few serving mainly as header.
G1:K4
L1:P4
M6:06
 
Upvote 0
As long as you don't have merged cells in row 7, that shouldn't be a problem.
So I'm at a loss why it's not working for you.
 
Upvote 0
As long as you don't have merged cells in row 7, that shouldn't be a problem.
So I'm at a loss why it's not working for you.


I have removed all the merged cells, and made sure that there is no protected sheets.

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, Mws.Columns(1), 0)
            On Error GoTo 0
            If FR <> 0 Then Mws.Cells(FR, emptyColumn).Value = Cl.Offset(, 16).Value
            
            
  
         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
I have removed all the merged cells, and made sure that there is no protected sheets.

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, Mws.Columns(1), 0)
            On Error GoTo 0
            If FR <> 0 Then Mws.Cells(FR, emptyColumn).Value = Cl.Offset(, 16).Value
            
            
  
         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


I found a few worksheets sharing same name between Master and Source workbooks, and they are protected.
SO Sorry. Just noticed it.

(1) Is there a way to code on skipping those protected worksheets in this copy paste?
(2) I realised this copy paste does not copy the format (ie colour, border). Is there a way to do it?


Thanks
 
Upvote 0

Forum statistics

Threads
1,224,759
Messages
6,180,814
Members
452,996
Latest member
nelsonsix66

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