여러분의 코드를 정리하세요. 많은 통곡과이를 갈고 난 후에, 문제는 내가 .Sum 논쟁에 넣은 여분의 괄호로 밝혀졌습니다. @BigBen과 @JvdV에 감사드립니다.
문제 : Worksheetfunction.Sum은 동적 범위에 대해 0의 합계를 반환하지만 rows.count> 1 에 대해서만 통화 형식의 참조 데이터에 대해서만 반환 됩니다.
추가 세부 정보 : 참조 통합 문서를 스크랩하고 사용자 입력에 따라 4 개의 서로 다른 텍스트 상자에 서로 다른 번호를 반환하도록 설정된 사용자 양식이 있습니다. 경우에 따라 숫자는 참조 통합 문서에있는 여러 행의 합계 여야합니다. 아래 코드를 사용하면 행 수가 1 인 한 모든 반환 텍스트 상자에 대해 꿈처럼 작동하지만 다른 항목에 대해서는 0 (또는 $ 0.00)을 반환합니다. 그러나 정수인 하나의 합계에 대해 모든 상황에서 잘 작동합니다. 나머지는 통화 형식입니다.
내가 한 일 : MsgBoxes를 사용하여 동적 범위가 올바른 주소, 즉 합계를 원하는 모든 셀을 반환하고 해당 주소의 숫자가 텍스트가 아니라 실제로 숫자인지 확인했습니다 (IsNumber에 대한 True 반환으로 확인 됨). . .Subtotal 및 .Aggregate를 사용하여 도움이 될 수 있는지 확인했지만 누락 된 개체 및 기타 오류가 발생하여 VBA를 처음 사용했기 때문에 엉뚱한 소리를 냈습니다.
코드:
내 기본 논리는 다음과 같습니다. textbox.value에 대한 모든 참조 (csrWorkbook) 시트에서 검색합니다. 발견되면 병합 된 영역의 높이를 측정합니다 (내가 알고 있지만 병합 결정은 내 급여보다 높음). 4 개의 다른 관련 수량을 찾으려면 오른쪽으로 오프셋하십시오. 여러 행이있는 경우이 수량을 합산하십시오. 합계를 4 개의 다른 텍스트 상자로 반환합니다.
도움!
Private Sub ScrapeButton_Click()
'Enter search term into first TB
'click search button
'result DOES(!!) appear in second TB
'Variables
Dim csrWorkbook As Workbook
Dim refWorkbook As Workbook
Dim refVariables As Worksheet
Dim csrFilePath As String
Dim csrFileName As String
Dim slinAddress As String
Dim ws As Worksheet
Dim sheetCount As Long
Dim rowCount As Long
Dim slinCell As Excel.Range
Dim quantCells As Excel.Range
Dim costCells As Excel.Range
Dim feeCells As Excel.Range
Dim totalCells As Excel.Range
Dim i As Integer
Dim iCost As Double
Dim iFee As Double
Dim iTotal As Double
Set refWorkbook = Workbooks("AutomationBackbone.xlsm")
csrFileName = refWorkbook.Sheets("Variable Storage").Range("A2").Value
Set csrWorkbook = Workbooks(csrFileName)
sheetCount = csrWorkbook.Sheets.Count
'search all worksheets for data in a known column
For i = 1 To sheetCount
Set slinCell = csrWorkbook.Sheets(i).Range("C1:C100").find(Me.TextBox1.Value)
If Not slinCell Is Nothing Then
'Find sums and populate
rowCount = slinCell.MergeArea.Rows.Count 'count the number of rows in merged area
Set quantCells = slinCell.Offset(0, 2).Resize(rowCount, 1) 'establish a new range of that same height
Set costCells = quantCells.Offset(0, 6)
Set feeCells = quantCells.Offset(0, 7)
Set totalCells = quantCells.Offset(0, 8)
Me.iQuantityTB.Value = Application.WorksheetFunction.Sum((quantCells)) 'populate the Initial Quantity
iCost = Application.WorksheetFunction.Sum((costCells)) 'find sum of Cost range
iFee = Application.WorksheetFunction.Sum((feeCells)) 'find sum of Fee range
iTotal = Application.WorksheetFunction.Sum((totalCells)) 'find sum of Total range
Me.iCostTB.Value = iCost 'populate textboxes
Me.iFeeTB.Value = iFee
Me.iTotalTB.Value = iTotal
'original code commented out to see if being more explicit helped.
'Narrator: it didn't
'Me.iCostTB.Value = Application.WorksheetFunction.Sum((quantCells.Offset(0, 6))) 'populate the Initial Cost
'Me.iFeeTB.Value = Application.WorksheetFunction.Sum((quantCells.Offset(0, 7))) 'populate the Initial Fee
'Me.iTotalTB.Value = Application.WorksheetFunction.Sum((quantCells.Offset(0, 8))) 'populate the Initial Total
Exit Sub
End If
Next i
End Sub
편집 : 명확성을 위해 사진이 추가되었습니다.
괄호가 다시 표시됩니다!
여분의 괄호를 포함하면 내부 표현식이 평가 되고 결과가 전달됩니다 Sum
.
그래서
Application.WorksheetFunction.Sum((costCells))
다음과 같다
Application.WorksheetFunction.Sum(costCells.Value)
기본 데이터가 인 경우 0을 반환합니다 Currency
.
Sum
여기 에서 s 동작에 대한 재현 가능한 작은 예로서 (내가 예상 한 것과는 다릅니다) :
Dim x(0 To 1) As Currency
x(0) = 1
x(1) = 2
Debug.Print Application.Sum(x) '<~ returns zero.
참고 .Value2
않습니다 하지 사용 Currency
데이터 유형 및 라인
Application.WorksheetFunction.Sum(costCells.Value2)
기본 값에 관계없이 올바른 결과를 반환합니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다