所以我有一些CSV文件,其中包含一个月的小时数据。我想编写一个VBA代码来检索每个CSV文件,并将每日总数写入新创建的工作簿。在这个新工作簿中,每个CSV文件都有自己的工作表,在新工作表中,每小时数据将转换为每日总数。
到目前为止,除了正确的计算外,我的代码完成了以上所有操作。如果特定小时属于相关日期,我会通过将每小时的值添加到每行的空白变量来获得每日总计。 (例如 - 1 / 12/2016 11:00的值将添加到对应于1/12:2016的目标列中。
但是,我的源表是文件夹中的CSV文件,我的目标是新创建的excel文件。除了计算,我的代码做的一切都正确。我的逻辑很准确。所以可能问题在于我没有正确地执行我的价值观。
Do While k <= how_many_cols ' Each column in the data set
destisheet.Cells(1, k).Value = sourcesheet.Cells(1, k).Value ' Column titles
n = 2
Do While n - 1 < m ' n - 1 to get the last entry // Each row in the result set eg 1st = 31st
x = 0 ' reset the totaliser
j = 2 ' reset the row counter
Do While j <= col_length
'------
' Check date and totalise daily data amounts. Takes into account daylight savings
If (sourcesheet.Cells(j, 1).Value >= destisheet.Cells(n, 1).Value + dls) And _
(sourcesheet.Cells(j, 1).Value < destisheet.Cells(n + 1, 1).Value + dls) Then
Debug.Print sourcesheet.Cells(j, k).Address
x = x + sourcesheet.Cells(j, k).Value
Debug.Print " x = " & x
End If
j = j + 1
'------
Loop
destisheet.Cells(n, k).Value = x 'write to sheet
n = n + 1
Loop
k = k + 1
Loop
如果有人能告诉我这里做错了什么,我真的很感激。这只是代码的一小部分,但我认为这是相关的一点。我在一开始就定义了一切。
所以这不会引发任何错误。除了x的值始终为零,因此它将零写入目标表。重申一下,源表是一个CSV文件。目标表是一个单独的文件。
更令人困惑的是,如代码所示,它可以很好地导入标题。数据也与我尝试导入的内容相对应,因此它应该建立连接,而不是。
我对VBA很新,所以请原谅我的编码。任何帮助将不胜感激。