如何使用vba更新用户窗体中的Excel工作表的数据

2023-12-06

你想知道如何从 Excel 工作表中检索数据并在用户表单中更新它。

在图片上您可以看到用户表单的样子。 我想做的是创建另一个用户表单,可以在工作表中搜索特定引用并更新该特定行的一些单元格。

Userform This is the code I have now to insert data into the sheet.

Private Sub cmdClear_Click()
' Clear the form
 For Each ctl In Me.Controls
 If TypeName(ctl) = "TextBox" Or TypeName(ctl) = "ComboBox" Then
 ctl.Value = ""
 ElseIf TypeName(ctl) = "CheckBox" Then
 ctl.Value = False
 End If
 Next ctl
End Sub

Private Sub cmdSend_Click()
    Dim RowCount As Long
    Dim ctl As Control
' Check user input

    If Me.combTechnieker.Value = "" Then
        MsgBox "Dag vreemdeling! Welke van de 4 Mongolen ben je?", vbExclamation, "RMA invoer"
        Me.combTechnieker.SetFocus
        Exit Sub
    End If

    If Me.txtPcwRef.Value = "" Then
        MsgBox "Vul onze referentie in!", vbExclamation, "RMA invoer"
        Me.txtPcwRef.SetFocus
        Exit Sub
    End If

    If Me.txtKlant.Value = "" Then
        MsgBox "Vul de naam van de klant in!", vbExclamation, "RMA invoer"
        Me.txtKlant.SetFocus
        Exit Sub
    End If

    If Me.txtMerk.Value = "" Then
        MsgBox "Vul het merk in!", vbExclamation, "RMA invoer"
        Me.txtMerk.SetFocus
        Exit Sub
    End If

    If Me.txtMerkRef.Value = "" Then
        MsgBox "Vul de referentie van de fabrikant in!", vbExclamation, "RMA invoer"
        Me.txtMerkRef.SetFocus
        Exit Sub
    End If

    If Me.txtProduct.Value = "" Then
        MsgBox "Vul het product in!", vbExclamation, "RMA invoer"
        Me.txtProduct.SetFocus
        Exit Sub
    End If

    If Me.txtSerienummer.Value = "" Then
        MsgBox "Vul het serienummer in!", vbExclamation, "RMA invoer"
        Me.txtSerienummer.SetFocus
        Exit Sub
    End If

    If Me.txtProbleem.Value = "" Then
        MsgBox "Vul de probleem omschrijving in!", vbExclamation, "RMA invoer"
        Me.txtProbleem.SetFocus
        Exit Sub
    End If

    If Me.txtOnderdelen.Value = "" Then
        MsgBox "Bent u zeker dat er geen onderdelen achterblijven. Indien ja. Vul N/A in", vbExclamation, "RMA invoer"
        Me.txtOnderdelen.SetFocus
        Exit Sub
    End If

' Write data to worksheet
    RowCount = Worksheets("RMA 2016").Range("A1").CurrentRegion.Rows.Count

    With Worksheets("RMA 2016").Range("A1")
        .Offset(RowCount, 0).Value = Format(Now, "dd/mm/yyyy hh:nn:ss")
        .Offset(RowCount, 1).Value = "Open"
        .Offset(RowCount, 3).Value = Me.txtPcwRef.Value
        .Offset(RowCount, 4).Value = Me.txtKlant.Value
        .Offset(RowCount, 5).Value = Me.txtMerk.Value
        .Offset(RowCount, 6).Value = Me.txtMerkRef.Value
        .Offset(RowCount, 7).Value = Me.txtProduct.Value
        .Offset(RowCount, 8).Value = Me.txtSerienummer.Value
        .Offset(RowCount, 9).Value = Me.txtOnderdelen.Value
        .Offset(RowCount, 10).Value = Me.txtProbleem.Value
        .Offset(RowCount, 13).Value = Me.combTechnieker.Value

    If Me.chkGarantie.Value = True Then
     .Offset(RowCount, 2).Value = "Ja"
     Else
    .Offset(RowCount, 2).Value = "Nee"
    End If
    End With
    ' Clear the form
     For Each ctl In Me.Controls
     If TypeName(ctl) = "TextBox" Or TypeName(ctl) = "ComboBox" Then
     ctl.Value = ""
    ElseIf TypeName(ctl) = "CheckBox" Then
    ctl.Value = False
    End If
    Next ctl
