Excel溢出错误

这是我的代码:

Dim i As Integer, a As Integer, rowsInThere As Integer, rowsInI As Integer Dim ws As Worksheet, b As Integer Dim x As Integer, z As Integer Dim total As Integer Dim value As String rowsInProjects = Sheets("Projects").UsedRange.Rows.Count z = 3 Worksheets("Summary_Sheet (2)").Range("b5:b50").ClearContents Worksheets("Summary_Sheet (2)").Range("c5:c50").ClearContents Worksheets("Summary_Sheet (2)").Range("d5:d50").ClearContents Worksheets("Summary_Sheet (2)").Range("e5:e50").ClearContents Worksheets("Summary_Sheet (2)").Range("F5:F50").ClearContents Worksheets("Summary_Sheet (2)").Range("G5:G50").ClearContents Worksheets("Summary_Sheet (2)").Range("H5:H50").ClearContents For a = 1 To rowsInProjects value = Worksheets("Projects").Cells(a, 1).value Worksheets("Summary_Sheet (2)").Cells(a + 4, 2).value = value For i = 5 To Worksheets.Count rowsInI = Worksheets(i).UsedRange.Rows.Count For x = 1 To rowsInI If Worksheets(i).Cells(x + 8, 3).value = value Then total = total + Worksheets(i).Cells(x + 8, 6).value End If Worksheets("Summary_Sheet (2)").Cells(i, z).value = total Next x z = z + 1 Next i z = 3 Next a 

总数=总数+ …线出现错误。 我的代码正在做的是从工作表复制一个项目列表到一个新的。

然后必须通过其他工作表来search每个添加的项目名称。 每个其他工作表将有2-3个logging与项目名称。 我想从每个工作表中获取项目的总成本,然后将其插回到原始文件中。

步骤:1.创build项目列表

  1. 迭代List

  2. 遍历每个工作表

  3. 合计项目的价值

  4. 将值插回到项目列表中

J O'Brien,Choate和Townsend是3个工作表

在这里输入图像说明

这是Choate工作表

在这里输入图像说明

这种方法适合我所要达到的目的吗?

你也问是否这个(你的方法)是正确的方法,你的工作是这样的。 但是,它可能会被优化。

对于项目列表中的每个项目,您都要在所有数据表中遍历数据表中的每一行。

其中,取决于每张纸的行数,是less数! (至less项目*行* 3)

我不知道你的“项目”列表是否是你每次生成的项目,所以你只能得到一些项目,或者如果它只是你所拥有的一切。

希望下面的代码是有道理的,如果你决定放弃它,请确保你运行它的数据副本! 这是一个例子,它只是将结果转储到debugging窗口。

下面的代码(可能不会完美,因为我可能会得到错误的列和行)将遍历每个表一次,并计算每个项目的每张总计(允许在同一个工作表中的多个实例,如果这在你的数据中是可能的)

 Sub Main() Dim Projects As Object 'Dim Projects As Scripting.Dictionary Set Projects = CreateObject("Scripting.Dictionary") 'Set Projects = New Scripting.Dictionary Dim Project As String Dim Sheets() As String Dim Name As String Dim Sheet As Worksheet Dim SheetIndex As Integer Dim ProjectColumn As Variant Dim TotalColumn As Variant Dim Index As Integer Dim Max As Long Dim MaxRow As Long ' You'll need to put your sheet names below ' not very nice, just a way to predefine an array containing sheet names Sheets = Split("Sheet1,Sheet2,Sheet3", ",") ' loop over all the sheets For SheetIndex = 0 To UBound(Sheets) ' get a reference to the sheet were looking at Set Sheet = ThisWorkbook.Worksheets(Sheets(SheetIndex)) ' calculate the last row in the workbook (as using UsedRange isnt always right) MaxRow = Sheet.Cells(Sheet.Rows.Count, 3).End(xlUp).Row ' get the data were looking for ' the 9 in the next 2 lines might be wrong, it should be the row# for the first data row Set ProjectColumn = Sheet.Range(Sheet.Cells(9, 3), Sheet.Cells(MaxRow, 3)) ' the 9 here might be wrong! Set TotalColumn = Sheet.Range(Sheet.Cells(9, 6), Sheet.Cells(MaxRow, 6)) ' again, 9 Max = MaxRow - 8 ' adjust the max row to account for the +8 header cells above the data For Index = 1 To Max ' loop over all the projects in the current sheet Project = ProjectColumn(Index, 1) ' this allows for multiple instances of the same project per sheet (no idea if this occurs in your data) If Projects.Exists(Project) Then If Projects(Project).Exists(Sheets(SheetIndex)) Then ' update the total Projects(Project)(Sheets(SheetIndex)) = Projects(Project)(Sheets(SheetIndex)) + TotalColumn(Index, 1) Else ' inclue the total for the sheet Projects(Project).Add Sheets(SheetIndex), CLng(TotalColumn(Index, 1)) End If Else ' new project, add it and the total value for the current sheet 'Projects.Add Project, New Scripting.Dictionary Projects.Add Project, CreateObject("Scripting.Dictionary") Projects(Project).Add Sheets(SheetIndex), CLng(TotalColumn(Index, 1)) End If Next Index Set ProjectColumn = Nothing Set TotalColumn = Nothing Next SheetIndex ' Projects now contains a list of all projects, and the totals for your sheets. ' Projects ' - Project Name ' - - Sheet Name - Sheet Total ' dump the data to the immediate window in the vba editor For Each Key In Projects.Keys For Each SubKey In Projects(Key).Keys Debug.Print Key & ", " & SubKey & " = " & Projects(Key)(SubKey) Next SubKey Next Key End Sub 

使用这个,你只需要遍历每张纸一次,然后在项目表上进一步迭代,从结果中提取所需的总和。

你可能会溢出一个Integer的最大值,也就是32767.尝试使用long范围循环计数器来代替。 这将适用于a,x,z和rowsInI

我通常的做法是将所有可用的数据保存在一个工作表中,以便我可以使用数据透视表来分析它。 如果我然后需要创build一个非数据透视表工作表,然后我使用VBA复制和粘贴特殊数据透视表作为一个常规的范围。

如果在三个单独的工作表中保存这些数据是非常好的理由,我将使用VBA(基本上将所有数据复制并粘贴到一个带有一组列标题的单个工作表中)合并在一起,添加一个额外的列,其中包含在复制/粘贴过程中select“Choate”,“OBrien”或“Townsend”,然后从最终的合并中创build数据透视表。

我使用这种方法很多 – 我所有的数据都是标准化的,我可以根据需要过滤数据透视表 – 按date,项目,经理/销售人员/创build者(Choate,Townsend和O'Brien),货币或其他。

恐怕这不是严格的回答你的问题,更多的是build议采用不同的方法。 当然,我不知道你如何得到这些数据的情况,所以对你来说可能是不可行的。