工作表函数Sum-如果引起错误类型不匹配

时间:2019-11-04 21:32:14

标签: excel vba

我正在遍历工作表以将“ C”和“ D”的金额相加。

每个选项卡的M列中都有金额,而N列中是贷方还是借方,均由字母C或D表示。

首先,我将字母C和D放在空白单元格上,并尝试将这两个数字相加。

sumif行出现类型不匹配错误,并且整行都突出显示。

起初,我认为可能是ws = Worksheets(I)引起了问题,所以我将它们换成了Worksheets(I),但仍然无法正常工作。

Sub Sumiftabs()

Dim Filepath As String    
Dim UIGFile As Workbook    
Dim Column As String    
Dim Month As String    
Dim Year As Integer    
Dim I As Long    
Dim TY As Integer    
Dim TYLetter As String    
Dim T As Long
Dim TreatyYear As Long    
Dim WS_Count As Integer    
Dim ws As Worksheet


Application.ScreenUpdating = False

TreatyYear = Sheets("Control").Range("B7").Value

'On Error Resume Next

For T = TreatyYear To 16 Step -1    
TYLetter = WorksheetFunction.VLookup(T, Workbooks("Separate tabs.xlsm").Sheets("Control").Range("H:I"), 2, False)    
TY = Sheets("Control").Range("B7").Value    
Month = Sheets("Control").Range("B5").Value    
Year = Sheets("Control").Range("B4").Value    
Filepath = Sheets("Control").Range("B2").Value    

Set UIGFile = Workbooks.Open("K:\Ayoon\UIG\" & Year & "\" & Month & "\UIG_NSA_MonYear_TY" & T & ".xls")

WS_Count = UIGFile.Worksheets.Count

For I = WS_Count To 1 Step -1

Set ws = Worksheets(I)

'Direct Premium
UIGFile.Worksheets(I).Range("S4").Value = "C"    
UIGFile.Worksheets(I).Range("S5").Value = "D"    
UIGFile.Worksheets(I).Range("T4").Value = _
   Application.WorksheetFunction.SumIf(ws.Range("N:N"), ws.Range("S:S"), ws.Range("M:M"))

UIGFile.Worksheets(I).Range("T5").Value = _
   Application.WorksheetFunction.SumIf(ws.Range("N:N"), ws.Range("S:S"), ws.Range("M:M"))

Next I

UIGFile.Close savechanges:=True


Next T

Application.ScreenUpdating = True

End Sub

1 个答案:

答案 0 :(得分:0)

您的SUMIF看起来不正确。

例如,尝试以下操作:

UIGFile.Worksheets(I).Range("T4").Value = _
    Application.WorksheetFunction.SumIf(ws.Range("N:N"), "C", ws.Range("M:M"))