第一次在這里發帖。
我需要幫助!
所以我試圖在初始化用戶表單時創建一個回圈。基本上,當我單擊用戶表單時,頂部的文本框將顯示帶有空白相鄰單元格的人的姓名
Private Sub UserForm_Initialize()
Dim i As Long
Set wb = ThisWorkbook.Sheets("Sheet1")
With wb
i = 2
Do Until IsEmpty(Cells(i, 2).Value) = False
'Check if cell i, 2 is blank (in this case Range B2 is blank so code will proceed)
If IsEmpty(Cells(i, 2).Value) = True Then
'Get value of adjacent cell and place in text box
Textbox1.Value = Sheets("Sheet1").Cells(i, 1).Value
'value is name of person with blank adjacent cell value
End If
i = i 1
Loop
'code will loop until there is no longer any blanks in column B
End With
End Sub
范圍 B2 為空白

范圍 A2 應該是文本框的值

uj5u.com熱心網友回復:
一個做...回圈
Option Explicit
Private Sub UserForm_Initialize()
Dim ws As Worksheet: Set ws = ThisWorkbook.Worksheets("Sheet1")
Dim i As Long: i = 2
Do Until IsEmpty(ws.Cells(i, "B").Value)
i = i 1
Loop
TextBox1.Value = ws.Cells(i, "A").Value
End Sub
以下是其他一些相同的口味:
Sub DoLoop2()
Dim ws As Worksheet: Set ws = ThisWorkbook.Worksheets("Sheet1")
Dim i As Long: i = 2
Do
If IsEmpty(ws.Cells(i, 2).Value) Then Exit Do
i = i 1
Loop
TextBox1.Value = ws.Cells(i, 1).Value
End Sub
Sub DoLoop3()
Dim ws As Worksheet: Set ws = ThisWorkbook.Worksheets("Sheet1")
Dim i As Long: i = 2
Do
If IsEmpty(ws.Cells(i, 2).Value) Then
TextBox1.Value = ws.Cells(i, 1).Value
Exit Do
End If
i = i 1
Loop
End Sub
uj5u.com熱心網友回復:
使用 for 回圈嘗試此代碼
Private Sub UserForm_Initialize()
Dim mysheet As Worksheet
Dim LastRow As Long
Set mysheet = ActiveSheet
'find the last row number of a range using Find Function
LastRow = mysheet.Cells.Find("*", searchorder:=xlByRows, searchdirection:=xlPrevious).Row
For i = 2 To LastRow
If IsEmpty(Cells(i, 2).Value) Then
Me.TextBox1.Text = Cells(i, 1).Value ' add person's name to textbox1
End If
Next i
End Sub
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/452345.html
