Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.22 KB | None | 0 0
  1. Option Explicit
  2.  
  3. Sub Import_ClientRating()
  4.  
  5. '1. First Import data 1-to-1 to Table template
  6. '2. Used Find function to find the matching header from Client Class in New Data
  7. '3. Copy and Paste the required column from New Data in Clien Class
  8.  
  9. Dim FiletoOpen As Variant
  10. Dim FileCnt As Byte
  11. Dim SelectedBook As Workbook
  12. Dim lastrow As Long, LastTemp As Long 'lasttemp is "last row for table template
  13. Const StartRowTemp As Byte = 1
  14. Dim c As Byte 'number of columns
  15. Dim GetHeader As Range 'find
  16.  
  17. Call Entry_Point
  18. On Error GoTo Handle
  19.  
  20. 'pick files to import - allow multiselect
  21. FiletoOpen = Application.GetOpenFilename _
  22. (FileFilter:="Excel Files (*.xlsx), *.xlsx", Title:="Select Workbook to Import", MultiSelect:=True)
  23.  
  24. If IsArray(FiletoOpen) Then
  25. For FileCnt = 1 To UBound(FiletoOpen)
  26. Set SelectedBook = Workbooks.Open(Filename:=FiletoOpen(FileCnt))
  27. ShDataN.Cells.Clear
  28. SelectedBook.Worksheets("Client").Cells.Copy
  29. ShDataN.Range("A1").PasteSpecial xlPasteValuesAndNumberFormats
  30. SelectedBook.Close
  31.  
  32. 'locate last empty row in Client Class Table
  33. lastrow = ShTrial.Cells(Rows.Count, 1).End(xlUp).Row + 1
  34.  
  35. 'locate last row in the new data
  36. LastTemp = ShDataN.Cells(Rows.Count, 1).End(xlUp).Row
  37.  
  38. 'use find function to match headers, since the template could have a different order of columns than the Client Class Table
  39.  
  40. c = 1
  41. Do While ShTrial.Cells(1, c) <> ""
  42.  
  43. Set GetHeader = ShDataN.Rows(StartRowTemp).Find _
  44. (What:=ShTrial.Cells(1, c).Value, LookIn:=xlValues, MatchCase:=False, lookat:=xlWhole)
  45. If Not GetHeader Is Nothing Then
  46. ShDataN.Range(ShDataN.Cells(StartRowTemp + 1, GetHeader.Column), ShDataN.Cells(LastTemp, GetHeader.Column)).Copy
  47. ShTrial.Cells(lastrow, c).PasteSpecial
  48.  
  49. End If 'get Header
  50. c = c + 1
  51. Loop
  52.  
  53. Next FileCnt
  54. MsgBox "Data imported sucessfully", vbInformation, "General Information"
  55.  
  56. End If 'isArray
  57. ShTrial.Select
  58. Range("A1").Select
  59.  
  60. Call Exit_Point
  61.  
  62. Exit Sub
  63.  
  64. Handle:
  65. If Err.Number = 9 Then
  66. Else
  67. MsgBox "An error has occured"
  68. End If
  69.  
  70. Call Exit_Point
  71.  
  72. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement