使用 VBA Userform 使用 excel 创建“数据库”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19938726/
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-12 00:24:03  来源:igfitidea点击:

Creating a "database" with excel with VBA Userform

excelvba

提问by Stephenloky

Private Sub CommandButton1_Click()

Dim actual_sheet As String

actual_sheet = "Sheet1"

Dim data_base As Variant

data_base = Sheets(actual_sheet).Range("G4:K100000")

nome = TextBox1.Value
Age = TextBox2.Value
Adress = TextBox3.Value
Phone = TextBox4.Value

For i = 1 To UBound(data_base)

    Line_at = data_base(i, 1)

    If Line_at = "" Then

        ' id
        data_base(i, 1) = i
        'name
        data_base(i, 2) = nome
        'age
        data_base(i, 3) = Age
        'adress
        data_base(i, 4) = Adress
        'phone
        data_base(i, 5) = Phone

        Exit For

    End If

Next


Sheets(actual_sheet).Range("G4:K100000") = database

UserNew.Hide



End Sub

I have a form for a user called UserNewand I want some information to a 'database' *just a table for exercise * , but when i click the CommandButton_1 nothing happens to my "data_base", i debugged and i saw that the values are actually getting passed, but i think the problem it's with the writting part ... I don't know is not working .... Any help is appreciated.
Actually i can pass the values to a sub(name,age,address,phone) , but there's a way to this automatically like this example ?

我有一个名为用户的表单UserNew,我想要一些信息到“数据库”*只是一个练习表*,但是当我单击 CommandButton_1 时,我的“data_base”没有任何反应,我进行了调试,我看到这些值实际上是获得通过,但我认为问题在于写作部分......我不知道它不起作用......任何帮助表示赞赏。
实际上我可以将值传递给 sub(name,age,address,phone) ,但是有一种方法可以像这个示例一样自动执行此操作吗?

采纳答案by Dick Kusleika

You've dim'd data_basebut where you assign back to a range, you use database. Always, always put Option Explicitat the top of your module and declare all variables.

你已经变暗了,data_base但是在你分配回一个范围的地方,你使用database. 始终,始终放在Option Explicit模块的顶部并声明所有变量。