简体   繁体   English

Excel VBA访问工作表

[英]excel vba accessing worksheets

I have a user that is in need of accessing the raw data in a worksheet. 我有一个用户需要访问工作表中的原始数据。 We gather numbers throughout the month based on a estimate. 我们根据估算值收集整个月的数字。 At the end of the month the numbers need to be adjusted to the exact total amount. 月底需要将数字调整为确切的总数。 The user more-or-less randomly adjusts some or all of the items until the estimate entered = the exact amount. 用户或多或少地随机调整部分或全部项目,直到输入的估算值等于确切数量。 In other words, I can't just adjust every item by, say, 2% or $50. 换句话说,我不能仅将每件商品的价格调整2%或50美元。 The user manually goes thru the list of items and assigns adjustments. 用户手动浏览项目列表并进行调整。 My question is: can I send the user to the worksheet to make the adjustments and have the user return to the userform that called the worksheet? 我的问题是:我可以将用户发送到工作表进行调整,并让用户返回到称为工作表的用户窗体吗?

Say a month's end we have: 说一个月底,我们有:

在此处输入图片说明

So values were entered in A1 thru A31 因此在A1A31中输入了值
There is a SUM() formula 8n A32 有一个SUM()公式8n A32

To perform the adjustment, enter the actual month value in B32 and run this small macro: 要进行调整,请在B32中输入实际的月份值并运行以下小宏:

Sub dural()
    Dim v1 As Long, v2 As Long, delta As Long
    v1 = Range("A32").Value
    v2 = Range("B32").Value
    If v1 = v2 Then Exit Sub
    delta = v2 - v1

    If delta > 0 Then
        For i = 1 To delta
        j = Application.WorksheetFunction.RandBetween(1, 31)
        Cells(j, 1).Value = Cells(j, 1).Value + 1
        Next i
    Else
        For i = 1 To -delta
            j = Application.WorksheetFunction.RandBetween(1, 31)
            Cells(j, 1).Value = Cells(j, 1).Value - 1
        Next i
    End If
End Sub

Results in: 结果是:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM