简体   繁体   English

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

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

workbook工作簿

profitloss利润损失

userform用户表单

new userform option新的用户表单选项

User @Lambik helped me a lot in achieving functionality in this workbook.用户@Lambik 帮助我实现了本工作簿中的功能。 Successfully managed to get the data from userform1 to January 2019 sheet and at the same time transferred on to the profitloss sheet.成功获取了userform1到2019年1月表中的数据,同时转入到了盈亏表中。 What I didn't thought about was that in profit and loss all fields can not be copied and pasted.没想到的是,盈亏中所有字段都不能复制粘贴。

I tried to add new label and new textbox on the userform and get it to work by studying the code provided by @Lambik but there was another error, not error its more like a repetition, if I keep the edited version of userform1 (I've deleted it btw very non practical) then even if extra fields are "not" filled on the userform1 the rest of the entries still go into profitloss, that means, manually deleting every entry which is not suppose to be in there.我尝试在用户表单上添加新标签和新文本框,并通过研究@Lambik 提供的代码使其工作,但还有另一个错误,而不是更像是重复的错误,如果我保留 userform1 的编辑版本(我'顺便说一下,我删除了它非常不切实际)那么即使在 userform1 上“未”填充了额外的字段,其余条目仍然会进入利润损失,这意味着手动删除每个不应该在那里的条目。 So I thought long and hard and only thing I can think of is to add a checkbox on the userform1 and when the checkbox is clicked only then the first two fields from userform1 which are comment and rent go into profitloss along with the respective month selected from the combobox1.所以我想了很久,唯一能想到的就是在 userform1 上添加一个复选框,当复选框被点击时,userform1 中的前两个字段是评论和租金,以及从中选择的相应月份进入利润损失组合框1。 I think, I think it's the right logic.我认为,我认为这是正确的逻辑。 If anybody can help with the VBA code that how to achieve this using the checkbox then I'd appreciate it very much.如果有人可以帮助使用 VBA 代码来说明如何使用复选框来实现这一点,那么我将不胜感激。 I've attached links to the screenshots of the workbook, column rows of profitloss, old userform1 (works like a charm, thanks to @Lambik) and the new edited userform1 with the extra label and checkbox to have data copied onto the profit and loss sheet along with the current month selected.我已经附加了工作簿截图的链接,利润损失的列行,旧的 userform1(就像一个魅力,感谢@Lambik)和新编辑的 userform1,带有额外的标签和复选框,可以将数据复制到损益表上工作表以及所选的当前月份。

The code that have the userform1 working is,使 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

The added checkbox on userform1 is checkbox1 and I put (comment/source) (rent) in code above to identify which rows needs to be transferred.在 userform1 上添加的复选框是 checkbox1,我在上面的代码中放置了 (comment/source) (rent) 以确定需要传输哪些行。 If possible then when the checkbox is checked meaning entry will go into january 2019 and also in profitloss then that entry be automatically be highlighted and change colour to lets say green?如果可能,那么当复选框被选中时意味着条目将进入 2019 年 1 月,并且在利润损失中,那么该条目将自动突出显示并更改颜色,让我们说绿色? or any for that matter.或任何有关此事。 just to stand out if userform1 is not in use and user is just looking at the sheet.只是为了在未使用 userform1 并且用户只是查看工作表时脱颖而出。 I appreciate your help in achieving this.我感谢您在实现这一目标方面的帮助。 Thanks in advance.提前致谢。

Cheers.干杯。

PS From userform1 only comment and rent needs to be added onto the profitloss sheet when the checkbox is checked and nothing else.从UserForm1检查userForm1的PS只需在选中复选框时添加到利润下的租金上,而且没有其他。

OK I'm no expert and I had help from @FreeMan who took his time in explaining and providing the code.好的,我不是专家,我得到了@FreeMan 的帮助,他花时间解释和提供代码。 I take no credit in this.我不相信这一点。 Putting it out there so anybody else with similar issue can benefit from this.把它放在那里,这样其他有类似问题的人都可以从中受益。 I well and truly appreciate all who have tried to help with this and with other VBA related questions and code here.我非常感谢所有试图帮助解决此问题以及其他 VBA 相关问题和代码的人。

Here it is:这里是:

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

I hope it'll help somebody.我希望它会帮助某人。 Cheers.干杯。

暂无
暂无

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

相关问题 Excel2016。如何在工作表的Userform1上显示特定值? - Excel 2016. How to show specific value on Userform1 from a sheet? 使用VBA将Excel工作表链接到同一工作簿中的另一个工作表 - Linking an Excel Sheet to another in the same workbook using VBA 用于将多个范围复制到另一个工作簿中的特定工作表的 Excel VBA 代码 - Excel VBA code to copy several ranges to specific sheet in another workbook Excel VBA; 尝试将数据从工作簿中的一个工作表复制到同一工作簿中的另一工作表时,运行时错误'438' - Excel VBA; Run-time error '438' when trying to copy data from one sheet in a work book to another sheet in the same workbook 如何使用VBA更新用户表单中的Excel工作表数据 - How to update data of an excel sheet in a userform with vba Excel VBA:使用同一工作簿中的工作表吗? - Excel VBA: Use a sheet from the same workbook? 在工作簿中的每个工作表上重复Excel VBA代码 - Repeat Excel VBA code on Each Sheet in Workbook VBA将数据从一个工作簿表复制到另一工作簿表 - VBA copy data from one workbook sheet to another workbook sheet VB代码:将用户窗体数据导入Excel工作表 - VB code : Import userform data to excel sheet 是否可以从 UserForm2 的代码访问 Excel UserForm1 的动态 object? 不是来自模块的代码 - It is possible to access a dynamic object of Excel UserForm1 from code of UserForm2? Not from code of a module
相关标签
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM