首页 > 解决方案 > 如何在 x 列和 y 行上生成随机值范围,这些值总和为 EXCEL 2007 中行和列上的特定值

问题描述

我试图在 3 个单独的列和不断变化的行数中动态生成从 0 到 5 的随机值。每行的值总和必须为 5,并且列上的特定值。每列的总和是不同的。

我的工作表计算 3 列中每一列所需的值和所需的行数。

我对公式有平均技能,而对 VBA 没有,但有兴趣学习。

试图达到

标签: excelvbaexcel-2007

解决方案


解释

下面的代码使用在 B 列中找到的数字并计算一个随机数,循环直到找到 B 列中的最后一行。轴完全由宏格式化。该宏唯一需要运行的是 B 列中的值。该代码已被大量注释以供进一步解释。

有关如何在工作簿中包含宏的步骤

  • 第 1 步:打开 VBA(在 Excel 中的热键:alt-F11)
  • 第2步:

    • a) 如果您打开了多个工作表,请导航到正确的工作表。名称列在左侧菜单中。

      在此处输入图像描述

    • b) 因为此代码定义了工作表 ( Dim ws As Worksheet: Set ws = wb.Worksheets("Sheet1")),所以您可以将此代码复制/粘贴到将要运行的工作表中(在本例中为Sheet1Thisworkbook,或者您可以将其添加到模块中(Module1在图片)。记得给Sub比“测试”更好的名字:)

创建按钮的步骤

  • 第 1 步:如果您的 Excel 没有从标题栏创建按钮的选项,请按照以下步骤操作,否则跳到第 2 步。

    • a) 单击标题栏中的下拉菜单 在此处输入图像描述

    • b) 单击“更多命令...”

    • c)单击“从:中选择命令”并选择“开发人员选项卡”
    • d) 单击“设计模式”,然后单击添加>>。设计模式将让您在不激活宏的情况下单击按钮。此选项现在将显示在您的标题栏上。在此处输入图像描述
    • e) 单击“插入控件”,然后单击添加>>。插入控件将是创建按钮的选项。此选项现在将显示在您的标题栏上。在此处输入图像描述
  • 第2步:

    • a) 单击标题栏上的插入控件图标。
    • b) 单击“按钮(表单控件)”(左上选项)。

      在此处输入图像描述

    • c) 当您将鼠标悬停在工作簿中的单元格上时,您的鼠标光标现在将成为十字准线。单击并拖动以创建一个按钮。

    • d) 系统将自动提示您选择要分配给按钮的宏。选择您刚刚复制/粘贴的宏。

第 3 步:单击您的按钮/享受您的宏。


代码(已测试)

Sub test()
    ' dim your variables. this tells vba what type of variable it is working with
    Dim lRow As Long
    ' defining wb is easier than typing out ThisWorkbook everytime
    Dim wb As Workbook: Set wb = ThisWorkbook
    ' defining ws is easier than typing out Worksheets("Sheet1") everytime
    Dim ws As Worksheet: Set ws = wb.Worksheets("Sheet1")
    ' find the last row in column b (2) in the above defined ws
    lRow = ws.Cells(ws.Rows.Count, 2).End(xlUp).Row

    ' loop through rows 3 to last row
    For i = 3 To lRow
        ' generate a random number between 0 and the row contents of column B (5)
        ws.Cells(i, 3).Value = Int(Rnd() * (ws.Cells(i, 2).Value + 1))
        ' generate a random number between 0 and the difference between column B and colum C
        ws.Cells(i, 4).Value = Int(Rnd() * (ws.Cells(i, 2).Value - ws.Cells(i, 3).Value))
        ' subtract the difference between column B and the sum of column C and column D
        ws.Cells(i, 5).Value = ws.Cells(i, 2).Value - (ws.Cells(i, 3).Value + ws.Cells(i, 4).Value)
    Next i

    ' sum column C (column 3) and place the value in C2
    ws.Cells(2, 3).Value = Application.WorksheetFunction.Sum(Range(Cells(3, 3), Cells(lRow, 3)))
    ' sum column D (column 4) and place the value in D2
    ws.Cells(2, 4).Value = Application.WorksheetFunction.Sum(Range(Cells(3, 4), Cells(lRow, 4)))
    ' sum column E (column 5) and place the value in E2
    ws.Cells(2, 5).Value = Application.WorksheetFunction.Sum(Range(Cells(3, 5), Cells(lRow, 5)))

    ' format from A3 to the last row in column A - cell alignment / merge cells / value
    With ws.Range(Cells(3, 1), Cells(lRow, 1))
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlCenter
        .WrapText = False
        .Orientation = 0
        .AddIndent = False
        .IndentLevel = 0
        .ShrinkToFit = False
        .ReadingOrder = xlContext
        .MergeCells = True
        .Value = "row sum"
    End With

    ' format from C1 to E1 - cell alignment / merge cells / value
    With ws.Range(Cells(1, 3), Cells(1, 5))
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlBottom
        .WrapText = False
        .Orientation = 0
        .AddIndent = False
        .IndentLevel = 0
        .ShrinkToFit = False
        .ReadingOrder = xlContext
        .MergeCells = True
        .Value = "column sum"
    End With

    ' format from B3 to the last row in column B - color formatting
    With ws.Range(Cells(3, 2), Cells(lRow, 2)).Interior
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        .Color = 5296274
        .TintAndShade = 0
        .PatternTintAndShade = 0
    End With

    ' format from C2 to E2 - color formatting
    With ws.Range(Cells(2, 3), Cells(2, 5)).Interior
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        .Color = 5296274
        .TintAndShade = 0
        .PatternTintAndShade = 0
    End With
End Sub

推荐阅读