对于excell工作簿,我对vba中的单元格计数有疑问。我试图将在组合框中选择的文本写入excel中的正确行(图片中的第26行),如果它已满,则更改它(移动到下一行)直到第32行。如果它到达第33行,则写入错误的文本窗口(没有足够的空间)。为了更好的wiev和理解,我做了一个图片,它是所有expained。目前我使用以下代码,但它运行良好:
Private Sub btnvzdrzevalna_Click()
Dim nvdsheet As Worksheet
Set nvdsheet = ThisWorkbook.Sheets("iNVD")
nr = nvdsheet.Cells(Rows.Count, 1).End(xlUp).Row - 15
'- 15 leads to the right cell from bottom of last cell but it allways returns the same cell because the last cell in rows.count is the same so it overwrites it.
nvdsheet.Cells(nr, 2) = Me.cmbvzdrzevanje
nvdsheet.Cells(nr, 3) = "Stanovanjski zakon (SZ-1)"
nvdsheet.Cells(nr, 4) = Me.cmbizvajalec2
nvdsheet.Cells(nr, 5) = Me.cmbperioda
nvdsheet.Cells(nr, 6) = Me.cmbpregled
nvdsheet.Cells(nr, 8) = Me.tbcena1
End Sub
更好地理解问题的图片
感谢您的帮助。祝你有愉快的一天,
答案 0 :(得分:2)
尝试这些修改:
Private Sub btnvzdrzevalna_Click()
Dim nvdsheet As Worksheet
Set nvdsheet = ThisWorkbook.Sheets("iNVD")
Dim nr As Long
nr = 26
With nvdsheet
'finds first empty row starting at 26, but not moving past 33
Do Until IsEmpty(.Cells(nr, 2)) Or nr = 33
nr = nr + 1
Loop
If nr < 33 Then
.Cells(nr, 2) = Me.cmbvzdrzevanje
.Cells(nr, 3) = "Stanovanjski zakon (SZ-1)"
.Cells(nr, 4) = Me.cmbizvajalec2
.Cells(nr, 5) = Me.cmbperioda
.Cells(nr, 6) = Me.cmbpregled
.Cells(nr, 8) = Me.tbcena1
Else
MsgBox "Not Enough Space"
End If
End With
End Sub
答案 1 :(得分:0)
问题尚不清楚但是,cmbvzdrzevanje,cmbizvajalec2等是表格控件吗? (组合框等)。如果是,则需要使用text属性。
.Cells(nr, 2) = Me.cmbvzdrzevanje.text
.Cells(nr, 3) = "Stanovanjski zakon (SZ-1)"
.Cells(nr, 4) = Me.cmbizvajalec2.text
.Cells(nr, 5) = Me.cmbperioda.text
.Cells(nr, 6) = Me.cmbpregled.text
.Cells(nr, 8) = Me.tbcena1.text