Microsoft Excel:VBA中的简单GUI [英] Microsoft excel: Simple gui in VBA

查看:178
本文介绍了Microsoft Excel:VBA中的简单GUI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始学习VBA,并且现在正在编写第一个GUI代码.因此,我应该创建一个用户表单,用户可以在其中填写许多不同的数据来比较其退休选择.我的工作簿中有表格,我可以从中生成值.但是,我不知道如何引用现有的工作表,以便新用户在用户窗体中输入的随机数据将进行正确的计算并为用户提供一个比较表.

I just started to learn VBA and I am writing first GUI code right now. So I am supposed to create a userform where user can fill in many different data to compare their retirement options. I have the sheets in my workbook from which I can generate the values. However, I have no idea how to refer to the existing worksheets so that the random data entered by new user in userform will do the correct calculation and gives user a comparison sheet.

任何帮助都会很好!

    Sub cancelCommand_Click()
         Unload Me
        End Sub
     Sub previousCmd_Click()
      MultiPage1.Value = MultiPage1.Value - 1
     UpdateButtons
      End Sub
     Sub nextCmd_Click()
      MultiPage1.Value = MultiPage1.Value + 1
      UpdateButtons
     End Sub
     Sub finishCmd_Click()
     Worksheets("Conclusion").Activate
     Unload Me
    End Sub

推荐答案

您可以通过在命令"按钮的代码内调用 ThisWorkbook 对象来与工作簿中的数据进行交互.

You interact with data in workbooks by calling the ThisWorkbook object within the code of a Command button.

要以最简单的形式对其进行测试,请执行以下操作:

To test it out at it's very simplest form do the following:

创建一个新的工作簿

创建新的用户表单

添加文本框

添加按钮

双击该按钮,您将自动进入一个名为"Private Sub CommandButton1_Click()."的编码如下.

Double click on the button and you will automatically be taken to something called "Private Sub CommandButton1_Click(). Code it as follows.

Private Sub CommandButton1_Click()

ThisWorkbook.Application.ActiveCell.Value = TextBox1.Value
Unload Me

End Sub

移动到工作表上的任何单元格,然后运行用户表单.在文本框中输入一些文本,然后按一下按钮.无论您在文本框中输入的内容如何,​​都将显示在您所在的单元格中.

Move to any cell on your sheet and then run the user form. Type some text in to the text box and press your button. Whatever you typed in to the text box will be shown in the cell that you were just in.

您可以尝试使用另一种替代形式代替 ThisWorkbook.Application.ActiveCell.Value = TextBox1.Value 行:

Another variations that you may try instead of the ThisWorkbook.Application.ActiveCell.Value = TextBox1.Value line may also be:

ThisWorkbook.Sheets(1).Range("A2").Value ="TextBox1.Value

上面的代码将使Sheet1!A2 =每次在文本框中键入的内容.底线是您通过范围与Excel单元格进行通信.请参见范围对象(Excel)适用于Range可以使用的所有不同方法和属性.

The code above will Make Sheet1!A2 = Whatever was typed in to the text box every time. Bottom line you communicate with Excel cells via Ranges. See Range Object (Excel) for all of the different methods and properties that you can use with a Range.

这篇关于Microsoft Excel:VBA中的简单GUI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