End Sub

Private Sub UserForm_Click()

End Sub

我创建了一个小示例来展示加载、保存和删除记录的一般机制如何与表单配合使用。当您尝试使用不存在的 ID 保存记录时,它会将新记录追加到表中。这应该非常接近您所要求的内容,并向您展示如何在用户表单和工作表之间调整数据。

Sample table and form

Private Sub cmdLoad_Click()

    ' check if provided product ID is not empty
    If Len(Trim(Me.txtId)) = 0 Then
        MsgBox "Enter product ID to load the record."
        Exit Sub
    End If

    ' try to retrieve the product by ID
    Dim rngIdList As Range, rngId As Range
    Set rngIdList = ActiveSheet.Range([a2], [a2].End(xlDown))

    Set rngId = rngIdList.Find(Me.txtId, LookIn:=xlValues)
    If rngId Is Nothing Then
        ' product ID is not found
        MsgBox "Product ID " & Me.txtId & " doesn't exist."
        Exit Sub
    Else
        ' product ID is found -- fill out the form
        Me.txtId = rngId.Offset(0, 0)
        Me.txtName = rngId.Offset(0, 1)
        Me.txtNote = rngId.Offset(0, 2)
    End If

End Sub

Private Sub cmdSave_Click()

    ' check if provided product ID is not empty
    If Len(Trim(Me.txtId)) = 0 Then
        MsgBox "Enter product ID to load the record."
        Exit Sub
    End If

    ' try to retrieve the product by ID
    Dim rngIdList As Range, rngId As Range
    Set rngIdList = ActiveSheet.Range([a2], [a2].End(xlDown))

    Set rngId = rngIdList.Find(Me.txtId, LookIn:=xlValues)
    If rngId Is Nothing Then
        ' if product ID is not found, append new one to the end of the table
        With rngIdList
            Set rngId = .Offset(.Rows.Count, 0).Resize(1, 1)
        End With
    End If

    ' update excel record
    rngId.Offset(0, 0) = Me.txtId
    rngId.Offset(0, 1) = Me.txtName
    rngId.Offset(0, 2) = Me.txtNote

End Sub

Private Sub cmdDelete_Click()

    ' check if provided product ID is not empty
    If Len(Trim(Me.txtId)) = 0 Then
        MsgBox "Enter product ID to delete the record."
        Exit Sub
    End If

    ' try to retrieve the product by ID
    Dim rngIdList As Range, rngId As Range
    Set rngIdList = ActiveSheet.Range([a2], [a2].End(xlDown))

    Set rngId = rngIdList.Find(Me.txtId, LookIn:=xlValues)
    If rngId Is Nothing Then
        ' product ID is not found -- nothing to delete
        MsgBox "Product ID " & Me.txtId & " doesn't exist."
        Exit Sub
    Else
        ' product ID is found -- delete the entire line
        rngId.EntireRow.Delete
    End If

End Sub
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何使用vba更新用户窗体中的Excel工作表的数据 的相关文章

