2013-07-30 123 views
0

這裏是我的代碼:總出現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 

有錯誤=總+ ...線。我的代碼正在做的是將工作表中的項目列表複製到新項目中。

然後,它必須搜索其他工作表以查找添加的每個項目名稱。其他每個工作表都會有2-3個記錄和項目名稱。我想從每個工作表中獲取項目的總成本,然後將其重新插入原始文件中。

步驟: 1.創建的項目列表

  • 通過列表迭代

  • 迭代通過各工作表

  • 從匹配項目

    共計值
  • 將值插回項目列表

  • Ĵ奧布萊恩,喬特和湯森是3個工作表

    enter image description here

    這是喬特工作

    ​​3210

    是爲了什麼,我想這種做法權實現?

    +3

    嘗試將您的總數從整數轉換爲長整數,您的數字可能會太大。 – NickSlash

    +2

    順便說一句,工作表(「Summary_Sheet(2)」)。範圍(「B5:H50」)。ClearContents'可能保存6行代碼。 – pnuts

    回答

    1

    你也問過這個(你的方法)是否是正確的方法,你的工作是這樣的。但是,它可能會被優化。

    對於項目列表中的每個項目,您都要在所有數據表中遍歷數據表中的每一行。

    其中,取決於每張紙的行數,是少數! (它至少是項目*行* 3)

    我不知道你的「項目」列表是你每次生成的項目,所以你只能得到一些項目,或者如果它只是你所擁有的一切。

    希望下面的代碼具有一定的意義,如果您決定放棄它,請確保將它運行在您的數據副本上!這是一個例子,它只將結果轉儲到調試窗口。

    下面的代碼(可能無法完美工作,因爲我可能會出現錯誤的列和行)會在每個工作表上循環一次,並計算每個工程的每張工作表總計(允許同一工程的多個實例在一張紙,如果可能的話在你的數據)

    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 
    

    使用這個你只需要進一步一次迭代在項目表中提取從結果所需的總計一次,然後遍歷每一張紙。

    4

    你可能會溢出Integer的最大大小,即32767.請嘗試使用long範圍循環計數器代替。 這將適用於a,x,z和rowsInI

    1

    我的常規方法是將所有可用數據保存在一個工作表中,以便我可以使用數據透視表對其進行分析。如果我然後需要創建一個非數據透視表工作表,然後我使用VBA將數據透視表複製並粘貼爲常規範圍。

    如果有一個很好的理由保持在3個獨立的工作表這個數據,我將合併使用VBA(基本上覆制和所有的數據粘貼到一個工作表中有一組列標題)的3張,增加額外的在複製/粘貼過程中包含「Choate」,「OBrien」或「Townsend」的列,然後從最終的合併中創建數據透視表。我使用這種方法很多 - 我的所有數據都是標準化的,我可以根據需要過濾數據透視表 - 按日期,項目,經理/銷售人員/創建者(Choate,Townsend和O'Brien),貨幣或任何其他類型。

    恐怕這不是嚴格地說你的問題的答案,更多的是一種不同方法的建議。當然,我不知道你如何獲得這些數據的情況,所以對你來說可能是不可行的。