How can i map data from one file to another based on entering a date and searching for that value in the second file

huangapple go评论95阅读模式
英文:

How can i map data from one file to another based on entering a date and searching for that value in the second file

问题

  1. Sub UpdateData()
  2. Dim wbSource As Workbook
  3. Dim wsSource As Worksheet
  4. Dim wbTarget As Workbook
  5. Dim wsTarget As Worksheet
  6. Dim sourceRow As Long
  7. Dim targetRow As Long
  8. Dim targetDate As Date
  9. Dim targetDateStr As String
  10. ' 打开源文件并设置源工作表
  11. Set wbSource = Workbooks.Open("C:\Users\E12\Desktop\FY23.xlsx") ' 用源文件的路径进行更新
  12. Set wsSource = wbSource.Sheets("Pull in") ' 用源工作表的名称进行更新
  13. ' 打开你的文件并设置目标工作表
  14. Set wbTarget = ThisWorkbook ' 如果你的文件不同,请进行更新
  15. Set wsTarget = wbTarget.Sheets("Released") ' 用目标工作表的名称进行更新
  16. ' 提示用户输入一个日期
  17. targetDateStr = Application.InputBox("输入日期 (DDMMMYY)", Type:=2)
  18. ' 将目标日期字符串转换为有效的日期格式
  19. On Error Resume Next
  20. targetDate = DateValue(Replace(targetDateStr, ".", "-"))
  21. On Error GoTo 0
  22. ' 检查目标日期是否是有效日期
  23. If IsDate(targetDate) Then
  24. ' 将目标日期格式化为 "dd-mmm-yy"
  25. targetDateStr = Format(targetDate, "dd-mmm-yy")
  26. ' 在源文件的列 "W" 中查找基于日期和 "Release to Logistics" 值的行
  27. Dim searchRange As Range
  28. Set searchRange = wsSource.Columns("W")
  29. sourceRow = 0
  30. On Error Resume Next
  31. sourceRow = Application.WorksheetFunction.Match(CDate(targetDate), searchRange, 0)
  32. On Error GoTo 0
  33. ' 如果找到匹配项,则更新文件中的数据
  34. If Not IsError(sourceRow) Then
  35. ' 将特定列从源文件映射到你的文件
  36. wsTarget.Cells(targetRow, 1).Value = wsSource.Cells(sourceRow, 2).Value
  37. wsTarget.Cells(targetRow, 2).Value = wsSource.Cells(sourceRow, 4).Value
  38. wsTarget.Cells(targetRow, 3).Value = wsSource.Cells(sourceRow, 5).Value
  39. wsTarget.Cells(targetRow, 4).Value = wsSource.Cells(sourceRow, 6).Value
  40. wsTarget.Cells(targetRow, 5).Value = wsSource.Cells(sourceRow, 7).Value
  41. wsTarget.Cells(targetRow, 6).Value = wsSource.Cells(sourceRow, 8).Value
  42. wsTarget.Cells(targetRow, 7).Value = wsSource.Cells(sourceRow, 9).Value
  43. wsTarget.Cells(targetRow, 8).Value = wsSource.Cells(sourceRow, 12).Value
  44. wsTarget.Cells(targetRow, 9).Value = wsSource.Cells(sourceRow, 13).Value
  45. wsTarget.Cells(targetRow, 10).Value = wsSource.Cells(sourceRow, 16).Value
  46. wsTarget.Cells(targetRow, 11).Value = wsSource.Cells(sourceRow, 18).Value
  47. wsTarget.Cells(targetRow, 12).Value = wsSource.Cells(sourceRow, 21).Value
  48. wsTarget.Cells(targetRow, 13).Value = wsSource.Cells(sourceRow, 23).Value
  49. wsTarget.Cells(targetRow, 14).Value = wsSource.Cells(sourceRow, 1).Value
  50. MsgBox "数据更新成功。"
  51. Else
  52. MsgBox "未找到指定日期的数据或未找到 'Release to Logistics' 值。"
  53. End If
  54. Else
  55. MsgBox "输入的日期无效。请重试。"
  56. End If
  57. ' 关闭源文件,不保存更改
  58. wbSource.Close SaveChanges:=False
  59. End Sub
英文:
  1. the user enters a date in the target file(where the macro is)
  2. If user enters a date like 12jun23, it should convert it to 12-Jun-23 or any variation
  3. search that date in W column in source file (POR file)
  4. if there's a row filled with that date, map few of the columns data to my target file.
  5. It should map the columns like A column in my file, to B column in the source file.

