2016-12-27 41 views
0
Sub summarize() 
Dim sh As Worksheet 
'Using sh for explicit summary worksheet for simplicity, doesn't have to reslect constantly 
Dim summarySheet As Worksheet 
'Defines the summarysheet as a worksheet 
Dim summaryExists As Boolean 
'Checks for truthiness 


Dim summarysheet9 As Worksheet 

Dim SummaryExists9 As Boolean 



Dim n As Integer 
n = ActiveWorkbook.Sheets.Count 
'How many sheets are in the workbook? 

Dim procMe() 
ReDim procMe(n) 
'Defining how many sheets we'll actually use 

Dim ii As Integer 

reportDate = WorksheetFunction.EoMonth(Date, -1) 
'We should be doing month closings in the next month. This finds the last day of the prior month, when month closings are booked 

RefDescription = Format(reportDate, "MMMM") & " close" 
'The batch description entry 

Dim headers() As Variant 
headers = Array("Reference", "Reference Desc", "Type", "Reversal Date", _ 
    "Close Date", "Project", "Job#", "Cost Code", "Account", _ 
    "Variance Code", "Amount", "Transaction Date", "Detail Description") 
'The headers for the summary sheet 

ii = 0 

REFCOL = 1 
DESCCOL = 2 
TYPECOL = 3 
REVCOL = 4 
CLOSECOL = 5 
PROJCOL = 6 
JOBCOL = 7 
CCCOL = 8 
ACCCOL = 9 
VARCOL = 10 
AMTCOL = 11 
DATECOL = 12 
DDESCCOL = 13 

'Defines the location of each column, and what goes in each column 

summaryExists = False 
'Setting it to false initially, so that if it isn't found, we call it false. Just incase VBA decides to call it true by default. 

SummaryExists9 = False 


For Each sh In ActiveWorkbook.Sheets 

    If sh.Name = "JournalEntryTransactions" Then 
    summaryExists = True 
    'If we find a sheet called JournalEntryTransactions, then we're good to go. 
    End If 


    If sh.Name = "JournalEntryTranscations9" Then 
    SummaryExists9 = True 
    'Same as above, but for 900 
    End If 


    If sh.Name = "Payroll" Then 
    sh.Activate 
    ii = ii + 1 
    procMe(ii) = "Payroll" 
    End If 
    'If we find a payroll sheet, we like it 

    If Val(sh.Name) > 0 Then 
    ii = ii + 1 
    procMe(ii) = sh.Name 
    'If the name of the sheet is an integer, we like it 

    Else 
    'Debug.Print "I don't know what to do with " & sh.Name 
    'Debugging when it wasn't working too well 

    End If 

Next sh 

ReDim Preserve procMe(ii) 

If Not summaryExists Then 
    Set summarySheet = ActiveWorkbook.Sheets.Add 
    summarySheet.Name = "JournalEntryTransactions" 
    'If we don't have a summary sheet, add it in 

Else 
    Set summarySheet = ActiveWorkbook.Sheets("JournalEntryTransactions") 
    summarySheet.Range("A1", summarySheet.Cells.SpecialCells(xlCellTypeLastCell)).Clear 
End If 
'If we already have a summary sheet, we're clearing it out 

If Not SummaryExists9 Then 
    Set summarysheet9 = ActiveWorkbook.Sheets.Add 
    summarysheet9.Name = "JournalEntryTransactions9" 
    'If we don't have a summary sheet, add it in 

Else 
    **Set summarysheet9 = ActiveWorkbook.Sheets("JournalEntryTransactions9")** 
    summarySheet.Range("A1", summarySheet.Cells.SpecialCells(xlCellTypeLastCell)).Clear 
End If 
'If we already have a summary sheet, we're clearing it out 

近終點時,集summarysheet9 = ActiveWorkbook.Sheets(「JournalEntryTransactions9」)是造成運行時錯誤9.對於我的生活,我不能找到我想念的東西。麻煩找出一個運行時間錯誤9

我想要做的是,如果我們有一個已經生成的彙總表(9),它將清除表(9)中的所有內容。

「常規」工作表(上面的代碼位) - 只是工作表9不起作用。當我沒有工作表時,它會生成良好的狀態,它只是生氣清理它。

+4

你有一個錯字 - ' 「JournalEntryTranscations9」''沒有 「JournalEntryTransactions9」'。 – Comintern

+0

謝謝!我修復了它,但它仍然對我生氣 – Selkie

回答

0

試試下面的代碼

Sub summarize() 

Dim sh As Worksheet 
'Using sh for explicit summary worksheet for simplicity, doesn't have to reslect constantly 
Dim summarySheet As Worksheet 
'Defines the summarysheet as a worksheet 
Dim summaryExists As Boolean 
'Checks for truthiness 


Dim summarysheet9 As Worksheet 

Dim SummaryExists9 As Boolean 



Dim n As Integer 
n = ActiveWorkbook.Sheets.Count 
'How many sheets are in the workbook? 

Dim procMe() 
ReDim procMe(n) 
'Defining how many sheets we'll actually use 

Dim ii As Integer 

reportDate = WorksheetFunction.EoMonth(Date, -1) 
'We should be doing month closings in the next month. This finds the last day of the prior month, when month closings are booked 

RefDescription = Format(reportDate, "MMMM") & "close" 
'The batch description entry 

Dim headers() As Variant 
headers = Array("Reference", "Reference Desc", "Type", "Reversal Date", _ 
    "Close Date", "Project", "Job#", "Cost Code", "Account", _ 
    "Variance Code", "Amount", "Transaction Date", "Detail Description") 
'The headers for the summary sheet 

ii = 0 

REFCOL = 1 
DESCCOL = 2 
TYPECOL = 3 
REVCOL = 4 
CLOSECOL = 5 
PROJCOL = 6 
JOBCOL = 7 
CCCOL = 8 
ACCCOL = 9 
VARCOL = 10 
AMTCOL = 11 
DATECOL = 12 
DDESCCOL = 13 

'Defines the location of each column, and what goes in each column 

summaryExists = False 
'Setting it to false initially, so that if it isn't found, we call it false. Just incase VBA decides to call it true by default. 

SummaryExists9 = False 


For Each sh In ActiveWorkbook.Sheets 

    If sh.Name = "JournalEntryTransactions" Then 
    summaryExists = True 
    'If we find a sheet called JournalEntryTransactions, then we're good to go. 
    End If 


    If sh.Name = "JournalEntryTransacations9" Then 
    SummaryExists9 = True 
    'Same as above, but for 900 
    End If 


    If sh.Name = "Payroll" Then 
    sh.Activate 
    ii = ii + 1 
    procMe(ii) = "Payroll" 
    End If 
    'If we find a payroll sheet, we like it 

    If Val(sh.Name) > 0 Then 
    ii = ii + 1 
    procMe(ii) = sh.Name 
    'If the name of the sheet is an integer, we like it 

    Else 
    'Debug.Print "I don't know what to do with " & sh.Name 
    'Debugging when it wasn't working too well 

    End If 

Next sh 

ReDim Preserve procMe(ii) 

If Not summaryExists Then 
    Set summarySheet = ActiveWorkbook.Sheets.Add 
    summarySheet.Name = "JournalEntryTransactions" 
    'If we don't have a summary sheet, add it in 

Else 
    Set summarySheet = ActiveWorkbook.Sheets("JournalEntryTransactions") 
    summarySheet.Range("A1", summarySheet.Cells.SpecialCells(xlCellTypeLastCell)).Clear 
End If 
'If we already have a summary sheet, we're clearing it out 

If Not SummaryExists9 Then 
    Set summarysheet9 = ActiveWorkbook.Sheets.Add 
    summarysheet9.Name = "JournalEntryTransactions9" 
    'If we don't have a summary sheet, add it in 

Else 
    Set summarysheet9 = ActiveWorkbook.Sheets("JournalEntryTransacations9") 
    summarySheet.Range("A1", summarySheet.Cells.SpecialCells(xlCellTypeLastCell)).Clear 
End If 
'If we already have a summary sheet, we're clearing it out 

End Sub 
+0

所以,我能找到的唯一區別就是修復JournalEntryTranscations9的拼寫錯誤。我修復了所有的拼寫錯誤,但仍然在同一位置出現同樣的錯誤 – Selkie