Можно ли сохранить ответ функции, записанной следующим образом, в переменной?
'--------------------
'This is part of a somewhat bigger code so some variables has been declared again
'--------------------
Sub afletteren()
Dim vAfletteren As Boolean
Dim OutWb As Workbook
Dim OutFileStatus As Range
Dim OutFileHeader As Range
Dim Key1 As Variant
Dim Key2 As Variant
Dim OutFileKPL As Range
Dim OutFileGBR As Range
Dim OutFileBedrag As Range
Dim OutFileStatistieknr As Range
Dim OutFileVouchernr As Range
vAfletteren = True
Set OutWb = ActiveWorkbook
Set OutFileHeader = Range("D5")
If vAfletteren = True Then
With OutWb.Sheets("Dump")
Set OutFileKPL = .Cells(OutFileHeader.Row, 1).EntireRow.Find(What: = "KPL", LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:=False)
Set OutFileGBR = .Cells(OutFileHeader.Row, 1).EntireRow.Find(What: = "Rekeningnummer", LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:=False)
Set OutFileBedrag = .Cells(OutFileHeader.Row, 1).EntireRow.Find(What: = "BedragPrimair", LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:=False)
Set OutFileStatistieknr = .Cells(OutFileHeader.Row, 1).EntireRow.Find(What: = "Statistieknummer1", LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:=False)
Set OutFileVouchernr = .Cells(OutFileHeader.Row, 1).EntireRow.Find(What: = "FactVerplNr", LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:=False)
Set OutFileStatus = .Cells(OutFileHeader.Row, 1).EntireRow.Find(What: = "Status", LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:=False)
End With
'Whenever key1 = 0 then set the "Status" to "Afletteren", the first row of this example equals to 0 so after running this code, the outcome has to be "Afletteren" in the first cell of the status column
S = OutFileHeader.Row + 1
With OutWb.Sheets("Dump")
For T = OutFileHeader.Row + 1 To .Cells(Rows.Count, 1).End(xlUp).Row
If .Cells(S, OutFileStatus.Column).Value <> "Afletteren" Then
Key1 = Application.SumIfs(OutFileBedrag.EntireColumn.Address, OutFileKPL.EntireColumn.Address, .Range(Split(Cells(1, OutFileKPL.Column).Address, "$")(1) & S).Value, OutFileGBR.EntireColumn.Address, .Range(Split(Cells(1, OutFileGBR.Column).Address, "$")(1) & S).Value, OutFileVouchernr.EntireColumn.Address, .Range(Split(Cells(1, OutFileVouchernr.Column).Address, "$")(1) & S).Value)
'Key1 = Application.SumIfs(sumrange , Range 1 , Criteria 1 , Range 2 , Criteria 2 , Range 3 , Criteria 3
'Check
MsgBox "Application.SumIfs(" & OutFileBedrag.EntireColumn.Address & ", " & OutFileKPL.EntireColumn.Address & ", " & .Range(Split(Cells(1, OutFileKPL.Column).Address, "$")(1) & S).Value & ", " & OutFileGBR.EntireColumn.Address & ", " & .Range(Split(Cells(1, OutFileGBR.Column).Address, "$")(1) & S).Value & ", " & OutFileVouchernr.EntireColumn.Address & ", " & .Range(Split(Cells(1, OutFileVouchernr.Column).Address, "$")(1) & S).Value & ")"
MsgBox Key1 'has to be equal to 0
If Key1 = 0 Then
.Cells(S, OutFileStatus.Column).Value = "Afletteren"
End If
End If
S = S + 1
Next T
End With
End If
Exit Sub
ERROR_HANDLING:
MsgBox "Error Handling = " & vStatus
End Sub
Всякий раз, когда я запускаю этот фрагмент кода, он возвращает ошибку типа в строке msgbox. можно ли сохранить ответ функции в переменной вместо предопределенного диапазона? я хочу продолжать всякий раз, когда функция Sumifs возвращает значение 0.
я надеюсь услышать от кого-то.
Заранее спасибо,
Sub StackOverflow()
Dim Key1 As Variant
Key1 = Application.WorksheetFunction.SumIfs(Range("B1:B6"), Range("A1:A6"), Range("A1"))
MsgBox Key1
End Sub
Очевидно, вам нужно настроить диапазоны (вы можете использовать переменные, я просто вставил фиксированный диапазон).
Окно сообщений показывает числовой результат.
спасибо @Hronic за быстрый ответ. я еще не нашел решения, поэтому я изменил приведенный выше код на несколько больший, чтобы указать проблему. Ошибка возникает несколько в конце кода. В первом msgbox отображается следующая формула: Application.Sumifs($H:$H, $B:$B, 1, $F:$F, 100, $M:$M, 0), во втором msgbox выдается ошибка. Ответ на формулу всякий раз, когда я использую ее на самом листе, дает 0. Можете ли вы определить, что идет не так?
&& Я хочу опубликовать пример файла, но я думаю, что это невозможно. если есть способ и он нужен, то просветите меня как
не могли бы вы закинуть файл на гугл диск? я посмотрю на это
Да, вы можете сохранить результат функции рабочего листа в переменной вместо ячейки листа. Ваша проблема, скорее всего, в том, что
SumIfs
возвращает ошибку, которую нельзя отобразить с помощью MsgBox.