Increment columns (by two) in Do While Loop

smd71092

New Member
Joined
Jul 1, 2015
Messages
16
Hello all! I posted a thread recently about consolidation and have gotten it close to where I want it, but am still having trouble with regard to one aspect. I am trying to fix up the part about the NextEmptyColumn. I would like for every new "FilesInPath" that the copied cells be added two columns to the right. So if the first file gets copied and pasted into columns B&C, I want the next file to get copied into D&E, and the next to be in F&G, etc. I cannot think of a way to increment the paste column by two each time, can any of you think of a way to do this?

Please only focus on the bolded part. The rest of the macro is working fine. I would appreciate any help on this. Thank you very much!

Code:
Option Explicit

Sub CombineFiles()

Dim FolderPath      As String
Dim FileName        As String
Dim Wkb             As Workbook
Dim WS              As Worksheet
Dim FolderPicker    As Object
Dim FilesInPath     As String
Dim LastRow         As Long
Dim NextEmptyColumn As Long

    Application.EnableEvents = False
    Application.ScreenUpdating = False
    Dim intChoice As Integer
    Set FolderPicker = Application.FileDialog(msoFileDialogFolderPicker)
    FolderPicker.AllowMultiSelect = False
    'make the file dialog visible to the user
    intChoice = FolderPicker.Show
    'determine what choice the user made
    If intChoice <> 0 Then
    'get the folder path selected by the user
    FolderPath = Application.FileDialog( _
        msoFileDialogOpen).SelectedItems(1)
    Else: End
    End If
    
    ' Add a slash at the end of the path if needed.
    If Right(FolderPath, 1) <> "\" Then
        FolderPath = FolderPath & "\"
    End If

    ' If there are no Excel files in the folder, exit.
    FilesInPath = Dir(FolderPath & "*.xls*")
    If FilesInPath = "" Then
        MsgBox "No files found"
        Exit Sub
    End If
    
[B]    Do While FilesInPath <> ""
        Set Wkb = Workbooks.Open(FolderPath & FilesInPath)
        For Each WS In Wkb.Worksheets
        WS.Cells.UnMerge
        NextEmptyColumn = Cells(1, Columns.Count).End(xlToLeft).Column
        If WS.Name <> "Features" Then
        LastRow = WS.Cells(WS.Rows.Count, "A").End(xlUp).Row
            ActiveWorkbook.Worksheets(WS.Name).Range("B1:C" & LastRow).Copy
            ThisWorkbook.Worksheets(WS.Name).Cells(1, NextEmptyColumn).PasteSpecial (xlPasteAll)
        Else
        LastRow = WS.Cells(WS.Rows.Count, "A").End(xlUp).Row
            ActiveWorkbook.Worksheets(WS.Name).Range("C1:D" & LastRow).Copy
            ThisWorkbook.Worksheets(WS.Name).Cells(1, NextEmptyColumn).PasteSpecial (xlPasteAll)
        End If
        Next WS
        Wkb.Close False
        FilesInPath = Dir()
    Loop
    Application.EnableEvents = True
    Application.ScreenUpdating = True[/B]

End Sub
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Note that this calculation:
Code:
NextEmptyColumn = Cells(1, Columns.Count).End(xlToLeft).Column
does NOT find the next empty column. Rather, it finds the last populated column.

If you wanted to find the next empty column after the last populated column, you would need to use something like this:
Code:
NextEmptyColumn = Cells(1, Columns.Count).End(xlToLeft).Offset(0,1).Column
or this:
Code:
NextEmptyColumn = Cells(1, Columns.Count).End(xlToLeft).Column + 1
 
Upvote 0
Note that this calculation:
Code:
NextEmptyColumn = Cells(1, Columns.Count).End(xlToLeft).Column
does NOT find the next empty column. Rather, it finds the last populated column.

If you wanted to find the next empty column after the last populated column, you would need to use something like this:
Code:
NextEmptyColumn = Cells(1, Columns.Count).End(xlToLeft).Offset(0,1).Column
or this:
Code:
NextEmptyColumn = Cells(1, Columns.Count).End(xlToLeft).Column + 1

