收到错误如果不是isempty

时间:2018-05-16 13:15:21

标签: excel vba excel-vba

我正在尝试对一张纸上的数据列进行排序,并跳过空单元格并复制非空单元格并将其粘贴到另一张纸张中。我有这个代码工作,最近我开始收到错误1004.我相信"。选择"导致错误。我不熟悉物体,但这可能是要走的路?

有没有人知道更好的方法呢?

Sub PopulateList()

With ActiveWorkbook
Worksheets("Create Appointments").Select
[A4:J100].Clear
End With

'Two Week Follow Up
    With ActiveWorkbook
    Worksheets("All Current Projects").Select
    'Column L has Install Dates
    [L4].Select
    End With

Dim Cell As Range
For Each Cell In Range("L4:L80").Cells
If Not IsEmpty(Cell) Then
        'Only move values that occurred after today
        If Cell.Value >= Now() Then
            'Once Values are found, move them to CreateAppointments sheet
            Worksheets("Create Appointments").Select
                Range("F3").End(xlDown).Offset(1, 0).Value = Cell.Value + 14 
        End If
End If
Next Cell

End Sub

5 个答案:

答案 0 :(得分:0)

"使用ActiveWorkbook工作表("所有当前项目")。选择"

尝试ActiveWorkbook.sheets("All Current Projects")

答案 1 :(得分:0)

首先要做的事情。停止使用[Produces("application/json")] [Route("api/WebService")] public class WebServiceController { [HttpPost("CreateRequest")] public override IActionResult Create([FromBody] CreateRequest request) { if (request == null) { return BadRequest(); } else { // Do stuff with CreateRequest object ... return new OkResult(); } } } .Select是人类做的事情。您的代码不需要“选择”一个单元格。快速重写:

这可能会或可能不会解决您的问题,因为不清楚是什么引发了这个错误:

.Select

答案 2 :(得分:0)

我清理了你的代码并取出了IsEmpty:

classifier

答案 3 :(得分:0)

xlDown没有正确找到下一个空格,最好用xlUp找到它。

注意:此代码假定在清除A4:J100后,F3后列F上没有更多数据

编辑:我更改了测试范围,更改了范围以反映您的范围。

Sub PopulateList()
    ActiveWorkbook.Worksheets("Create Appointments").Range("A4:J100").Clear
    'Two Week Follow Up
    'Column L has Install Dates
    Dim Cell As Range
    For Each Cell In Worksheets("All Current Projects").Range("L4:L80")
        If Cell.Value <> 0 Then
            'Only move values that occurred after today
            If Cell.Value >= Now() Then
                'Once Values are found, move them to CreateAppointments sheet
                'This assumes that after clearing Range A4:J100 there is no more data under F3
                Worksheets("Create Appointments").Cells(Rows.Count, 6).End(xlUp).Offset(1, 0).Value = Cell.Value + 14
            End If
        End If
    Next Cell
End Sub

答案 4 :(得分:0)

当F列的最后一个单元格填满时,我可以复制您的错误。 上面提到的里卡多A: “注意:此代码假定在清除A4:J100后,F3之后的F列上没有更多数据”

您可以查看:

Debug.Print Worksheets("Sheet1").Range("F3").End(xlDown).Address