我有一个用户表单,可以将数据输入到第I列到第S列,但有时不需要输入所有数据点。我可能只是在列I上排了最后一行,所以如果我在J到S中有数据,它们将被替换为第一列中包含数据的下一组数据。
我需要帮助的是编码所有列的最后一行或所有列的下一个空行。感谢。
我的代码:
Private Sub cmd_EnterData_Click()
Dim iRow As Long
Dim Lastrow As Long
Dim ws As Worksheet
Set ws = Worksheets("FirstShift")
Lastrow = ws.Range("i101").End(xlUp).Row
'find first empty row in database
For iRow = 16 To Lastrow
If ws.Cells(iRow, 9) = "" And ws.Cells(iRow, 10) = "" Then
ws.Cells(iRow, 9).Value = Me.textbox_Lane1.Value
ws.Cells(iRow, 10).Value = Me.textbox_Lane2.Value
ws.Cells(iRow, 11).Value = Me.textbox_Lane3.Value
ws.Cells(iRow, 12).Value = Me.textbox_Lane4.Value
ws.Cells(iRow, 13).Value = Me.textbox_Lane5.Value
ws.Cells(iRow, 14).Value = Me.textbox_Lane6.Value
ws.Cells(iRow, 15).Value = Me.textbox_Lane7.Value
ws.Cells(iRow, 16).Value = Me.textbox_Length.Value
ws.Cells(iRow, 17).Value = Me.textbox_SheetCount.Value
ws.Cells(iRow, 18).Value = Me.cbchecktype.Value
ws.Cells(iRow, 19).Value = Me.cbchecktype1.Value
End If
Next iRow
If checkbox_Retest.Value = False And Me.textbox_Lane1.Value = "" Then
'do nothing
Me.textbox_Lane1.SetFocus
MsgBox "ENTER LANE 1 WIDTH!"
Exit Sub
End If
If checkbox_Retest.Value = False And Me.textbox_Length.Value = "" Then
'do nothing
Me.textbox_Length.SetFocus
MsgBox "ENTER YOUR LENGTH!"
Exit Sub
End If
If checkbox_Retest.Value = False And Me.textbox_SheetCount.Value = "" Then
'do nothing
Me.textbox_SheetCount.SetFocus
MsgBox "ENTER THE SHEETCOUNT!"
Exit Sub
End If
If checkbox_Retest.Value = False And Me.cbchecktype.Value = "" Then
'do nothing
Me.cbchecktype.SetFocus
MsgBox "ENTER 'PASS' OR 'FAIL' FOR PERF CHECK!!"
Exit Sub
Select Case checktype
Case Trim(Me.cbchecktype.Value) = "PASS"
checktype = "PASS"
Case Trim(Me.cbchecktype.Value) = "FAIL"
checktype = "FAIL"
End Select
End If
If checkbox_Retest.Value = False And Me.cbchecktype1.Value = "" Then
'do nothing
Me.cbchecktype1.SetFocus
MsgBox "ENTER 'PASS' OR 'FAIL' FOR SLITHER CHECK!!"
Exit Sub
Select Case checktype1
Case Trim(Me.cbchecktype1.Value) = "PASS"
checktype1 = "PASS"
Case Trim(Me.cbchecktype1.Value) = "FAIL"
checktype1 = "FAIL"
End Select
End If
With ws
.Unprotect Password:="password"
.Cells(iRow, 9).Value = Me.textbox_Lane1.Value
.Cells(iRow, 10).Value = Me.textbox_Lane2.Value
.Cells(iRow, 11).Value = Me.textbox_Lane3.Value
.Cells(iRow, 12).Value = Me.textbox_Lane4.Value
.Cells(iRow, 13).Value = Me.textbox_Lane5.Value
.Cells(iRow, 14).Value = Me.textbox_Lane6.Value
.Cells(iRow, 15).Value = Me.textbox_Lane7.Value
.Cells(iRow, 16).Value = Me.textbox_Length.Value
.Cells(iRow, 17).Value = Me.textbox_SheetCount.Value
.Cells(iRow, 18).Value = Me.cbchecktype.Value
.Cells(iRow, 19).Value = Me.cbchecktype1.Value
.Protect Password:="password"
End With
'clear the data
Me.textbox_Lane1.Value = ""
Me.textbox_Lane2.Value = ""
Me.textbox_Lane3.Value = ""
Me.textbox_Lane4.Value = ""
Me.textbox_Lane5.Value = ""
Me.textbox_Lane6.Value = ""
Me.textbox_Lane7.Value = ""
Me.textbox_Length.Value = ""
Me.textbox_SheetCount.Value = ""
Me.cbchecktype.Value = ""
Me.cbchecktype1.Value = ""
Me.checkbox_Retest.Value = False
Me.Hide
End Sub
A.S.H,我已经尝试过您的代码,同样的事情正在发生但我不认为它的代码。请查看之前和之后的图片。我认为问题是我的IF声明:
"If ws.Cells (iRow, 9) = "" And ws.Cells(iRow, 10) = "" Then
正如你所看到的,当我在第9列(I)和第10列(J)中有一个项目时,该函数工作正常,但是当我在这两列中没有数据时,它会被任何数据替换为I'只要在第9列或第10列中包含数据,我就会在我的用户表单上输入。有什么可以纠正这个吗?
答案 0 :(得分:0)
添加这两个暗淡的陈述。
Dim ColumnCount As Integer
Dim x As Long
更改您的代码:
Lastrow = ws.Range("i101").End(xlUp).Row
为:
Lastrow = 0
For ColumnCount = 0 To 10
x = ws.Range("I101").Offset(0, ColumnCount).End(xlUp).Row
If x > Lastrow Then Lastrow = x
Next ColumnCount
编辑: 这个for循环总是从16开始。它是否应该在Lastrow + 1开始?
For iRow = 16 To Lastrow
答案 1 :(得分:0)
考虑到所有列,以下内容将为您提供最后一个非空行:
Lastrow = ws.UsedRange.Find("*", , , , xlByRows, xlPrevious).Row
您还可以将.UsedRange
替换为特定的列范围,将其限制为一组列,即以下内容可以使G
列中的最后一个非空行变为{{1} }:
AB