繁体   English   中英

用于 Userform1 上复选框的 Excel 2016 VBA 代码,用于指定要传输到另一个工作表(同一工作簿)上的数据

[英]Excel 2016 VBA Code for Checkbox on Userform1 to specify data to be transferred onto another sheet (same workbook)

工作簿

利润损失

用户表单

新的用户表单选项

用户@Lambik 帮助我实现了本工作簿中的功能。 成功获取了userform1到2019年1月表中的数据,同时转入到了盈亏表中。 没想到的是,盈亏中所有字段都不能复制粘贴。

我尝试在用户表单上添加新标签和新文本框,并通过研究@Lambik 提供的代码使其工作,但还有另一个错误,而不是更像是重复的错误,如果我保留 userform1 的编辑版本(我'顺便说一下,我删除了它非常不切实际)那么即使在 userform1 上“未”填充了额外的字段,其余条目仍然会进入利润损失,这意味着手动删除每个不应该在那里的条目。 所以我想了很久,唯一能想到的就是在 userform1 上添加一个复选框,当复选框被点击时,userform1 中的前两个字段是评论和租金,以及从中选择的相应月份进入利润损失组合框1。 我认为,我认为这是正确的逻辑。 如果有人可以帮助使用 VBA 代码来说明如何使用复选框来实现这一点,那么我将不胜感激。 我已经附加了工作簿截图的链接,利润损失的列行,旧的 userform1(就像一个魅力,感谢@Lambik)和新编辑的 userform1,带有额外的标签和复选框,可以将数据复制到损益表上工作表以及所选的当前月份。

使 userform1 工作的代码是,

Private Sub ComboBox1_Change()

Dim SheetName As String
Dim ws As Worksheet
Dim LastRow As Long

SheetName = ComboBox1.Value
Set ws = Sheets(SheetName)

 LastRow = ws.Cells(ws.Rows.Count, "G").End(xlUp).Row
 Label8.Caption = "         Balance is: " & ws.Cells(LastRow, 7).Value


Private Sub CommandButton1_Click()

Dim dcc As Long
Dim abc As Worksheet, pfl As Worksheet

 Set abc = ThisWorkbook.Worksheets(Me.ComboBox1.Value)
 Set pfl = Sheets("ProfitLoss")

With abc

dcc = .Range("A" & Rows.Count).End(xlUp).Row

.Cells(dcc + 1, 1).Value = Date
.Cells(dcc + 1, 2).Value = Me.TextBox1.Value (comment/source)
.Cells(dcc + 1, 3).Value = Me.TextBox2.Value (rent)
.Cells(dcc + 1, 4).Value = Me.TextBox3.Value
.Cells(dcc + 1, 5).Value = Me.TextBox4.Value
.Cells(dcc + 1, 6).Value = Me.TextBox5.Value

End With

With pfl
dcc = .Range("A" & Rows.Count).End(xlUp).Row

.Cells(dcc + 1, 1).Value = Date
.Cells(dcc + 1, 2).Value = Me.TextBox1.Value (comment/source)
.Cells(dcc + 1, 3).Value = Me.TextBox2.Value (In)
.Cells(dcc + 1, 4).Value = Me.TextBox3.Value 
.Cells(dcc + 1, 5).Value = Me.TextBox4.Value
.Cells(dcc + 1, 6).Value = Me.TextBox5.Value
End With

TextBox1.Text = ""
TextBox2.Text = ""
TextBox3.Text = ""
TextBox4.Text = ""
TextBox5.Text = ""


End Sub

在 userform1 上添加的复选框是 checkbox1,我在上面的代码中放置了 (comment/source) (rent) 以确定需要传输哪些行。 如果可能,那么当复选框被选中时意味着条目将进入 2019 年 1 月,并且在利润损失中,那么该条目将自动突出显示并更改颜色,让我们说绿色? 或任何有关此事。 只是为了在未使用 userform1 并且用户只是查看工作表时脱颖而出。 我感谢您在实现这一目标方面的帮助。 提前致谢。

干杯。

从UserForm1检查userForm1的PS只需在选中复选框时添加到利润下的租金上,而且没有其他。

好的,我不是专家,我得到了@FreeMan 的帮助,他花时间解释和提供代码。 我不相信这一点。 把它放在那里,这样其他有类似问题的人都可以从中受益。 我非常感谢所有试图帮助解决此问题以及其他 VBA 相关问题和代码的人。

这里是:

Private Sub CommandButton1_Click()

  Dim dcc As Long
  Dim abc As Worksheet, pfl As Worksheet

  Set abc = ThisWorkbook.Worksheets(Me.ComboBox1.Value)
  Set pfl = Sheets("ProfitLoss")

With abc
dcc = .Range("A" & Rows.count).End(xlUp).row
.Cells(dcc + 1, 1).Value = Date
.Cells(dcc + 1, 2).Value = Me.TextBox1.Value
.Cells(dcc + 1, 3).Value = Me.TextBox2.Value
.Cells(dcc + 1, 4).Value = Me.TextBox3.Value
.Cells(dcc + 1, 5).Value = Me.TextBox4.Value
.Cells(dcc + 1, 6).Value = Me.TextBox5.Value

End With


If CheckBox1.Value Then 'this is a shorter way of writing the conditional
With pfl
  dcc = .Range("A" & Rows.count).End(xlUp).row
  .Cells(dcc + 1, 1).Value = Date
  .Cells(dcc + 1, 2).Value = Me.TextBox1.Value
  .Cells(dcc + 1, 3).Value = Me.TextBox2.Value
  .Cells(dcc + 1, 4).Value = Me.TextBox3.Value
  .Cells(dcc + 1, 5).Value = Me.TextBox4.Value
  .Cells(dcc + 1, 6).Value = Me.TextBox5.Value
 End With
End If

 TextBox1.text = ""
 TextBox2.text = ""
 TextBox3.text = ""
 TextBox4.text = ""
 TextBox5.text = ""

End Sub

我希望它会帮助某人。 干杯。

暂无
暂无

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

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