Guest User

Untitled

a guest
Jun 24th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.71 KB | None | 0 0
  1. Sub CopyData()
  2.  
  3. Dim fileDialog As fileDialog
  4. Dim strPathFile As String
  5. Dim strFileName As String
  6. Dim strPath As String
  7. Dim dialogTitle As String
  8. Dim wbSource As Workbook
  9. Dim rngToCopy As Range
  10. Dim rngRow As Range
  11. Dim rngDestin As Range
  12. Dim lngRowsCopied As Long
  13.  
  14.  
  15. dialogTitle = "Navigate to and select required file."
  16. Set fileDialog = Application.fileDialog(msoFileDialogFilePicker)
  17. With fileDialog
  18. .InitialFileName = "C:UsersUserDocuments"
  19. '.InitialFileName = ThisWorkbook.Path & "" 'Alternative to previous line
  20. .AllowMultiSelect = False
  21. .Filters.Clear
  22. .Title = dialogTitle
  23.  
  24.  
  25.  
  26. If .Show = False Then
  27. MsgBox "File not selected to import. Process Terminated"
  28. Exit Sub
  29. End If
  30. strPathFile = .SelectedItems(1)
  31. End With
  32.  
  33. Set wbSource = Workbooks.Open(Filename:=strPathFile)
  34.  
  35. With wbSource.Worksheets("Sheet1") 'Edit "Sheet1" to required sheet name
  36. Set rngToCopy = .Range(.Cells(1, "A"), .UsedRange.SpecialCells(xlCellTypeLastCell))
  37. For Each rngRow In rngToCopy.Rows
  38. If WorksheetFunction.CountA(rngRow) = 0 Then
  39. rngRow.EntireRow.Hidden = True 'Hides rows with no data
  40. End If
  41. Next rngRow
  42.  
  43. Set rngDestin = ThisWorkbook.Sheets("Sheet1").Cells(1, "A") 'Edit "Sheet1" to destination sheet name
  44.  
  45. rngToCopy.SpecialCells(xlCellTypeVisible).Copy Destination:=rngDestin
  46.  
  47. lngRowsCopied = rngToCopy.Columns(1).SpecialCells(xlCellTypeVisible).Count
  48. MsgBox lngRowsCopied & " rows copied."
  49.  
  50.  
  51. .Rows.Hidden = False 'Unhides previously hidden rows
  52.  
  53. End With
  54.  
  55. wbSource.Close SaveChanges:=False
  56.  
  57. Set fileDialog = Nothing
  58. Set rngRow = Nothing
  59. Set rngToCopy = Nothing
  60. Set wbSource = Nothing
  61. Set rngDestin = Nothing
  62.  
  63. 'MsgBox "The data is copied"
  64.  
  65. End Sub
Add Comment
Please, Sign In to add comment