Thanks for that! Even after fixing that up it still appears that it doesn't quite get the columns or pastes done correctly. Does that function you just provided only look for the last populated column by searching for an empty cell in Row 1 of the column? I see the Cells(1, Column.Count) and I believe the 1 refers to Row. Is there any way to check for the next column that is completely empty, not just based off of one row?
 
Upvote 0
Maybe this?
Code:
NextEmptyColumn = Range("A1").SpecialCells(xlLastCell).Offset(0, 1).Column
SpecialCells(xlLastCell) goes to the intersection of the last used row/column.
 
Upvote 0
Maybe this?
Code:
NextEmptyColumn = Range("A1").SpecialCells(xlLastCell).Offset(0, 1).Column
SpecialCells(xlLastCell) goes to the intersection of the last used row/column.

Hmm that did seem to work at first but then it skips a whole bunch of columns. But I think I have figured it out! I know I have some some things cautioning the use of "UsedRange" but it seems to be the only thing that works for me.



Code:
[COLOR=#000000][FONT=Arial]    [/FONT][/COLOR][COLOR=#000000][FONT=Arial]Do While FilesInPath <> ""[/FONT][/COLOR]

[COLOR=#000000][FONT=Arial]    [/FONT][/COLOR][COLOR=#000000][FONT=Arial]    [/FONT][/COLOR][COLOR=#000000][FONT=Arial]Set Wkb = Workbooks.Open(FolderPath & FilesInPath)[/FONT][/COLOR]
[COLOR=#000000][FONT=Arial]    [/FONT][/COLOR][COLOR=#000000][FONT=Arial]    [/FONT][/COLOR][COLOR=#000000][FONT=Arial]For Each WS In Wkb.Worksheets[/FONT][/COLOR]
[COLOR=#000000][FONT=Arial]    [/FONT][/COLOR][COLOR=#000000][FONT=Arial]    [/FONT][/COLOR][COLOR=#000000][FONT=Arial]If WS.Name <> "Features" Then[/FONT][/COLOR]
[COLOR=#000000][FONT=Arial]    [/FONT][/COLOR][COLOR=#000000][FONT=Arial]    [/FONT][/COLOR][COLOR=#000000][FONT=Arial]LastRow = WS.Cells(WS.Rows.Count, "A").End(xlUp).Row[/FONT][/COLOR]
[COLOR=#000000][FONT=Arial]        [/FONT][/COLOR][COLOR=#000000][FONT=Arial]    [/FONT][/COLOR][COLOR=#000000][FONT=Arial]ActiveWorkbook.Worksheets(WS.Name).Range("B1:C" & LastRow).Copy[/FONT][/COLOR]
[COLOR=#000000][FONT=Arial]    [/FONT][/COLOR][COLOR=#000000][FONT=Arial]    [/FONT][/COLOR][COLOR=#000000][FONT=Arial]With ThisWorkbook.Worksheets(WS.Name).UsedRange[/FONT][/COLOR]
[COLOR=#000000][FONT=Arial]    [/FONT][/COLOR][COLOR=#000000][FONT=Arial]    [/FONT][/COLOR][COLOR=#000000][FONT=Arial]LastColumn = .Columns(.Columns.Count).Column + 1[/FONT][/COLOR]
[COLOR=#000000][FONT=Arial]    [/FONT][/COLOR][COLOR=#000000][FONT=Arial]    [/FONT][/COLOR][COLOR=#000000][FONT=Arial]End With[/FONT][/COLOR]
[COLOR=#000000][FONT=Arial]    [/FONT][/COLOR][COLOR=#000000][FONT=Arial]    [/FONT][/COLOR][COLOR=#000000][FONT=Arial]ThisWorkbook.Worksheets(WS.Name).Cells(1, LastColumn).PasteSpecial (xlPasteAll)[/FONT][/COLOR]
[COLOR=#000000][FONT=Arial]    [/FONT][/COLOR][COLOR=#000000][FONT=Arial]    [/FONT][/COLOR][COLOR=#000000][FONT=Arial]Else[/FONT][/COLOR]
[COLOR=#000000][FONT=Arial]    [/FONT][/COLOR][COLOR=#000000][FONT=Arial]    [/FONT][/COLOR][COLOR=#000000][FONT=Arial]WS.Cells.UnMerge[/FONT][/COLOR]
[COLOR=#000000][FONT=Arial]    [/FONT][/COLOR][COLOR=#000000][FONT=Arial]    [/FONT][/COLOR][COLOR=#000000][FONT=Arial]LastRow = WS.Cells(WS.Rows.Count, "A").End(xlUp).Row[/FONT][/COLOR]
[COLOR=#000000][FONT=Arial]        [/FONT][/COLOR][COLOR=#000000][FONT=Arial]    [/FONT][/COLOR][COLOR=#000000][FONT=Arial]ActiveWorkbook.Worksheets(WS.Name).Range("C1:D" & LastRow).Copy[/FONT][/COLOR]
[COLOR=#000000][FONT=Arial]    [/FONT][/COLOR][COLOR=#000000][FONT=Arial]    [/FONT][/COLOR][COLOR=#000000][FONT=Arial]With ThisWorkbook.Worksheets(WS.Name).UsedRange[/FONT][/COLOR]
[COLOR=#000000][FONT=Arial]    [/FONT][/COLOR][COLOR=#000000][FONT=Arial]    [/FONT][/COLOR][COLOR=#000000][FONT=Arial]LastColumn = .Columns(.Columns.Count).Column + 1[/FONT][/COLOR]
[COLOR=#000000][FONT=Arial]    [/FONT][/COLOR][COLOR=#000000][FONT=Arial]    [/FONT][/COLOR][COLOR=#000000][FONT=Arial]End With[/FONT][/COLOR]
[COLOR=#000000][FONT=Arial]    [/FONT][/COLOR][COLOR=#000000][FONT=Arial]    [/FONT][/COLOR][COLOR=#000000][FONT=Arial]ThisWorkbook.Worksheets(WS.Name).Cells(1, LastColumn).PasteSpecial (xlPasteAll)[/FONT][/COLOR]
[COLOR=#000000][FONT=Arial]    [/FONT][/COLOR][COLOR=#000000][FONT=Arial]    [/FONT][/COLOR][COLOR=#000000][FONT=Arial]End If[/FONT][/COLOR]
[COLOR=#000000][FONT=Arial]    [/FONT][/COLOR][COLOR=#000000][FONT=Arial]    [/FONT][/COLOR][COLOR=#000000][FONT=Arial]Next WS[/FONT][/COLOR]
[COLOR=#000000][FONT=Arial]    [/FONT][/COLOR][COLOR=#000000][FONT=Arial]    [/FONT][/COLOR][COLOR=#000000][FONT=Arial]Wkb.Close False[/FONT][/COLOR]
[COLOR=#000000][FONT=Arial]    [/FONT][/COLOR][COLOR=#000000][FONT=Arial]    [/FONT][/COLOR][COLOR=#000000][FONT=Arial]FilesInPath = Dir()[/FONT][/COLOR]
[COLOR=#000000][FONT=Arial]    [/FONT][/COLOR][COLOR=#000000][FONT=Arial]Loop[/FONT][/COLOR]

[COLOR=#000000][FONT=Arial]    [/FONT][/COLOR][COLOR=#000000][FONT=Arial]Application.EnableEvents = True[/FONT][/COLOR]
[COLOR=#000000][FONT=Arial]    [/FONT][/COLOR][COLOR=#000000][FONT=Arial]Application.ScreenUpdating = True[/FONT][/COLOR]
 
Upvote 0

Forum statistics

Threads
1,223,115
Messages
6,170,192
Members
452,309
Latest member
liclice

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