다음 내용이 포함된 스프레드시트가 있습니다.
Prod No Store 1 $ Store 1 Qty Store 2 Sale Store 2 Qty etc
A 4.00 1 7.50 2
B 0 0 15.00 1
C 4.00 2 -8 -1
D 5.00 1 5.00 1
각 부품 번호의 고유 가격을 모두 구한 다음 각 가격의 수량을 합산해야 합니다. 예:
Prod No A has 4.00 1 unit and 7.50 2 units
Prod No B has 0.00 0 units and 15.00 1 unit
Prod No C has 4.00 2 units and -8 -1 unit
Prod No D has 5.00 2 units
또한 고유한 가격과 각 가격의 총 수량 목록이 필요합니다.
예:
4.00 3 units
7.50 2 units
0.00 0 units
-8 -1 unit
15.00 1 unit
5.00 2 units
답변1
우선, 데이터를 좀 더 수평적인 형태로 저장한다면 장기적으로 더 많은 행운이 있을 것이라고 생각합니다.
Store Product Qty Sales
Store 1 A 1 4.00
Store 1 B 0 0.00
Store 1 C 2 4.00
Store 1 D 0 0.00
여러 열 쌍보다 단일 열을 조회하는 것이 훨씬 쉽습니다.
(크기와 규모에 따라 별도의 Store, Product 및 Sales 테이블이 있는 Access 데이터베이스가 그보다 더 나을 수도 있습니다)
즉, 현재 가지고 있는 것에 문제가 있고 시트에 VBA 매크로를 저장할 수 있는 경우 다음을 시도해 볼 수 있습니다.
다음을 포함하는 클래스 모듈을 VBA 프로젝트에 추가합니다
Tuple
.Private szKey As String Private nValue As Double Public Property Get Key() As String Key = szKey End Property Public Property Let Key(newKey As String) szKey = newKey End Property Public Property Get Value() As Double Value = nValue End Property Public Property Let Value(newValue As Double) nValue = newValue End Property
Module 1
예를 들어 프로젝트에 다음을 포함하는 일반 모듈을 추가합니다 .Public Function Summarize(ByRef rng As Range) As String If rng.Cells.Count Mod 2 = 1 Then Err.Raise 100, "", "Expected range of even cells" Dim coll As New Collection On Error Resume Next Dim flag As Boolean: flag = False Dim prevCel As Range, cel As Range: For Each cel In rng.Cells If flag Then Dim Key As String: Key = "" & prevCel.Value2 coll(Key).Value = coll(Key).Value + cel.Value2 If Err.Number <> 0 Then Err.Clear Dim t1 As New Tuple t1.Key = "" & prevCel.Value2 t1.Value = cel.Value2 coll.Add t1, Key Set t1 = Nothing End If End If Set prevCel = cel flag = Not flag Next cel On Error GoTo 0 Dim t2 As Variant: For Each t2 In coll If Len(Summarize) Then Summarize = Summarize & ", " Summarize = Summarize & Format(t2.Key, "#0.00") & " @ " & t2.Value Next t2 End Function
그런 다음 워크시트에 다음과 같은 수식을 입력할 수 있습니다.
="Product " & $A2 & " has " & Summarize($B2:$I2)
$B2:$I2 범위를 가능한 모든 Store 수를 포괄할 수 있을 만큼 넓은 범위로 대체해야 합니다. 또한 판매/수량 값이 쌍으로 되어 있으므로 동일한 크기의 범위를 사용해야 합니다. 그렇지 않으면 오류가 발생합니다
#VALUE
.