vba 如何从一个工作表提交数据以更新单独的工作表?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15705075/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 20:20:05  来源:igfitidea点击:

How do you submit data from one worksheet to update a separate worksheet?

excelvbacontrolsstock

提问by Toby Lufc York

I am trying to develop a stock control system (on Microsoft Excel 2010) but I have encountered a problem, hopefully someone out there can help me with it!

我正在尝试开发一个库存控制系统(在 Microsoft Excel 2010 上),但我遇到了一个问题,希望有人可以帮助我!

I have a parts incoming worksheet and separate worksheets for each storage location. (5 of them)

我有一个零件传入工作表和每个存储位置的单独工作表。(其中 5 个)

On the parts incoming page you enter the parts name, quantity, a checkbox to say whether the invoice for the part has been added to the accountants file and a choice of where the part will be stored (i.e. the storage location) as a drop down selectable list.

在零件进货页面上,您输入零件名称、数量、一个复选框以说明零件的发票是否已添加到会计文件中,并在下拉菜单中选择零件的存储位置(即存储位置)可选列表。

I want to create a submit button on the data entry sheet (parts incoming sheet) that will update the RELEVANT worksheet (the storage location selected by the user) and add the details submitted, to that worksheet. I know how to add the details (i think) but my question is how can you make it add the details to the selected storage location/worksheet?

我想在数据输入表(零件传入表)上创建一个提交按钮,该按钮将更新相关工作表(用户选择的存储位置)并将提交的详细信息添加到该工作表。我知道如何添加详细信息(我认为),但我的问题是如何将详细信息添加到选定的存储位置/工作表?

So far I have only really used macros and have a fairly limited knowledge on VBA but I am willing to try anything that you may think will let it work. I hope that makes sense. Any help would be greatly appreciated! Thanks Toby

到目前为止,我只真正使用过宏并且对 VBA 的了解相当有限,但我愿意尝试任何您认为可以让它工作的东西。我希望这是有道理的。任何帮助将不胜感激!谢谢托比

回答by David Zemens

This should get you started, some simple examples of transferring cell values or ranges of cells from one sheet to another.

这应该让您开始,一些将单元格值或单元格范围从一张纸传输到另一张纸的简单示例。

Create a commandButton on the worksheet, and attach this macro to it:

在工作表上创建一个 commandButton,并将此宏附加到它:

Sub TransferData() 
    Dim sht1 as Worksheet
    Dim sht2 as Worksheet

    Set sht1 = ThisWorkbook.Sheets("Parts Incoming")  '<-- modify as needed'
    Set sht2 = ThisWOrkbook.Sheets("Storage Location") '<-- modify as needed'

    'Example of placing cell value from B16 on Parts Incoming in the cell A1 on the storage location sheet.'
    sht2.Range("A1") = sht1.Range("B16") 

    'Example of transfering data from a Range of cells to another range of cells on a different worksheet:'
    sht2.Range("A1:A50") = sht1.Range("E50:E100")


    ' you will need to modify or add to this to transfer data from different places '

End Sub