Opening workbook based on looping value in range

Ato09

New Member
Joined
Feb 6, 2019
Messages
1
Hi.

I'm trying to make a macro on Excel 2016 that will open a certain workbook based on data in a determined range. Once the workbook has been open, the data inside the newly opened workbook will be copy and paste into the last blank row of one Main Excel workbook. This will carry on until the macro find empty cell in the determined range.

I have tried using the loop but I can't seem to be able to make it to read from the range.

Code:
Sub copyData()
'
' copyData Macro
'
    
    Dim excelName As Variant
    excelName = Range("filesList")
        
    Dim categoryPath As Variant
    categoryPath = Range("category")
    
'   Turn off notification
    Application.DisplayAlerts = False
    Application.ScreenUpdating = False
        
        'Code below is to be loop until excelName/filesList returns blank
        Workbooks.Open fileName:="C:\Users\User Name\Desktop\Folder 1\Folder 2\" & categoryPath & "\" & excelName
                
        Windows(excelName).Activate
    
        'enable edit if file is protected
        If Application.ProtectedViewWindows.Count > 0 Then
        Application.ActiveProtectedViewWindow.Edit
        End If
    
        'select data from row 2 to last row
        Rows("2:2").Select
        Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
        Selection.copy
    
        Windows(categoryPath & "_compiled.xlsx").Activate
        Worksheets("Worksheet").Select
        Worksheets("Worksheet").Range("A1").Select
    
        'select last blank cell then paste new data
        If Worksheets("Worksheet").Range("A1").Offset(1, 0) <> "" Then
        Worksheets("Worksheet").Range("A1").End(xlDown).Select
        End If
        ActiveCell.Offset(1, 0).Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
        ActiveCell.Offset(1, 0).Select
       
        'close excel file
        Windows(excelName).Activate
        ActiveWindow.Close
        
End Sub
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.

Forum statistics

Threads
1,216,174
Messages
6,129,297
Members
449,499
Latest member
HockeyBoi

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