随机推荐

  • 如何重新标记 matplotlib 热图的轴刻度

    我正在关注这个例子生成热图 是否可以重新标记 X 轴上的值 并向其添加一个常量 例如 我希望 X 轴上的数字不是 0 1 2 3 4 而是 5 6 7 8 9 您可以使用关键字参数来标记 x 轴和 y 轴extent在致电imshow 这是
  • 如何从Python列表中生成所有可能的排列对? [复制]

    这个问题在这里已经有答案了 如何从 Python 列表中生成所有可能的对排列 Example input 3 8 2 output 3 8 3 2 8 3 8 2 2 3 2 8 您可以使用itertools permutations im
  • 带有 CTE 的 BigQuery UPDATE 语句

    正在寻求有关 BigQuery 的帮助 我似乎无法在 UPDATE 语句中使用 CTE 即 with ctename as select column1 column2 column3 from blah update table2 set
  • 如何禁用引导列中的垂直滚动

    我有两个引导列 左 和 右 我想用屏幕修复左栏并禁用滚动 但我想在右列中启用滚动 这将有更多内容 基本上我想显示帖子 使帖子标题显示在左列中 帖子内容显示在右列中 完全相同this 注 我用过overflow y hidden 但没有成功
  • 在主题中使用 fontFamily 属性时,Toast 会抛出 ArrayIndexOutOfBoundsException 和 appcompat v26

    每当我展示一个Toast 应用程序崩溃 如果我使用旧版本的 AppCompat 库或删除该应用程序 则该应用程序可以正常工作fontFamily从风格上 创建时 Override protected void onCreate Bundle
  • 在 Dataobject 中提交表单 - Silverstripe 3.1

    我正在使用 Aram 的 DataobjectAsPage 模块 现在我想在每个 DOaP 站点上都有一个表单 我在我的数据对象中创建了这样的表单 public function RegistrationForm fields new Fi
  • 获得mysql中每个人第二高的工资

    我们有如下表 person id salary 1 1500 1 1000 1 500 2 2000 2 1000 3 3000 3 2000 4 3000 4 1000 我们希望每个人的薪水都是第二高的 按每个人分组并获得第二高的工资 像
  • 不同实现之间的随机输出不同

    我已经用 libstdc libc 和 dinkumware 尝试过这个程序 include
  • 重新排列 SimpleCursorAdapter 中的列表视图项目

    我有一个数据库 SQLite 然后使用 SimpleCursorAdapter 将值显示到列表视图中 但是 我想重新排列列表视图项目并显示以 ID 1 开头的项目 然后显示 ID 号 2 的项目 依此类推 问题是 我无法理解基于重新分配列表
  • 如何将xpath的值放入html img标签中

    以下代码为我提供了适当的字符串 但我不知道如何将该值放入 html img 标签中
  • 将 Apache Cassandra 与 Apache Ignite 集成

    我正在尝试将 Apache Ignite 与 Apache Cassandra 3 11 2 集成 因为我想使用 Ignite 缓存现有 Cassandra 数据库中存在的数据 在浏览了在线资源之后 到目前为止我已经完成了以下工作 已下载阿
  • 仅指定操作时如何将一个控制器设置为默认控制器?

    我正在使用 MVC 2013 中的标准 MVC 模板 有一个Home具有 关于 联系 等操作的控制器 有一个Account具有登录 注销等操作的控制器 该应用程序部署在域中website 网址http 网站将产生 Home Index 的输
  • 在javascript中将十六进制颜色转换为整数

    我正在尝试将十六进制颜色字符串转换为 JavaScript 中的 int color int 必须与 VB6 的格式相同 我认为字节顺序不正确 例如 255 是红色 ff0000 16776960 是水色 00ffff 我有一个函数可以做相
  • C:传递(双重)赋值

    我在 C 中使用过这样的构造 list gt head list gt tail NULL 现在我考虑这是否真的如我所想 这是什么意思吗 list gt head NULL list gt tail NULL or list gt head
  • 以 Stellar JS 为中心的 CSS 背景图像

    我正在使用 Stellar JS 将背景图像设置为元素 并尝试实现以视差移动的居中图像 然而 当我尝试使用标准方法时 我意识到背景位置是由恒星更新的 然后恒星会覆盖居中 我假设这就是覆盖它的原因 我创建了一个小提琴来解释这种情况 http
  • 仅允许某些字符

    我想阻止输入除 0 9 a z A Z 范围内的字母数字字符之外的所有字符 所以当有人打字时 例如 没有任何内容写入输入 我怎样才能做到这一点 您需要编写一个函数来监听onkeypress事件 然后检查表单是否包含任何不需要的字符 如果包含
  • Ninject - 基于子域动态指定连接字符串

    我正在尝试使用 ninject 根据 url 动态指定连接字符串 我正在使用 ninject mvc nuget 包 该包使用 webActivator 我的代码如下 我的注射 kernel Bind
  • 检查选择是否包含链接

    我正在创建一个富文本编辑器 我想使用相同的按钮来链接和取消链接选择 document execCommand createLink and document execCommand unlink 允许用户链接和取消链接window getS
  • CentOS *** 错误:Phusion Passenger 似乎没有运行

    当我通过 capistrano 将 Rails 应用程序部署到我的 CentOS 服务器时 我一直收到此错误 错误 Phusion Passenger 似乎没有运行 如果您确定它正在运行 那么导致此问题的原因可能是 的 您使用 Apache
  • 如何使用vba更新用户窗体中的Excel工作表的数据

    你想知道如何从 Excel 工作表中检索数据并在用户表单中更新它 在图片上您可以看到用户表单的样子 我想做的是创建另一个用户表单 可以在工作表中搜索特定引用并更新该特定行的一些单元格 This is the code I have now