将UserForm Data输入下一个空单元格区域

时间:2017-05-09 14:02:34

标签: excel vba excel-vba

我有一个UserForm,它接受文本输入和OptionButton选择,将数据输入到具有特定分类的工作表中。

使用来自计数器i的值将条目放置在工作表中的特定单元格中,并且可以将条目移除到"存档"工作表位于同一工作簿上,通过使用按钮。这可以在列表中留下自由的条目。即

Job 1 is entered 
Job 2 is entered
Job 3 is entered 
Job 2 is archived

There now exists a space between job 1 and 3.

我要做的是创建一个循环,它将检查一个空的Cell范围,并将UserForm数据放在下一个可用的空单元格中。每5个单元格输入数据,因此A5,A10,A15 ......等等。

我目前的代码如下:

If UserForm1.OptionButton1.Value = True Then
  If Range("A" & (5 * i)).Value = "" Then
    ActiveSheet.Range("A" & (5 * i) & ":" & "O" & (3 + (5 * i))).Select
    With Selection.Interior
      .Color = RGB(239, 231, 121)
    End With

    With Worksheets("Active Jobs")
        .Range("A" & (5 * i)) = UserForm1.TextBox1     'Place job 5 cells apart
        .Range("B" & (5 * i) & ":" & "E" & (3 + (5 * i))).Merge
        .Range("B" & (5 * i)) = UserForm1.TextBox2
        .Range("B" & (5 * i) & ":" & "E" & (3 + (5 * i))).VerticalAlignment = xlTop
        .Range("G" & (5 * i) & ":" & "O" & (3 + (5 * i))).Merge

    Set t = .Range("T" & (5 * i))
    Set btn = .Buttons.Add(t.Left, t.Top, t.Width, t.Height)

      With btn
        .Name = "RemovetoArchive" & i1
        .Caption = "Archive"
        .OnAction = "RemovetoArchive"
      End With

    End With

  i = i + 1

  ElseIf Range("A" & (5 * i)).Value <> "" Then
    For i = 1 To 25
    'I can't figure out how to implement the for loop here.
    Next i
  End If

1 个答案:

答案 0 :(得分:0)

所以猜测我有一个脑屁,离开这几天然后回来几乎立即解决了。

使用范围

的新变量C创建While循环
While Range("A" & (5 * c)).Value <> ""
    c = c + 1
Wend 

这每5个细胞找到最近的空细胞。