簡體   English   中英

Excel VBA-在具有復制公式的命名范圍內插入的新行中輸入用戶窗體數據

[英]Excel VBA - Input Userform data in new row inserted in named range with copied formulas

我是Excel VBA的新手,正在學習反復試驗和堆棧溢出方面的時間。 我花了幾個小時尋找解決方案(仍在弄清楚如何最好地解決Google VBA問題...),但沒有找到我能理解的解決方案,因此,對現有線程的任何幫助或指導將不勝感激。

我本來希望我的代碼將用戶表單數據輸入到特定命名范圍內的下一個空行中,但是現在我意識到我需要使其更具靈活性。

Sheets("Budget Input").Range("ContractInputField") 

目前是A50:E50。 F50:V50包含的公式需要與我的范圍一樣多的行。

當用戶單擊用戶窗體上的“確定”時,我需要:

  • 在現有范圍內插入新行(在“ ContractInputField”內,在該范圍的最后一個條目的下方,以及不在該范圍內並且已經具有寶貴內容的其他行的上方)
  • 復制上一行中存在的公式的新行(當前為第50行,但第一個“ ok”將增長到51,然后第二個“ ok”將增長到51,依此類推)
  • 要輸入到該新行的用戶表單數據

這是我使用原始方法的代碼:

Private Sub cmdOK_Click()
 Dim J As Long

Sheets("Budget Input").Activate
ActiveSheet.Range("ContractInputLine1").Activate

Do While IsEmpty(ActiveCell.Offset(J, 0)) = False
    J = J + 1
Loop

ActiveCell.Offset(J, 0).Value = Me.txtContractorName.Value
ActiveCell.Offset(J, 1).Value = Me.txtContractPurpose.Value
ActiveCell.Offset(J, 2).Value = Me.txtFlatRate.Value
ActiveCell.Offset(J, 3).Value = Me.txtContractHourlyRate.Value
ActiveCell.Offset(J, 4).Value = Me.txtContractHours.Value
ActiveCell.Offset(J, 16).Value = Me.ContractYear2.Value
ActiveCell.Offset(J, 17).Value = Me.ContractYear3.Value
ActiveCell.Offset(J, 18).Value = Me.ContractYear4.Value


'Clear boxes before next round of entry
Dim ctl As Control

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 cmdOK_Click()
    Dim J As Long
    Dim c As Long

    With Sheets("Budget Input")
        J = .Range("ContractInputLine1").Row
        c = .Range("ContractInputLine1").Column

        Do While Not IsEmpty(.Cells(J, c))
            J = J + 1
        Loop

        'Insert a new row for the new data, to ensure we don't start writing
        ' beyond the end of the ContractInputField range.  (Assumption is
        ' that there is already at least 1 blank row included at the end of
        ' ContractInputField or else even this won't stop the range being
        ' exceeded.)
        .Rows(J).EntireRow.Insert

        'Copy values, formulae, and formats from the previous row
        .Rows(J).EntireRow.FillDown

        'Replace values with values from the form    
        .Cells(J, c + 0).Value = Me.txtContractorName.Value
        .Cells(J, c + 1).Value = Me.txtContractPurpose.Value
        .Cells(J, c + 2).Value = Me.txtFlatRate.Value
        .Cells(J, c + 3).Value = Me.txtContractHourlyRate.Value
        .Cells(J, c + 4).Value = Me.txtContractHours.Value
        .Cells(J, c + 16).Value = Me.ContractYear2.Value
        .Cells(J, c + 17).Value = Me.ContractYear3.Value
        .Cells(J, c + 18).Value = Me.ContractYear4.Value
    End With

    'Clear boxes before next round of entry
    Dim ctl As Control

    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

注意:

1)您會注意到我在代碼中擺脫了ActiveCell。 那只是我個人的喜好-除非絕對需要,否則我不喜歡激活或選擇事物-因此,如果您有需要,請改回。

2)為了擺脫ActiveCell,我不得不包含一個變量c ,以防萬一ContractInputLine1不在A列中。如果它確實在A列中開始,則c隨處都可以替換為1,例如c + 2 。 ,被3取代。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM