簡體   English   中英

對另一個表中的 vba 腳本求和

[英]Sum on vba script from another table

我必須創建一個 vba 腳本,該腳本從 Excel 中另一個表的列中獲取數據( "Days Of Last Update" ,這是十進制),然后根據天數大於 2(在新表中顯示結果)進行求和柱子)。 看起來很簡單,但我是初學者,不知道如何進行。


更新:

大家好,感謝您的幫助。 現在我有一個新問題,仍然在這個項目中。 這是我所做的:

  • RawData 的工作表有一列名為"Days Since Last Update" ,它告訴我產品的服務請求何時更新。 所以,我用這個公式創建了一個新列=IF(N:N>2,1,0) ,告訴我自上次更新以來的天數是否高於 2。我刷新了我的數據透視表以獲得這個新列,做了一個數據總和,得到我以前想要的,但是,當工作表的更新功能運行時,數據透視表的新列以及帶有公式的 RawData 列都消失了。 在 Update 函數的代碼(不是我做的)中,有這樣的東西:

    工作表(“數據透視表”)。數據透視表(“數據透視表1”)。PivotCache.Refresh

    跟我的問題有關系嗎?

  1. 請找到下面附加的樣本數據表。 依靠另一個表的宏

  2. VBA 代碼附在下面。

     Sub Extract_Values() Dim wks As Worksheet Dim startRow As Integer Dim lastRow As Integer Dim vArray As Variant Dim vNewArray As Variant Dim i As Integer, j As Integer Dim Counter1 As Integer, Counter2 As Integer startRow = 2 Set wks = Sheets("Sheet1") With wks lastRow = .Cells(Rows.Count, 1).End(xlUp).Row vArray = .Range("A" & startRow & ":D" & lastRow).Value2 For i = 1 To UBound(vArray) If vArray(i, 4) = "Y" Then Counter1 = Counter1 + 1 End If Next i ReDim vNewArray(1 To Counter1, 1 To 2) For j = 1 To UBound(vArray) If vArray(j, 4) = "Y" Then Counter2 = Counter2 + 1 vNewArray(Counter2, 1) = vArray(j, 1) vNewArray(Counter2, 2) = vArray(j, 2) End If Next End With Range("E" & startRow & ":F" & startRow + Counter1 - 1) = vNewArray Range("E" & startRow & ":E" & startRow + Counter1 - 1).Select Selection.NumberFormat = "m/d/yyyy" Range("F" & startRow + Counter1).Select End Sub
  3. 我也是初學者在單元格 F8 中設置正確的 VBA 代碼,我已經計算了 Excel 內置的計數函數。

  4. D 列包含 IF 公式,如 'D2=IF(B2>1,"Y","N")' HTH

嘗試

Dim RowCount As Integer
Dim NewRow As Integer

RowCount = 2
NewRow = 2

Do Until RowCount > Cells(2, 2).End(xlDown).Row
    If Cells(RowCount, 2) > 1 Then
        Range(Cells(NewRow, 5), Cells(NewRow, 6)).Value = Range(Cells(RowCount, 1), Cells(RowCount, 2)).Value
        NewRow = NewRow + 1
    End If

    RowCount = RowCount + 1
Loop

結束子

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM