Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Sub CopyData()
- Dim fileDialog As fileDialog
- Dim strPathFile As String
- Dim strFileName As String
- Dim strPath As String
- Dim dialogTitle As String
- Dim wbSource As Workbook
- Dim rngToCopy As Range
- Dim rngRow As Range
- Dim rngDestin As Range
- Dim lngRowsCopied As Long
- dialogTitle = "Navigate to and select required file."
- Set fileDialog = Application.fileDialog(msoFileDialogFilePicker)
- With fileDialog
- .InitialFileName = "C:UsersUserDocuments"
- '.InitialFileName = ThisWorkbook.Path & "" 'Alternative to previous line
- .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
- Set wbSource = Workbooks.Open(Filename:=strPathFile)
- With wbSource.Worksheets("Sheet1") 'Edit "Sheet1" to required sheet name
- Set rngToCopy = .Range(.Cells(1, "A"), .UsedRange.SpecialCells(xlCellTypeLastCell))
- For Each rngRow In rngToCopy.Rows
- If WorksheetFunction.CountA(rngRow) = 0 Then
- rngRow.EntireRow.Hidden = True 'Hides rows with no data
- End If
- Next rngRow
- Set rngDestin = ThisWorkbook.Sheets("Sheet1").Cells(1, "A") 'Edit "Sheet1" to destination sheet name
- rngToCopy.SpecialCells(xlCellTypeVisible).Copy Destination:=rngDestin
- lngRowsCopied = rngToCopy.Columns(1).SpecialCells(xlCellTypeVisible).Count
- MsgBox lngRowsCopied & " rows copied."
- .Rows.Hidden = False 'Unhides previously hidden rows
- End With
- wbSource.Close SaveChanges:=False
- Set fileDialog = Nothing
- Set rngRow = Nothing
- Set rngToCopy = Nothing
- Set wbSource = Nothing
- Set rngDestin = Nothing
- 'MsgBox "The data is copied"
- End Sub
Add Comment
Please, Sign In to add comment