将行偏移到范围内 - Vba

时间:2018-02-05 20:38:02

标签: vba

我可以做些什么来帮助缩短此代码?

Dim iRow As Long
Dim Lastrow As Long
Dim ws As Worksheet
Set ws = Worksheets("FirstShift")

Lastrow = ws.Range("c:v").Find("*", , , , xlByRows, xlPrevious).Row

'This code here:

 For iRow = 16 To Lastrow
 If ws.Cells(iRow, 9).Value = "" And ws.Cells(iRow, 9).Offset(0, 1).Value = 
 "" And ws.Cells(iRow, 9).Offset(0, 2).Value = "" And ws.Cells(iRow, 
 9).Offset(0, 3).Value = "" And ws.Cells(iRow, 9).Offset(0, 4).Value = "" 
 And ws.Cells(iRow, 9).Offset(0, 5).Value = "" And ws.Cells(iRow, 
 9).Offset(0, 6).Value = "" And ws.Cells(iRow, 9).Offset(0, 7).Value = "" 
 And ws.Cells(iRow, 9).Offset(0, 8).Value = "" And ws.Cells(iRow, 
 9).Offset(0, 9).Value = "" And ws.Cells(iRow, 9).Offset(0, 10).Value = "" 
 Then
    ws.Cells(iRow, 9).Value = Me.textbox_Lane1.Value
 End If
Next iRow

For iRow = 16 To Lastrow
If ws.Cells(iRow, 10).Value = "" And ws.Cells(iRow, 10).Offset(0, 1).Value 
= "" And ws.Cells(iRow, 10).Offset(0, 2).Value = "" And ws.Cells(iRow, 
10).Offset(0, 3).Value = "" And ws.Cells(iRow, 10).Offset(0, 4).Value = "" 
And ws.Cells(iRow, 10).Offset(0, 5).Value = "" And ws.Cells(iRow, 
10).Offset(0, 6).Value = "" And ws.Cells(iRow, 10).Offset(0, 7).Value = "" 
And ws.Cells(iRow, 10).Offset(0, 8).Value = "" And ws.Cells(iRow, 
10).Offset(0, 9).Value = "" And ws.Cells(iRow, 10).Offset(0, -1).Value = "" 
Then
   ws.Cells(iRow, 10).Value = Me.textbox_Lane2.Value
End If
Next iRow

'To here...

我有11个数据单元,在输入数据时我需要将它们保存在一行或一行中。挑战在于,有时不是所有11个数据单元都有数据,所以为了让每个数据输入保持在一行或一行,这就是我提出的代码。

现在我还要添加一些数据单元格,这段代码变得太长而荒谬。感谢帮助!谢谢!

1 个答案:

答案 0 :(得分:1)

似乎你可以简单地编码

With ws
    Lastrow = .Range("c:v").Find("*", , , , xlByRows, xlPrevious).Row

    For iRow = 16 To Lastrow
        If WorksheetFunction.CountA(.Cells(iRow, 9).Resize(0, 11)) =0 Then .Cells(iRow, 9).Value = Me.textbox_Lane1.Value
    Next
End With