Excel 2016 VBA Code for Userform1上的复选框,用于指定要传输到另一张纸上的数据(同一工作簿)

时间:2018-11-06 22:17:59

标签: excel vba excel-vba checkbox

workbook

profitloss

userform

new userform option

@Lambik用户在此工作簿中为我提供了很多帮助。成功地从userform1到2019年1月的工作表中获取数据,同时转移到利润表中。我没有想到的是,在损益表中,所有字段都无法复制和粘贴。

我尝试在userform上添加新标签和新文本框,并通过研究@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,我在上面的代码中放入了(注释/源代码)(租金)以标识需要转移的行。如果可能的话,那么当选中此复选框时,该条目将进入2019年1月,同时也会出现利润损失,然后该条目将自动突出显示并将颜色更改为绿色?或任何与此有关的东西。只是在不使用userform1且用户只是查看工作表的情况下脱颖而出。感谢您为实现这一目标所提供的帮助。提前致谢。

干杯。

P.S。在userform1中,选中此复选框时,只需将注释和租金添加到利润表中即可,

1 个答案:

答案 0 :(得分:0)

好的,我不是专家,@ 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

我希望它将对某人有所帮助。干杯。