使用VBA将日期从.csv复制到.xlsx时,会交换日和月。

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

Using VBA to copy dates from a .csv to a .xlsx switches the day and month

问题

我在Excel中使用VBA有很多经验,但我在一个看似简单的问题上遇到了困难:

我有大量的.csv文件,我想要将这些数据汇总到一个.xlsx文件中。数据是日期和时间的列表,位于A列,然后是相应的数字在B和C列。我使用下面的代码逐个打开每个.csv文件,复制相关数据并粘贴到.xlsx文件中。

问题是,当我粘贴数据时,Excel会交换日期和月份,对于每个月的1到12号的日期(例如,02/01/2023变成了01/02/2023),甚至在.csv文件中也发生了变化。我知道日期格式可能会导致问题,但它在我复制的文件中发生变化真的让我感到困惑。

为了尝试解决这个问题,我在粘贴操作之前和之后将我的日期列的格式更改为与.csv文件相匹配(自定义:“dd/mm/yyyy hh:mm”),但没有效果。我还尝试了直接复制粘贴与粘贴值之间的区别,但没有任何不同。我还尝试了循环遍历范围中的每个单元格,使用xlsCell.value = csvCell.value,但仍然得到相同的结果。

以下是我的代码:

  1. Sub collateData()
  2. Dim StrFile As String
  3. Dim wb As Workbook, swb As Workbook
  4. Dim sht As Worksheet
  5. Dim pasteRow As Integer, headerRowCount As Integer, count As Integer
  6. headerRowCount = 8
  7. pasteRow = headerRowCount + 1
  8. Set wb = Workbooks.Open(ThisWorkbook.Path & "/Member ID - LJW6127 - collated.xlsx")
  9. StrFile = Dir("C:\Users\me\OneDrive\Member Id - LJW61Z7\*.csv")
  10. Do While Len(StrFile) > 0
  11. If Len(StrFile) = 26 Then
  12. Set swb = Workbooks.Open("C:\Users\me\OneDrive\Member Id - LJW61Z7\" & StrFile)
  13. Set sht = swb.Sheets(1)
  14. wb.Sheets("Hourly").Columns(1).NumberFormat = "dd/mm/yyyy hh:mm"
  15. sht.Range("A9:C" & sht.UsedRange.Rows.Count).Copy
  16. wb.Sheets("Hourly").Range("A" & pasteRow).PasteSpecial xlPasteValues
  17. pasteRow = pasteRow + sht.UsedRange.Rows.Count - headerRowCount
  18. Application.DisplayAlerts = False
  19. swb.Close
  20. Application.DisplayAlerts = True
  21. DoEvents
  22. Application.CutCopyMode = False
  23. End If
  24. StrFile = Dir
  25. Loop
  26. wb.Sheets("Hourly").Columns(1).NumberFormat = "dd/mm/yyyy hh:mm"
  27. End Sub

非常感谢您的帮助,提前致以感谢!

英文:

I have a lot of experience with using VBA in Excel but I'm really struggling with something that feels like it should be straight forward:

I have a large number of .csv files and I would like to collate the data into one .xlsx file. The data is a list of dates and time in column A and then corresponding figures in columns B and C. I'm using the code below to open each .csv one at a time, copy the relevant data and paste it into the .xlsx.

The problem is that when I paste the data, Excel is swapping the day and month for days 1 to 12 of the month (e.g. 02/01/2023 becomes 01/02/2023), IT IS EVEN CHANGING THEM IN THE .CSV. I know there can be issues with date formats but the fact that it is changing in the file I am copying FROM has really baffled me.

In an attempt to fix this I am changing the format of my dates column to match that of the .csv (CUSTOM: "dd/mm/yyyy hh:mm") before and after the paste operation, but to no avail. I've also tried a straight copy paste vs pasteValues and it doesn't make any difference. I've also tried looping through each cell in the range and using xlsCell.value = csvCell.value, but I still get the same result.

Here is my code:

  1. Sub collateData()
  2. Dim StrFile As String
  3. Dim wb As Workbook, swb As Workbook
  4. Dim sht As Worksheet
  5. Dim pasteRow As Integer, headerRowCount As Integer, count As Integer
  6. headerRowCount = 8
  7. pasteRow = headerRowCount + 1
  8. Set wb = Workbooks.Open(ThisWorkbook.Path & "/Member ID - LJW6127 - collated.xlsx")
  9. StrFile = Dir("C:\Users\me\OneDrive\Member Id - LJW61Z7\*.csv")
  10. Do While Len(StrFile) > 0
  11. If Len(StrFile) = 26 Then
  12. Set swb = Workbooks.Open("C:\Users\me\OneDrive\Member Id - LJW61Z7\" & StrFile)
  13. Set sht = swb.Sheets(1)
  14. wb.Sheets("Hourly").Columns(1).NumberFormat = "dd/mm/yyyy hh:mm"
  15. sht.Range("A9:C" & sht.UsedRange.Rows.count).Copy
  16. wb.Sheets("Hourly").Range("A" & pasteRow).PasteSpecial xlPasteValues
  17. pasteRow = pasteRow + sht.UsedRange.Rows.count - headerRowCount
  18. Application.DisplayAlerts = False
  19. swb.Close
  20. Application.DisplayAlerts = True
  21. DoEvents
  22. Application.CutCopyMode = False
  23. End If
  24. StrFile = Dir
  25. Loop
  26. wb.Sheets("Hourly").Columns(1).NumberFormat = "dd/mm/yyyy hh:mm"
  27. End Sub

Any help would be greatly appreciated, thank you in advance!

答案1

得分: 1

尝试 OpenText 方法

  1. Option Explicit
  2. Sub collateData()
  3. Const HDR = 8
  4. Const CSVFOLDER = "C:\Users\me\OneDrive\Member Id - LJW61Z7\"
  5. Const TARGET = "Member ID - LJW6127 - collated.xlsx"
  6. Dim wb As Workbook, rng As Range, ar
  7. Dim lastRow As Long, n As Long, StrFile As String
  8. Set wb = Workbooks.Open(ThisWorkbook.Path & "\" & TARGET)
  9. With wb.Sheets("Hourly")
  10. .Columns(1).NumberFormat = "dd/mm/yyyy hh:mm"
  11. Set rng = .Cells(HDR + 1, "A")
  12. End With
  13. StrFile = Dir(CSVFOLDER & "*.csv")
  14. Application.ScreenUpdating = False
  15. Do While Len(StrFile) > 0
  16. If Len(StrFile) = 26 Then
  17. Workbooks.OpenText Filename:=CSVFOLDER & StrFile, Origin:=xlWindows, _
  18. DataType:=xlDelimited, Comma:=True, local:=True, _
  19. FieldInfo:=Array(Array(0, 4), Array(1, 1), Array(2, 1))
  20. With ActiveWorkbook.Sheets(1)
  21. lastRow = .Cells(.Rows.count, "A").End(xlUp).Row
  22. ' 复制到数组
  23. ar = .Range("A" & HDR + 1 & ":C" & lastRow)
  24. ActiveWorkbook.Close False
  25. End With
  26. ' 更新数值
  27. rng.Resize(UBound(ar), UBound(ar, 2)) = ar
  28. Set rng = rng.Offset(UBound(ar))
  29. n = n + 1
  30. End If
  31. StrFile = Dir
  32. Loop
  33. Application.ScreenUpdating = True
  34. MsgBox n & " 个 CSV 文件已导入", vbInformation
  35. End Sub
英文:

Try OpenText method

  1. Option Explicit
  2. Sub collateData()
  3. Const HDR = 8
  4. Const CSVFOLDER = "C:\Users\me\OneDrive\Member Id - LJW61Z7\"
  5. Const TARGET = "Member ID - LJW6127 - collated.xlsx"
  6. Dim wb As Workbook, rng As Range, ar
  7. Dim lastRow As Long, n As Long, StrFile As String
  8. Set wb = Workbooks.Open(ThisWorkbook.Path & "\" & TARGET)
  9. With wb.Sheets("Hourly")
  10. .Columns(1).NumberFormat = "dd/mm/yyyy hh:mm"
  11. Set rng = .Cells(HDR + 1, "A")
  12. End With
  13. StrFile = Dir(CSVFOLDER & "*.csv")
  14. Application.ScreenUpdating = False
  15. Do While Len(StrFile) > 0
  16. If Len(StrFile) = 26 Then
  17. Workbooks.OpenText Filename:=CSVFOLDER & StrFile, Origin:=xlWindows, _
  18. DataType:=xlDelimited, Comma:=True, local:=True, _
  19. FieldInfo:=Array(Array(0, 4), Array(1, 1), Array(2, 1))
  20. With ActiveWorkbook.Sheets(1)
  21. lastRow = .Cells(.Rows.count, "A").End(xlUp).Row
  22. ' copy to array
  23. ar = .Range("A" & HDR + 1 & ":C" & lastRow)
  24. ActiveWorkbook.Close False
  25. End With
  26. ' update values
  27. rng.Resize(UBound(ar), UBound(ar, 2)) = ar
  28. Set rng = rng.Offset(UBound(ar))
  29. n = n + 1
  30. End If
  31. StrFile = Dir
  32. Loop
  33. Application.ScreenUpdating = True
  34. MsgBox n & " csv files imported", vbInformation
  35. End Sub
  36. </details>

huangapple
  • 本文由 发表于 2023年3月8日 19:39:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/75672562.html
匿名

发表评论

匿名网友

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

确定