简体   繁体   English

Excel vba用户表单设置单元格内部不会转到下一行

[英]Excel vba userform setting cell interior doesn't go to next row

I am trying to make a userform and I want it to change the color of the next cell. 我正在尝试制作用户表单,我希望它能够改变下一个单元格的颜色。 It works everytime when I just use strings but every time I try to change the cell interior, it stays on the same row instead of going 1 row down like it should. 它每次我只使用字符串时工作,但每次我尝试更改单元格内部时,它都会停留在同一行而不是像它应该的那样向下一行。 When I run it without the color part, everything works just fine and the strings go to the next row every time I want them to. 当我在没有颜色部分的情况下运行它时,一切正常并且每次我想要时字符串都会进入下一行。 But when I insert the cell color interior part it doesn't go 1 down. 但是当我插入单元格颜色的内部部件时,它不会向下移动1。 I want to make it that if the checkbox is checked, the first column of the row is in green and if it isn't checked, it's red. 我想说,如果选中该复选框,则该行的第一列为绿色,如果未选中,则为红色。

Here's the code: 这是代码:

Private Sub btnEnter_Click()
Dim ssheet As Worksheet

If Me.tbTitle.Value = "" Or Me.tbDate.Value = "" Or Me.cmbGenre = "" Or Me.tbKeywords = "" Or Me.tbDirector = "" Or Me.tbCast = "" Then
    If MsgBox("Not all forms are completed. Do you want to continue?", vbQuestion + vbYesNo) <> vbYes Then
    Exit Sub
    End If
End If

Set ssheet = ThisWorkbook.Sheets("MovieList")

nr = ssheet.Cells(Rows.Count, 1).End(xlUp).Row + 1

If CheckBox1.Value = True Then
    ssheet.Cells(nr, 1).Interior.ColorIndex = 4
    Else: ssheet.Cells(nr, 1).Interior.ColorIndex = 3
End If

ssheet.Cells(nr, 2) = Me.tbTitle
ssheet.Cells(nr, 3) = Me.tbDate
ssheet.Cells(nr, 4) = Me.cmbGenre
ssheet.Cells(nr, 5) = Me.tbKeywords
ssheet.Cells(nr, 6) = Me.tbDirector
ssheet.Cells(nr, 7) = Me.tbCast
ssheet.Cells(nr, 8) = Me.tbComments

Call resetForm

End Sub

Sub resetForm()

Me.tbTitle = ""
Me.tbDate = ""
Me.cmbGenre = ""
Me.tbKeywords = ""
Me.tbDirector = ""
Me.tbCast = ""
Me.tbComments = ""
Me.tbTitle.SetFocus

End Sub

Private Sub CheckBox1_Click()

End Sub

Private Sub UserForm_Initialize()

'fill combobox
For Each cell In [ListGenre]
    Me.cmbGenre.AddItem
Next cell


End Sub

I would be so glad if you could help me. 如果你能帮助我,我会很高兴的。 Thank you. 谢谢。

If I understood correctly your above code, you have issues with empty cells. 如果我正确理解了您的上述代码,那么您就会遇到空单元格问题。

If you do not have any string in your 1 column you need to add some, but if you whan to have "empty" cell, and colored at the same time, you can do it like this. 如果您的1列中没有任何字符串,则需要添加一些字符串,但如果您想要“空”单元格并同时着色,则可以这样做。

Ok, between this two parts of your code: 好的,在代码的这两部分之间:

If CheckBox1.Value = True Then
     ssheet.Cells(nr, 1).Interior.ColorIndex = 4
  Else: ssheet.Cells(nr, 1).Interior.ColorIndex = 3 
End If

and this one: 还有这个:

ssheet.Cells(nr, 2) = Me.tbTitle 
ssheet.Cells(nr, 3) = Me.tbDate
ssheet.Cells(nr, 4) = Me.cmbGenre 
ssheet.Cells(nr, 5) = Me.tbKeywords 
ssheet.Cells(nr, 6) = Me.tbDirector 
ssheet.Cells(nr, 7) = Me.tbCast 
ssheet.Cells(nr, 8) = Me.tbComments

You need to put this line: 你需要把这一行:

ssheet.Cells(nr, 1) = " "

You'll get what you want! 你会得到你想要的!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM