당신을 위해 일하는 것이이 솔루션을 땜질의 작은 비트와 함께, 당신은 이미 유효한 공식을 가지고 가정 :
Set ArrayFormula to many Excel cells using VBA
그러나, 수식은 1004 오류가 발생합니다 255 개 문자를 초과한다.
명명 된 범위를 사용하여이 수식을 상당히 축소 할 수 있습니다.
는 IMO는 분자를 계산하기 위해 QTY
열에서 SpecialCells(xlCellTypeVisible)
을 사용하여 다음 데이터베이스 범위에 AutoFilter
방법의 사용을 자동화하고, 아마도 더 쉽게
보다는 불쾌한 배열 수식을 사용하고 UPDATE 방정식의 & 분모는 메모리의 점수를 계산 한 다음 워크 시트 셀에 기록합니다.
이 메서드는 배열 수식과 동일한 결과를 반환하는 것으로 나타나며 수식에서 허용하지 않는 여러 "그룹"조건도 허용합니다. 여러 "그룹"기준 (예를 들어, 당신은을 통해 그룹 (11)을 수행 할 경우
Option Explicit
Sub UsingAutoFilters()
Dim wsDB As Worksheet, wsResults As Worksheet
Dim rngResults As Range, dbRange As Range
Dim sYear$, sRegion$, sStore$, sItmGrade$, sItem$, sWhse$, sType$, sGroup$
Dim r As Long, c As Long 'row & column iterator
Dim myDenominator, myNumerator As Double 'used for calculations
Set wsDB = Worksheets("Database")
'## This is the database table ##'
Set dbRange = wsDB.Range("A1:L8043")
Set wsResults = Worksheets("QueryResult")
'## This is the range of data in the QueryResults table, E8 to the end of the data ##'
Set rngResults = wsResults.Range("E8:H13") '## Modify as needed'
'## Iterate over columns in the results table'
For c = 1 To rngResults.Columns.Count
'Capture the filter values for each COLUMN.'
With wsResults
sYear = .Cells(1, c + 4).Value
sItmGrade = .Cells(2, c + 4).Value
sWhse = .Cells(3, c + 4).Value
sItem = .Cells(4, c + 4).Value
sType = .Cells(5, c + 4).Value
sGroup = .Cells(6, c + 4).Value
End With
'## Set the filters ##'
'Filter the year
dbRange.AutoFilter Field:=1, Criteria1:=sYear
'Filter the itemgrade'
dbRange.AutoFilter Field:=6, Criteria1:=sItmGrade
'Filter the whse'
dbRange.AutoFilter Field:=7, Criteria1:=sWhse
'Filter the Item#'
dbRange.AutoFilter Field:=8, Criteria1:=sItem
'## Now, for each row in the table...'
For r = 1 To rngResults.Rows.Count
'Capture the row filter values for each ROW.'
sRegion = wsResults.Cells(r + 7, 1).Value
sStore = wsResults.Cells(r + 7, 3).Value
'## Apply the row filters ##'
'Filter the region'
dbRange.AutoFilter Field:=2, Criteria1:=sRegion
'filter the store #'
dbRange.AutoFilter Field:=4, Criteria1:=sStore
'## Calculate the denominator ##'
'SUM OF VISIBLE CELLS IN wsDB.Column L
myDenominator = Application.WorksheetFunction.Sum(_
dbRange.Columns(12).SpecialCells(xlCellTypeVisible))
'## Now, filter for the type & group, to get the Numerator'
'Filter the type'
dbRange.AutoFilter Field:=10, Criteria1:=sType
'Filter the group(s) criteria
dbRange.AutoFilter Field:=11, Criteria1:=Array(Split(sGroup, ",")), _
Operator:=xlFilterValues
'## Calculate the numerator ##'
myNumerator = Application.WorksheetFunction.Sum(_
dbRange.Columns(12).SpecialCells(xlCellTypeVisible))
If myDenominator = 0 Then
'Avoid Div/0 errors:'
rngResults(r, c).Value = "N/A"
Else:
'## get the result and place it in the cell ##'
rngResults(r, c).Value = myNumerator/myDenominator
End If
'turn off the autofilter on fields 10 & 11'
dbRange.AutoFilter Field:=10
dbRange.AutoFilter Field:=11
Next r
'turn off the autofilter'
wsDB.AutoFilterMode = False
Next c
End Sub
주 :
Using the updated file
, 다음 코드를 정상적인 코드 모듈을 삽입하고 붙여 넣기 33)의 경우 해당 셀에서 쉼표로 구분 된 목록을 사용하십시오 (예 : 11, 22, 33
). "그룹"기준은 여러 기준을 수용 할 수있는 유일한 필드입니다. 이 업데이트 된 XLSM 파일에서 유효하지 않기 때문에
주 당신은, 열 G & H의 기준 필드를 업데이트해야합니다. 자신의 문제를 해결에서 시도를 보여줍니다하지
당신은 구글 문서 도구 또는 드롭 박스에 파일,하지만 당신도 적절하게 설명 할 수없는 매우 구체적인 질문의 종류를 업로드 할 수 있으며, 아니다 많은 도움이 될만한 답변을 얻을 수 있습니다. –
데이비드에게 감사드립니다. 이것은 내 처음으로 게시 하고이 환경에 처음이다. 데이터베이스, 결과 워크 시트 및 수동으로 수행하는 방법을 보여주는 파일을 첨부 할 수있는 가능한 최선을 다할 것입니다. 시각적으로 훨씬 쉽습니다. –
오늘 아주 많이 배웠고, Gmail 계정을 열고이 링크를 공유했습니다. 잘하면 작동합니다. https://docs.google.com/file/d/0ByCB_rXHlkEbS3U1SDFzRkRCd00/edit?usp=sharing –