【问题标题】:VBA generated output: only data that satisfies certain criteria in a new sheetVBA 生成的输出:仅在新工作表中满足特定条件的数据
【发布时间】:2012-02-13 21:00:33
【问题描述】:

我正在构建一个当前看起来像这样的模型 - 输入表 --> 基本 excel 函数 --> 输出表。基本的东西对吧?!

我想通过添加 VBA 生成的输出(新工作表)使模型更加动态,仅根据以下内容提取满足特定条件的数据:

Sub GenerateTargets()
'Dim UpperRange As Double
'Dim LowerRange As Double
'Dim Percentile As Double
'Dim Test
'Dim Total Revenue {Note: [This is a header in the output sheet] [should I use Array or Range? If array is it Stat or Dynamic] I might change the input later on and increase the number of observations}


'Read Values in Cells
UpperRange = Worksheets("sheet2").Cells(x, y).Value
LowerRange = Worksheets("sheet2").Cells(x, y).Value

Test = UpperRange > Percentile > LowerRange

Case Test Is True
    [This is the test I want to generate]

Case Test Is False
    [I do not want this to show in my new sheet]

[Here I would like to add another Case to stop counting if [Total revenue = ##]

End Sub

如何要求它在输出中生成相同的标头/数据但消除错误情况,同时在达到某个阈值时停止计数。

不胜感激任何帮助/建议 --

【问题讨论】:

  • 请提供有关输入和输出表的更多信息(提供和示例)。

标签: excel vba


【解决方案1】:

如果我理解正确,请查看此代码。您不需要 Select Case,因为您只想在“TEST”为 TRUE 时执行代码。您可以在 FOR 循环中检查“REVENUE”条件。您可以使用动态数组来存储您的值,然后最后写入 OUTPUT 表。

Sub GenerateTargets()
    Dim UpperRange As Double, LowerRange As Double, Percentile As Double
    Dim Rev As Double
    Dim Test As Boolean
    Dim Output() As String

    Rev = Somevalue '<~~ Revenue

    '~~> Read Values in Cells
    UpperRange = Worksheets("sheet2").Cells(x, y).Value
    LowerRange = Worksheets("sheet2").Cells(x, y).Value

    '~~> Test Condition
    'Test = UpperRange > Percentile > LowerRange

    If Test = True Then
        '~~> Use For Loop here to store values in a dynamic array
        '~~> Use Redim Preserve to store new values

        '~~> Create a condition for revenue and exit FOR loop if
        '~~> the condition is met
        ' If Rev = 0 Then Exit For

        '~~> Store results in the output worksheet if the array is not empty
    End If
End Sub

跟进

这是你想要做的吗? (在没有示例文件的情况下,代码未经测试)。我已经对代码进行了注释,以便您在理解代码时不会有任何问题。

Option Explicit

Sub GenerateTargets()
    Dim ws1 As Worksheet, ws2 As Worksheet
    Dim UpperRange As Double, LowerRange As Double, Percentile As Double
    Dim lastRowWs1 As Long, lastRowWs2 As Long, i as Long

    '~~> Sheet where data needs to be copied
    Set ws1 = Sheets("Sheet1")
    lastRowWs1 = ws1.Range("A" & ws1.Rows.Count).End(xlUp).Row + 1

    '~~> Sheet where the data needs to be compared
    Set ws2 = Sheets("Sheet2")
    '~~> Read Values in Cells
    UpperRange = ws2.Range("L2").Value
    LowerRange = ws2.Range("L3").Value
    lastRowWs2 = ws2.Range("A" & ws2.Rows.Count).End(xlUp).Row

    '~~> Starting from the 3rd row
    For i = 3 To lastRowWs2
        '~~> Get Percentile Value for comparision
        Percentile = ws2.Range("B" & i).Value

        '~~> Test Condition and proceed if true
        If UpperRange > Percentile And LowerRange < Percentile Then
            '~~> Copy range A to I from Sheet2 and paste it in Sheet1
            ws2.Range("A" & i & ":I" & i).Copy _
            ws1.Range("A" & lastRowWs1)
            lastRowWs1 = lastRowWs1 + 1
        End If
    Next i
    Application.CutCopyMode = False
End Sub

【讨论】:

  • 谢谢,循环的语法是什么?我怎样才能让它存储整个数组但只测试一列。另外,我如何将结果输入到新工作表中?顺便说一句,我决定不添加 rev 测试。
  • @user1207643:哇这么多问题:)。您可以上传一些关于您的输入和输出表的快照吗?这样,我可以在回答您的问题时得到正确的参考资料。
  • 当然!抱歉,如果我的问题太多了 - 我想输出相同的数据,但基于新工作表中的测试和转速计数!
  • 非常感谢!这是完美的!
猜你喜欢
  • 2012-02-17
  • 1970-01-01
  • 2011-12-28
  • 1970-01-01
  • 2011-07-12
  • 1970-01-01
  • 2015-05-14
  • 2013-02-21
  • 2018-04-12
相关资源
最近更新 更多