Advertisement
ivanpampulov

Untitled

Oct 31st, 2023
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Sub CopyAndFillData()
  2.     Dim sourceWorkbook As Workbook
  3.     Dim targetWorkbook As Workbook
  4.     Dim sourceSheet As Worksheet
  5.     Dim targetSheet As Worksheet
  6.     Dim lastRow As Long
  7.     Dim sourceDataA As Range
  8.     Dim sourceDataB As Range
  9.     Dim sourceDataD As Range
  10.     Dim sourceDataF As Range
  11.     Dim sourceDataAF As Range
  12.     Dim sourceDataL As Range
  13.     Dim sourceDataX As Range
  14.     Dim sourceDataZ As Range
  15.     Dim sourceDataAA As Range
  16.     Dim sourceDataY As Range
  17.     Dim sourceDataW As Range
  18.     Dim i As Long
  19.  
  20.      ' Prompt for the source location
  21.    sourceLocation = InputBox("Enter source location (path):")
  22.    
  23.     ' Prompt for the source file name
  24.    sourceFileName = InputBox("Enter source file name:")
  25.  
  26.     ' Open the source workbook
  27.    Set sourceWorkbook = Workbooks.Open(sourceLocation & "\" & sourceFileName & ".xlsx")
  28.     ' Set the source sheets
  29.    Set sourceSheet = sourceWorkbook.Sheets(InputBox("Input source sheet name:"))
  30.    
  31.     ' Create a new target workbook
  32.    Set targetWorkbook = Workbooks.Add
  33.    
  34.     ' Set a reference to the target worksheet in the new workbook
  35.    Set targetSheet = targetWorkbook.Worksheets(1)
  36.  
  37.  
  38.     ' Find the last row in the source sheet
  39.    lastRow = sourceSheet.Cells(sourceSheet.Rows.Count, "A").End(xlUp).Row
  40.  
  41.     ' Set the ranges for data in the source sheet
  42.    Set sourceDataA = sourceSheet.Range("A2:A" & lastRow)
  43.     Set sourceDataB = sourceSheet.Range("B2:B" & lastRow)
  44.     Set sourceDataD = sourceSheet.Range("D2:D" & lastRow)
  45.     Set sourceDataF = sourceSheet.Range("F2:F" & lastRow)
  46.     Set sourceDataAF = sourceSheet.Range("AF2:AF" & lastRow)
  47.     Set sourceDataL = sourceSheet.Range("L2:L" & lastRow)
  48.     Set sourceDataX = sourceSheet.Range("X2:X" & lastRow)
  49.     Set sourceDataZ = sourceSheet.Range("Z2:Z" & lastRow)
  50.     Set sourceDataAA = sourceSheet.Range("AA2:AA" & lastRow)
  51.     Set sourceDataY = sourceSheet.Range("Y2:Y" & lastRow)
  52.     Set sourceDataW = sourceSheet.Range("W2:W" & lastRow)
  53.    
  54.     For i = lastRow To 2 Step -1
  55.         ' Check for no email address
  56.        If IsEmpty(sourceSheet.Cells(i, 6).Value) Then
  57.             sourceSheet.Rows(i).Delete
  58.  
  59.         ' Check for valid email address
  60.        ElseIf InStr(1, sourceSheet.Cells(i, 6).Value, "@") = 0 Then
  61.             sourceSheet.Rows(i).Delete
  62.         ' Check for no family name
  63.        ElseIf IsEmpty(sourceSheet.Cells(i, 2).Value) Then
  64.             sourceSheet.Cells(i, 2).Value = sourceSheet.Cells(i, 1).Value
  65.         End If
  66.     Next i
  67.  
  68.     ' Copy data from source to target
  69.    For i = 2 To lastRow
  70.         targetSheet.Cells(i, 1).Value = sourceDataA.Cells(i - 1, 1).Value
  71.         targetSheet.Cells(i, 2).Value = sourceDataB.Cells(i - 1, 1).Value
  72.         targetSheet.Cells(i, 3).Value = sourceDataD.Cells(i - 1, 1).Value
  73.         targetSheet.Cells(i, 9).Value = sourceDataF.Cells(i - 1, 1).Value
  74.         targetSheet.Cells(i, 7).Value = sourceDataAF.Cells(i - 1, 1).Value
  75.         targetSheet.Cells(i, 10).Value = sourceDataL.Cells(i - 1, 1).Value
  76.         targetSheet.Cells(i, 11).Value = sourceDataX.Cells(i - 1, 1).Value
  77.         targetSheet.Cells(i, 13).Value = sourceDataZ.Cells(i - 1, 1).Value
  78.         targetSheet.Cells(i, 17).Value = sourceDataAA.Cells(i - 1, 1).Value
  79.         targetSheet.Cells(i, 15).Value = sourceDataY.Cells(i - 1, 1).Value
  80.         targetSheet.Cells(i, 18).Value = sourceDataW.Cells(i - 1, 1).Value
  81.     Next i
  82.  
  83.     ' Fill other specified columns in the target sheet
  84.    For i = 2 To lastRow
  85.         targetSheet.Cells(i, 4).Value = "Valya Krasteva"
  86.         targetSheet.Cells(i, 5).Value = "Ivan Pampulov"
  87.         targetSheet.Cells(i, 6).Value = "Fun Walls, Ropes Course, Rollglider, Adventure Trail, Caving, Cloud Climb, Zip Line, Ninja Course, Tree Course, Slides"
  88.         targetSheet.Cells(i, 8).Value = "Active Entertainment"
  89.         targetSheet.Cells(i, 14).Value = "LinkedIn"
  90.         targetSheet.Cells(i, 19).Value = "I first contacted client"
  91.         targetSheet.Cells(i, 23).Value = "Global"
  92.     Next i
  93.    
  94.     For i = 2 To lastRow
  95.         ' Check for 'United States' and update to 'USA'
  96.        If targetSheet.Cells(i, 7).Value = "United States" Then
  97.             targetSheet.Cells(i, 7).Value = "USA"
  98.         End If
  99.     Next i
  100.    
  101.     ' Prompt for the target location and file name
  102.    targetLocation = InputBox("Where should I save it? (path):")
  103.     targetFileName = InputBox("What will be the file name? (without extension):")
  104.    
  105.     ' Save the target workbook with the specified location and file name
  106.    targetWorkbook.SaveAs targetLocation & "\" & targetFileName & ".xlsx" ' Change the extension if needed
  107.    
  108.     ' Save the changes to the target workbook
  109.    targetWorkbook.Save
  110.     targetWorkbook.Close SaveChanges:=True
  111.     sourceWorkbook.Close SaveChanges:=False
  112. End Sub
  113.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement