У меня есть код, содержащий вложенные операторы if, и я получаю ошибки «Конец с без с» или «Конец с без «если». Мне нужна помощь, что я должен изменить в своем коде. Вот логика, которую мне нужно создать:
У меня есть два листа, один называется "ИСХОДЯЩИЙ АЧ", а другой называется "ИСХОДЯЩИЙ ПРОВОД". Я пытаюсь проверить их содержимое, все, что не имеет содержимого, должно быть удалено, а другое должно быть переформатировано и названо «ИСХОДЯЩИМ». Если оба имеют содержимое, переформатируйте их, объедините в один из листов, назовите его «ИСХОДЯЩИЙ», а другой удалите.
Вот простая версия моих аннотаций кода:
'If OUTGOING ACH is empty Then
'Delete OUTGOING ACH
'If OUTGOING WIRE is empty
'Delete OUTGOING WIRE
'Else If OUTGOING WIRE isn't empty
'Reformat OUTGOING WIRE
'Rename OUTGOING WIRE to "OUTGOING"
'End If
'Else If OUTGOING ACH isn't empty Then
'Reformat OUTGOING ACH
'Rename OUTGOING ACH to "OUTGOING"
'If OUTGOING WIRE is empty
'Delete OUTGOING WIRE
'Else If OUTGOING WIRE isn't empty
'Reformat OUTGOING WIRE
'Copy OUTGOING WIRE to OUTGOING (formerly "OUTGOING ACH")
'Delete OUTGOING WIRE
'End If
'End If
Ранее я определил заголовок и низ как строку заголовка и последнюю строку с данными. Вот полный код:
With NewBatch.Sheets("OUTGOING ACH")
Bottom = .Cells(.Rows.Count, 1).End(xlUp).Row
Header = Application.Match("Account*", .Range("A:A"), 0)
If Bottom = Header Then 'If Outgoing ACH is empty >> If 1
Application.DisplayAlerts = False 'Delete it and go to Outgoing wire
NewBatch.Sheets("OUTGOING ACH").Delete
Application.DisplayAlerts = True
End With
With NewBatch.Sheets("OUTGOING WIRE")
Bottom = .Cells(.Rows.Count, 1).End(xlUp).Row
Header = Application.Match("Account*", .Range("A:A"), 0)
If Bottom = Header Then 'If Outgoing wire is also empty >> If 2
Application.DisplayAlerts = False 'Delete it also
NewBatch.Sheets("OUTGOING WIRE").Delete
Application.DisplayAlerts = True
ElseIf Bottom <> Header Then 'But if Outgoing wire is not empty >> Else 2
.Activate 'Reformat OUTGOING WIRE
.Columns("B:D").Delete Shift:=xlToLeft
.Columns("C:Z").Delete Shift:=xlToLeft
.Columns("A:A").Insert Shift:=xlToRight
.Columns("A:A").Insert Shift:=xlToRight
.Columns("A:A").Insert Shift:=xlToRight
.Columns("E:E").Cut
.Columns("D:D").Insert Shift:=xlToRight
.Range("A" & Header).Select
Selection.FormulaR1C1 = "Payment Account (Kyriba Account Code)"
ActiveCell.Offset(0, 1).Select
Selection.FormulaR1C1 = "Transaction Code (CCD, PPD, FEDW, INTW, or DDBT)"
ActiveCell.Offset(0, 1).Select
ActiveCell.FormulaR1C1 = "Transaction Date (mm/dd/yyyy)"
ActiveCell.Offset(0, 2).Select
ActiveCell.FormulaR1C1 = "Third Party"
ActiveCell.Offset(0, 1).Select
ActiveCell.FormulaR1C1 = "CCY"
ActiveCell.Offset(0, 1).Select
ActiveCell.FormulaR1C1 = "Batch ID"
.Range(("E" & Header + 1) & ":" & "E" & Bottom).Copy
.Range(("A" & Header + 1) & ":" & "A" & Bottom).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
.Range(("B" & Header + 1) & ":" & "B" & Bottom).Value = "FEDW"
.Range(("C" & Header + 1) & ":" & "C" & Bottom).Value = Date
.Range(("F" & Header + 1) & ":" & "F" & Bottom).Value = "USD"
.Range(("G" & Header + 1) & ":" & "G" & Bottom).Value = Format(Now, "mmddyyyyhmmss")
.Range(("G" & Header + 1) & ":" & "G" & Bottom).NumberFormat = "#"
.Range("H" & Header).Value = "-1"
.Range("H" & Header).Copy
.Range(("D" & Header + 1) & ":" & "D" & Bottom).PasteSpecial Paste:=xlPasteAll, Operation:=xlMultiply, _
SkipBlanks:=False, Transpose:=False
.Range("H" & Header).ClearContents
.Rows("1:" & Header - 1).Delete Shift:=xlUp
.Cells.Select
With Selection.Interior
.Pattern = xlNone
.TintAndShade = 0
.PatternTintAndShade = 0
End With
With Selection.Font
.Name = "Calibri"
.Size = 11
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.ThemeColor = xlThemeColorLight1
.TintAndShade = 0
.ThemeFont = xlThemeFontMinor
.Bold = False
End With
With Selection.Borders
.LineStyle = xlNone
End With
With Selection
.WrapText = False
.EntireColumn.AutoFit
.EntireRow.AutoFit
End With
.Columns("D:D").NumberFormat = "0.00"
.Range("A1").Select
.Name = ("OUTGOING")
End If '>> End of If 2 (ACH is empty and whether or not Wire is empty)
End With
'Now, if Outgoing ACH wasn't empty >> Else If 1
'First reformat Outgoing ACH
With NewBatch.Sheets("OUTGOING ACH")
Bottom = .Cells(.Rows.Count, 1).End(xlUp).Row
Header = Application.Match("Account*", .Range("A:A"), 0)
ElseIf Bottom <> Header Then
.Activate
.Columns("B:D").Delete Shift:=xlToLeft
.Columns("C:Z").Delete Shift:=xlToLeft
.Columns("A:A").Insert Shift:=xlToRight
.Columns("A:A").Insert Shift:=xlToRight
.Columns("A:A").Insert Shift:=xlToRight
.Columns("E:E").Cut
.Columns("D:D").Insert Shift:=xlToRight
.Range("A" & Header).Select
Selection.FormulaR1C1 = "Payment Account (Kyriba Account Code)"
ActiveCell.Offset(0, 1).Select
Selection.FormulaR1C1 = "Transaction Code (CCD, PPD, FEDW, INTW, or DDBT)"
ActiveCell.Offset(0, 1).Select
ActiveCell.FormulaR1C1 = "Transaction Date (mm/dd/yyyy)"
ActiveCell.Offset(0, 2).Select
ActiveCell.FormulaR1C1 = "Third Party"
ActiveCell.Offset(0, 1).Select
ActiveCell.FormulaR1C1 = "CCY"
ActiveCell.Offset(0, 1).Select
ActiveCell.FormulaR1C1 = "Batch ID"
.Range(("E" & Header + 1) & ":" & "E" & Bottom).Copy
.Range(("A" & Header + 1) & ":" & "A" & Bottom).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
.Range(("B" & Header + 1) & ":" & "B" & Bottom).Value = "CCD"
.Range(("C" & Header + 1) & ":" & "C" & Bottom).Value = Date
.Range(("F" & Header + 1) & ":" & "F" & Bottom).Value = "USD"
.Range(("G" & Header + 1) & ":" & "G" & Bottom).Value = Format(Now, "mmddyyyyhmmss")
.Range(("G" & Header + 1) & ":" & "G" & Bottom).NumberFormat = "#"
.Range("H" & Header).Value = "-1"
.Range("H" & Header).Copy
.Range(("D" & Header + 1) & ":" & "D" & Bottom).PasteSpecial Paste:=xlPasteAll, Operation:=xlMultiply, _
SkipBlanks:=False, Transpose:=False
.Range("H" & Header).ClearContents
.Rows("1:" & Header - 1).Delete Shift:=xlUp
.Cells.Select
With Selection.Interior
.Pattern = xlNone
.TintAndShade = 0
.PatternTintAndShade = 0
End With
With Selection.Font
.Name = "Calibri"
.Size = 11
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.ThemeColor = xlThemeColorLight1
.TintAndShade = 0
.ThemeFont = xlThemeFontMinor
.Bold = False
End With
With Selection.Borders
.LineStyle = xlNone
End With
With Selection
.WrapText = False
.EntireColumn.AutoFit
.EntireRow.AutoFit
End With
.Columns("D:D").NumberFormat = "0.00"
.Range("A1").Select
.Name = ("OUTGOING")
End With
With NewBatch.Sheets("OUTGOING WIRE") 'Then check if Ougoing Wire is empty
Bottom = .Cells(.Rows.Count, 1).End(xlUp).Row
Header = Application.Match("Account*", .Range("A:A"), 0)
If Bottom = Header Then 'If Outgoing Wire is empty >> If 3
Application.DisplayAlerts = False 'Delete Outgoing Wire
NewBatch.Sheets("OUTGOING WIRE").Delete
Application.DisplayAlerts = True
ElseIf Bottom <> Header Then 'If Outgoing Wire isn't empty (both aren't empty) >> Else if 3
.Activate 'Reformat outgoing wire
.Columns("B:D").Delete Shift:=xlToLeft
.Columns("C:Z").Delete Shift:=xlToLeft
.Columns("A:A").Insert Shift:=xlToRight
.Columns("A:A").Insert Shift:=xlToRight
.Columns("A:A").Insert Shift:=xlToRight
.Columns("E:E").Cut
.Columns("D:D").Insert Shift:=xlToRight
.Range("A" & Header).Select
.Selection.FormulaR1C1 = "Payment Account (Kyriba Account Code)"
ActiveCell.Offset(0, 1).Select
Selection.FormulaR1C1 = "Transaction Code (CCD, PPD, FEDW, INTW, or DDBT)"
ActiveCell.Offset(0, 1).Select
ActiveCell.FormulaR1C1 = "Transaction Date (mm/dd/yyyy)"
ActiveCell.Offset(0, 2).Select
ActiveCell.FormulaR1C1 = "Third Party"
ActiveCell.Offset(0, 1).Select
ActiveCell.FormulaR1C1 = "CCY"
ActiveCell.Offset(0, 1).Select
ActiveCell.FormulaR1C1 = "Batch ID"
.Range(("E" & Header + 1) & ":" & "E" & Bottom).Copy
.Range(("A" & Header + 1) & ":" & "A" & Bottom).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
.Range(("B" & Header + 1) & ":" & "B" & Bottom).Value = "FEDW"
.Range(("C" & Header + 1) & ":" & "C" & Bottom).Value = Date
.Range(("F" & Header + 1) & ":" & "F" & Bottom).Value = "USD"
.Range(("G" & Header + 1) & ":" & "G" & Bottom).Value = Format(Now, "mmddyyyyhmmss")
.Range(("G" & Header + 1) & ":" & "G" & Bottom).NumberFormat = "#"
.Range("H" & Header).Value = "-1"
.Range("H" & Header).Copy
.Range(("D" & Header + 1) & ":" & "D" & Bottom).PasteSpecial Paste:=xlPasteAll, Operation:=xlMultiply, _
SkipBlanks:=False, Transpose:=False
.Range("H" & Header).ClearContents
.Rows("1:" & Header - 1).Delete Shift:=xlUp
.Cells.Select
With Selection.Interior
.Pattern = xlNone
.TintAndShade = 0
.PatternTintAndShade = 0
End With
With Selection.Font
.Name = "Calibri"
.Size = 11
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.ThemeColor = xlThemeColorLight1
.TintAndShade = 0
.ThemeFont = xlThemeFontMinor
.Bold = False
End With
With Selection.Borders
.LineStyle = xlNone
End With
With Selection.Borders
.WrapText = False
.EntireColumn.AutoFit
.EntireRow.AutoFit
End With
.Columns("D:D").NumberFormat = "0.00"
Bottom = WorksheetFunction.Match((Cells(Rows.Count, 1).End(xlUp)), Range("A:A"), 0)
.Rows("2" & ":" & Bottom).Copy
End With
With NewBatch.Sheets("OUTGOING")
Bottom = WorksheetFunction.Match((Cells(Rows.Count, 1).End(xlUp)), Range("A:A"), 0)
.Rows(Bottom + 1).PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
.Range("A1").Select
End With
Application.DisplayAlerts = False
Sheets("OUTGOING WIRE").Delete
Application.DisplayAlerts = True
End If 'End of If 3 (ACH isn't empty and whether or not wire is empty)
End If 'End of If 1 (The whole test for ACH and Outgoing Wire)
Я пытался изменить положение End If
и End With
, но это не сработало.
Спасибо!
@BigBen, но для этого есть еще вариант If. Могу ли я все еще закончить его там?
Bottom = WorksheetFunction.Match((Cells(Rows.Count, 1).End(xlUp)), Range("A:A"), 0)
не относится к содержащему With
блочному объекту
ElseIf Bottom <> Header Then
не имеет вступительного If
теста. В вашем коде много проблем, но их так много, что трудно их все обнаружить.
@TimWilliams Первоначально мой код был If Bottom = Header Then Application.DisplayAlerts = False NewBatch.Sheets("OUTGOING WIRE").Delete Application.DisplayAlerts = True Else 'format the sheet
, но, как я обнаружил, Else
не работает, я подумал, что он будет более конкретным, если я изменю его на ElseIf
Однако есть несколько случаев ElseIf Bottom <> Header Then
. По крайней мере один из них не открывается If...Then
Ваш End With
находится внутри оператора If
. Это проблема, если открытия With
нет.
Пожалуйста, установите бесплатную и фантастическую надстройку Rubberduck для VBA и используйте отступ для правильного отступа вашего кода. Затем вы увидите, что первый with на самом деле сочетается с концом if. Есть и другие несоответствия.
Как указывали другие, в вашем коде много ошибок, но вот некоторые из них:
With NewBatch.Sheets("OUTGOING ACH")
Bottom = .Cells(.Rows.Count, 1).End(xlUp).Row
Header = Application.Match("Account*", .Range("A:A"), 0)
If Bottom = Header Then 'If Outgoing ACH is empty >> If 1
Application.DisplayAlerts = False 'Delete it and go to Outgoing wire
NewBatch.Sheets("OUTGOING ACH").Delete
Application.DisplayAlerts = True
End If '<= this is the closing `End If` for 'If Bottom = Header Then'
End With
Далее раздел, начинающийся с:
With NewBatch.Sheets("OUTGOING ACH")
Bottom = .Cells(.Rows.Count, 1).End(xlUp).Row
Header = Application.Match("Account*", .Range("A:A"), 0)
If (something = true) Then '<= Missing `If` initialization in your code
'do something
ElseIf Bottom <> Header Then
'whole bunch of code ending with last few lines as per below
.Columns("D:D").NumberFormat = "0.00"
.Range("A1").Select
.Name = ("OUTGOING")
End If '<= Missing `End If` statement to close the `If (something = true) Then' statement
End With
Далее раздел, начинающийся с:
With NewBatch.Sheets("OUTGOING WIRE") 'Then check if Ougoing Wire is empty
Bottom = .Cells(.Rows.Count, 1).End(xlUp).Row
Header = Application.Match("Account*", .Range("A:A"), 0)
If Bottom = Header Then 'If Outgoing Wire is empty >> If 3
Application.DisplayAlerts = False 'Delete Outgoing Wire
NewBatch.Sheets("OUTGOING WIRE").Delete
Application.DisplayAlerts = True
ElseIf Bottom <> Header Then
'whole bunch of code ending with last few lines as per below
.Columns("D:D").NumberFormat = "0.00"
Bottom = WorksheetFunction.Match((Cells(Rows.Count, 1).End(xlUp)), Range("A:A"), 0)
.Rows("2" & ":" & Bottom).Copy
End If '<= Missing 'End If' to close the 'If Bottom = Header Then' statement
End With
Наконец-то:
With NewBatch.Sheets("OUTGOING")
Bottom = WorksheetFunction.Match((Cells(Rows.Count, 1).End(xlUp)), Range("A:A"), 0)
.Rows(Bottom + 1).PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
.Range("A1").Select
End With
Application.DisplayAlerts = False
Sheets("OUTGOING WIRE").Delete
Application.DisplayAlerts = True
End If 'End of If 3 (ACH isn't empty and whether or not wire is empty)
End If '<= These last two 'End If' statements do not have an opening statement? i.e. a corresponding 'If (something = true) Then'
Не забудьте правильно инкапсулировать свои утверждения If
, поскольку они должны быть внутри заявления With
.
With <something>
If (something = true) Then
'do something
ElseIf (somethingElse = true) Then
'do something else
Else
'do fallback
End If
End With
Вам обязательно нужен
End If
после первогоApplication.DisplayAlerts = True
.