簡體   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