I'm getting a 1004 error, stating application or object error.

  1. Also, tried making some changes, the recent error im getting is:
  2. When i enter the date, for example 30may23, it should format to 30-May-23, and then search for it in the file.
  3. But its currently formatting to 30-Dec-99.
  4. Sub UpdateData()
  5. Dim wbSource As Workbook
  6. Dim wsSource As Worksheet
  7. Dim wbTarget As Workbook
  8. Dim wsTarget As Worksheet
  9. Dim sourceRow As Long
  10. Dim targetRow As Long
  11. Dim targetDate As Date
  12. Dim targetDateStr As String
  13. ' Open the source file and set the source worksheet
  14. Set wbSource = Workbooks.Open("C:\Users\E12\Desktop\FY23.xlsx") ' Update with the path to the source file
  15. Set wsSource = wbSource.Sheets("Pull in") ' Update with the name of the source sheet
  16. ' Open your file and set the target worksheet
  17. Set wbTarget = ThisWorkbook ' Update if your file is different
  18. Set wsTarget = wbTarget.Sheets("Released") ' Update with the name of the target sheet
  19. ' Prompt the user to enter a date
  20. targetDateStr = Application.InputBox("Enter the date (DDMMMYY)", Type:=2)
  21. ' Convert the target date string to a valid date format
  22. On Error Resume Next
  23. targetDate = DateValue(Replace(targetDateStr, ".", "-"))
  24. On Error GoTo 0
  25. ' Check if the target date is a valid date
  26. If IsDate(targetDate) Then
  27. ' Format the target date to "dd-mmm-yy"
  28. targetDateStr = Format(targetDate, "dd-mmm-yy")
  29. ' Find the row in the source file based on the date and "Release to Logistics" value
  30. Dim searchRange As Range
  31. Set searchRange = wsSource.Columns("W")
  32. sourceRow = 0
  33. On Error Resume Next
  34. sourceRow = Application.WorksheetFunction.Match(CDate(targetDate), searchRange, 0)
  35. On Error GoTo 0
  36. ' Update the data in your file if a match is found
  37. If Not IsError(sourceRow) Then
  38. ' Map specific columns from the source file to your file
  39. wsTarget.Cells(targetRow, 1).Value = wsSource.Cells(sourceRow, 2).Value
  40. wsTarget.Cells(targetRow, 2).Value = wsSource.Cells(sourceRow, 4).Value
  41. wsTarget.Cells(targetRow, 3).Value = wsSource.Cells(sourceRow, 5).Value
  42. wsTarget.Cells(targetRow, 4).Value = wsSource.Cells(sourceRow, 6).Value
  43. wsTarget.Cells(targetRow, 5).Value = wsSource.Cells(sourceRow, 7).Value
  44. wsTarget.Cells(targetRow, 6).Value = wsSource.Cells(sourceRow, 8).Value
  45. wsTarget.Cells(targetRow, 7).Value = wsSource.Cells(sourceRow, 9).Value
  46. wsTarget.Cells(targetRow, 8).Value = wsSource.Cells(sourceRow, 12).Value
  47. wsTarget.Cells(targetRow, 9).Value = wsSource.Cells(sourceRow, 13).Value
  48. wsTarget.Cells(targetRow, 10).Value = wsSource.Cells(sourceRow, 16).Value
  49. wsTarget.Cells(targetRow, 11).Value = wsSource.Cells(sourceRow, 18).Value
  50. wsTarget.Cells(targetRow, 12).Value = wsSource.Cells(sourceRow, 21).Value
  51. wsTarget.Cells(targetRow, 13).Value = wsSource.Cells(sourceRow, 23).Value
  52. wsTarget.Cells(targetRow, 14).Value = wsSource.Cells(sourceRow, 1).Value
  53. MsgBox "Data updated successfully."
  54. Else
  55. MsgBox "No data found for the specified date or 'Release to Logistics' value not found."
  56. End If
  57. Else
  58. MsgBox "Invalid date entered. Please try again."
  59. End If
  60. ' Close the source file without saving changes
  61. wbSource.Close SaveChanges:=False
  62. End Sub

答案1

得分: 1

  1. Option Explicit
  2. Sub UpdateData()
  3. Const SRCFILE = "C:\Users\E12\Desktop\FY23.xlsx" ' 请更新为源文件的路径
  4. Dim wbSource As Workbook, wsSource As Worksheet
  5. Dim wbTarget As Workbook, wsTarget As Worksheet
  6. Dim sourceRow As Variant, targetRow As Long
  7. Dim arCol, i As Long
  8. ' 提示用户输入日期
  9. Dim targetDate As Date, targetDateStr As String
  10. targetDateStr = Application.InputBox("输入日期 (DD MMM YY)", Type:=2)
  11. ' 检查目标日期是否有效
  12. If Not IsDate(targetDateStr) Then
  13. MsgBox "'" & targetDateStr & "' 不是有效日期。请重试。", vbExclamation
  14. Exit Sub
  15. Else
  16. targetDate = DateValue(targetDateStr)
  17. ' 格式化目标日期为 "dd-mmm-yy"
  18. targetDateStr = Format(targetDate, "dd-mmm-yy")
  19. End If
  20. ' 打开源文件并设置源工作表
  21. Set wbSource = Workbooks.Open(SRCFILE)
  22. Set wsSource = wbSource.Sheets("Pull in") ' 请更新为源工作表的名称
  23. ' 打开您的文件并设置目标工作表
  24. Set wbTarget = ThisWorkbook ' 如果您的文件不同,请更新
  25. Set wsTarget = wbTarget.Sheets("Released") ' 请更新为目标工作表的名称
  26. targetRow = 2 ' 根据需要更新
  27. ' 根据日期和“Release to Logistics”值在源文件中查找行
  28. Dim rngSearch As Range
  29. Set rngSearch = wsSource.Columns("W")
  30. sourceRow = Application.Match(CDbl(targetDate), rngSearch, 0)
  31. ' 如果找到匹配项,则更新您的文件中的数据
  32. If Not IsError(sourceRow) Then
  33. ' 从源文件映射到您的文件的特定列
  34. arCol = Array("B", "D", "E", "F", "G", "H", "I", "L", "M", "P", "R", "U", "W", "A")
  35. For i = 0 To UBound(arCol)
  36. wsTarget.Cells(targetRow, i + 1) = wsSource.Cells(sourceRow, arCol(i)).Value
  37. Next
  38. MsgBox targetDateStr & " 的数据已成功更新", vbInformation, "目标行 " & targetRow
  39. Else
  40. MsgBox targetDateStr & " 没有找到数据或未找到 'Release to Logistics' 值。"
  41. End If
  42. ' 不保存更改关闭源文件
  43. wbSource.Close SaveChanges:=False
  44. End Sub
英文:
  1. Option Explicit
  2. Sub UpdateData()
  3. Const SRCFILE = "C:\Users\E12\Desktop\FY23.xlsx" ' Update with the path to the source file
  4. Dim wbSource As Workbook, wsSource As Worksheet
  5. Dim wbTarget As Workbook, wsTarget As Worksheet
  6. Dim sourceRow As Variant, targetRow As Long
  7. Dim arCol, i As Long
  8. ' Prompt the user to enter a date
  9. Dim targetDate As Date, targetDateStr As String
  10. targetDateStr = Application.InputBox("Enter the date (DD MMM YY)", Type:=2)
  11. ' Check if the target date is a valid date
  12. If Not IsDate(targetDateStr) Then
  13. MsgBox "'" & targetDateStr & "' is not a valid date. Please try again.", vbExclamation
  14. Exit Sub
  15. Else
  16. targetDate = DateValue(targetDateStr)
  17. ' Format the target date to "dd-mmm-yy"
  18. targetDateStr = Format(targetDate, "dd-mmm-yy")
  19. End If
  20. ' Open the source file and set the source worksheet
  21. Set wbSource = Workbooks.Open(SRCFILE)
  22. Set wsSource = wbSource.Sheets("Pull in") ' Update with the name of the source sheet
  23. ' Open your file and set the target worksheet
  24. Set wbTarget = ThisWorkbook ' Update if your file is different
  25. Set wsTarget = wbTarget.Sheets("Released") ' Update with the name of the target shee
  26. targetRow = 2 ' as appropriate
  27. ' Find the row in the source file based on the date and "Release to Logistics" value
  28. Dim rngSearch As Range
  29. Set rngSearch = wsSource.Columns("W")
  30. sourceRow = Application.Match(CDbl(targetDate), rngSearch, 0)
  31. ' Update the data in your file if a match is found
  32. If Not IsError(sourceRow) Then
  33. ' Map specific columns from the source file to your file
  34. arCol = Array("B", "D", "E", "F", "G", "H", "I", "L", "M", "P", "R", "U", "W", "A")
  35. For i = 0 To UBound(arCol)
  36. wsTarget.Cells(targetRow, i + 1) = wsSource.Cells(sourceRow, arCol(i)).Value
  37. Next
  38. MsgBox "Data updated successfully for " & targetDateStr, vbInformation, "Target Row " & targetRow
  39. Else
  40. MsgBox "No data found for " & targetDateStr & " or 'Release to Logistics' value not found."
  41. End If
  42. ' Close the source file without saving changes
  43. wbSource.Close SaveChanges:=False
  44. End Sub
  45. </details>

huangapple
  • 本文由 发表于 2023年6月25日 23:48:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76551224.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定