簡體   English   中英

基於變量設置范圍值的宏

[英]Macro to Set Range value based on a variable

您好,我是 VBA 新手,我正在嘗試編寫 QA 報告(呼叫審核),該報告將根據另一個工作表的數據自動計算值。 我有 2 個工作表:包含數據的元素和顯示滿足特定條件的單元格數量的 AutoMacro。

Elements 表具有名為 Element 和 Rate 以及 WeekNumber 的列。 我制作了一個代碼,將所有不同的周數列出到 Automacro Sheet 的“AM”列中。 現在,我想計算特定周(AutoMacro 中的 AM 列)在 Compassion(在 Elements 表的 Element 列中)中有多少呼叫獲得了“Developing”率(在 Elements 表的 Rate 列中)床單)

這段代碼工作正常:

CompDevCount = Application.WorksheetFunction.CountIfs( _
    Sheets("Elements").Range("f2:F1048576"), "Compassion", _
    Sheets("Elements").Range("G2:G1048576"),"Developing", _
    Sheets("Elements").Range("C2:C1048576"), "4") 

但是,不是在最后一個標准中放置一個固定值,即周數,我希望我的代碼獲取列 AM 中相應單元格的值(如果宏正在計算 AutoMacro 表中第 2 行的值,它應該更改“ 4" 到 AM2 的任何值),我不知道該怎么做。

任何幫助是極大的贊賞

這是我的示例文件的鏈接: https : //drive.google.com/file/d/1ZXuMsQv_mzvycPXnGCGY7bUL6-gWBrBr/view?usp=sharing

試試下面的代碼:

Sub LetsCount()

Dim lastElementsRowIndex As Long
Dim wsElements As Worksheet
Dim wsAutoMacro As Worksheet
Dim elementRange As Range
Dim rateRange As Range
Dim weekRange As Range
Dim i As Long
Dim weekNumber As Integer

Set wsElements = Sheets("Elements")
Set wsAutoMacro = Sheets("AutoMacro")

'//Get the index of the last filled row based on column A (Analyst)
lastElementsRowIndex = wsElements.Cells(Rows.Count, "A").End(xlUp).Row

'//Get ranges
Set elementRange = wsElements.Range("F2:F" & lastElementsRowIndex)
Set rateRange = wsElements.Range("G2:G" & lastElementsRowIndex)
Set weekRange = wsElements.Range("C2:C" & lastElementsRowIndex)

'//Looping through all filled rows in the AutoMacro sheet
For i = 2 To wsAutoMacro.Cells(Rows.Count, "A").End(xlUp).Row

    '//Get week number from cell in column "AM"
    weekNumber = wsAutoMacro.Cells(i, "AM").Value

    '//Get the number of calls with: Rate = 'Developing', Element = 'Compassion' and Week Number = weekNumber
    CompDevCount = Application.WorksheetFunction.CountIfs( _
    elementRange, "Compassion", _
    rateRange, "Developing", _
    weekRange, weekNumber)

    '//Insert the value in the corresponding cell of the AN column (column that I adopted to put the value)
    wsAutoMacro.Cells(i, "AN").Value = CompDevCount
Next

End Sub

暫無
暫無

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

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