簡體   English   中英

希望excel用戶窗體在下一個空白行中填充特定范圍

[英]Want excel userform to populate a specific range in the next blank row

第一次在這里發布。 我試圖在下一個“空白行”中填寫我的userform值,但該行不會真正空白。 我希望用戶表單填寫的范圍將為空白,但是工作表的第一列和最后兩列將具有公式。
例如,“ A”,“ D”和“ E”中已經包含公式,我需要在用戶表單中填寫“ B”和“ C”。 我希望這是有道理的。

Private Sub Add_Click()
'Copy input values to sheet.
Dim lastRow As Long
Dim ws As Worksheet
Set ws = Sheets("Risk&Opp")
lastRow = ws.Range("B" & Rows.Count).End(xlUp).Row + 1
ws.Range("B" & lastRow).Value = ComboBox1.Value
ws.Range("C" & lastRow).Value = ComboBox2.Value
ws.Range("D" & lastRow).Value = TextBox1.Value
ws.Range("E" & lastRow).Value = TextBox2.Value
ws.Range("F" & lastRow).Value = ComboBox4.Value
ws.Range("G" & lastRow).Value = ComboBox5.Value
ws.Range("H" & lastRow).Value = Me.ComboBox6.Value
ws.Range("I" & lastRow).Value = Me.ComboBox7.Value
ws.Range("J" & lastRow).Value = Me.ComboBox8.Value
ws.Range("K" & lastRow).Value = Me.TextBox3.Value
ws.Range("M" & lastRow).Value = Me.TextBox4.Value
ws.Range("O" & lastRow).Value = Me.ComboBox9.Value
ws.Range("P" & lastRow).Value = Me.TextBox5.Value
ws.Range("Q" & lastRow).Value = Me.TextBox6.Value

我在這里有A,R和S的公式。我希望我的用戶表單查看范圍B:Q是否為空,如果是,請從用戶表單中分配這些值。

這應該對您的第一次檢查有效。 可以根據需要進行的邏輯檢查

IF ws.Range("B" & lastRow) <> "" Then ws.Range("B" & lastRow) = ComboBox1.Value

您可以循環瀏覽最后一行中的單元格,以檢查每個單元格是否為空白。 如果不是,請對下一行執行相同操作,直到找到第一行,其中從B列到Q的所有單元格都為空白。

這是代碼:

Sub test()
Dim lastRow As Long
Dim ws As Worksheet
Dim rowFilled As Boolean

Set ws = Sheets("Risk&Opp")
lastRow = ws.Range("B" & Rows.Count).End(xlUp).Row

'Set boolean to true
rowFilled = True

'Count rows until you find a row where range B:Q is blank
While rowFilled = True
    rowFilled = False

    'Loop from column B (index 2) to column Q (index 17)
    For i = 2 To 17

        'If one of the cell is not blank increase lastrow
        If ws.Cells(lastRow, i) <> "" Then
            rowFilled = True
            lastRow = lastRow + 1
            Exit For
        End If
    Next i
Wend

'The first row with blank range B:Q
MsgBox lastRow

'...Add your existing code here...

End Sub

暫無
暫無

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

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