运行时错误'1004'我做错了什么?

时间:2017-05-22 15:22:32

标签: excel vba

“提取范围包含缺少或无效的字段名称”。我做错了什么?

Private Sub CommandButton1_Click()

Dim ws_count As Integer
Dim i As Integer
Dim j As Integer
Dim currentws As Variant
Dim lastrow As Long
ws_count = ActiveWorkbook.Worksheets.Count
j = 6

For i = 5 To ws_count

    lastrow = ActiveSheet.Cells(Rows.Count, "C").End(xlUp).Row
    currentws = ActiveWorkbook.Sheets("Admin").Range("D" & j).Text

    If i = 5 Then
        ActiveWorkbook.Sheets(currentws).Range("C13:C47").AdvancedFilter Action:=xlFilterCopy, CopyToRange:=ActiveWorkbook.Sheets("SM Summaries").Range("C6"), Unique:=True
    Else
    'Error occurs on the below line
        ActiveWorkbook.Sheets(currentws).Range("C13:C47").AdvancedFilter Action:=xlFilterCopy, CopyToRange:=ActiveWorkbook.Sheets("SM Summaries").Range("C" & lastrow), Unique:=True
    End If

    j = j + 1

Next i
End Sub

我检查并确保我的lastrow正在更新,我的工作表正在更新,我的整数正在改变。细胞范围完全清楚。现在我只想在长列中列出C13:C47每个工作表范围内的所有值。

我取得了一些进展。无论出于何种原因,我需要给每个列表一些粘贴之间的“喘息空间”。所以,如果我说它,每个粘贴之间有15个空白单元格,那很好。但是,如果它只是一个单元格太近我得到了那个错误。我不明白为什么。

编辑:我已将我的代码更新为“有效”。正如布鲁斯指出的那样,我从错误的页面得到了拉斯特罗。但是,在我排序和删除重复项之前,它仍然在我的数据之间存在各种疯狂的间距。不知道为什么,但是为每个循环添加+ 1会因某种原因而修复它...

Private Sub CommandButton1_Click()
Application.ScreenUpdating = False 'Run faster

Dim ws_count As Integer
Dim i As Integer
Dim j As Integer
Dim currentws As Variant
Dim lastrow As Integer
Dim sht As Worksheet
Set sht = ThisWorkbook.Worksheets("SM Summaries")
lastrow = sht.Cells(sht.Rows.Count, "C").End(xlUp).Row
ws_count = ActiveWorkbook.Worksheets.Count
j = 6

Range("C6:C1000").ClearContents

For i = 5 To ws_count

    lastrow = sht.Cells(sht.Rows.Count, "C").End(xlUp).Row
    currentws = ActiveWorkbook.Sheets("Admin").Range("D" & j).Text

    If i = 5 Then
        ActiveWorkbook.Sheets(currentws).Range("C13:C47").AdvancedFilter Action:=xlFilterCopy, CopyToRange:=ActiveWorkbook.Sheets("SM Summaries").Range("C6"), Unique:=False
    Else
        lastrow = lastrow + 1
        ActiveWorkbook.Sheets(currentws).Range("C13:C47").AdvancedFilter Action:=xlFilterCopy, CopyToRange:=ActiveWorkbook.Sheets("SM Summaries").Range("C" & lastrow), Unique:=False
    End If

    j = j + 1

Next i

With Range("C6:C1000")
    .Value = .Value
    .RemoveDuplicates Columns:=1, Header:=xlNo
    On Error Resume Next
    .SpecialCells(xlCellTypeBlanks).Delete xlShiftUp
    On Error GoTo 0
End With

MsgBox "Re-population complete.", vbInformation, "Success!"
Application.ScreenUpdating = True
End Sub

0 个答案:

没有答案