0

我正在尝试从工作表输入中选择列 E 和 K,处理Working sheet并粘贴Output sheet到最后使用的行之后。我已将最后使用的行号存储在 x 中,并将值粘贴到 x+1 单元格中。但是 excel 选择工作表的最后一行(x 为 65536)并给出运行时错误 4004。有人可以帮助我协助代码。

Dim x As Long, y As String
    Sheets("Input").Activate
    Range("E:E,K:K").Select
    Range("K1").Activate
    Selection.Copy
    Sheets("Working").Select
    Cells(1, 1).Select
    ActiveSheet.Paste
    Cells.Select
    Application.CutCopyMode = False
    Selection.AutoFilter
    Range("B5").Select
    ActiveSheet.Range("$A$1:$H$30").AutoFilter Field:=1, Criteria1:="="
    Cells.Select
    Selection.Delete Shift:=xlUp
    Columns("B:B").Select
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    Range("B2").Select
    ActiveCell.FormulaR1C1 = _
        "=IF(RC[-1]="""","""",VLOOKUP(RC[-1],Repository!C[-1]:C[1],3,0))"
    Range("B2").Select
    Selection.Copy
    Range("B3:B30").Select
    ActiveSheet.Paste
    Cells.Select
    Application.CutCopyMode = False
    Selection.Copy
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Range("B2:C2").Select
    Range(Selection, Selection.End(xlDown)).Select
    Application.CutCopyMode = False
    Selection.Copy
    Sheets("Output").Select
    Range("A1").Select
    x = Worksheets("Output").UsedRange.Rows.Count
    y = "a" & Trim(x + 1)
    ActiveSheet("Output").Range(y).Select
    ActiveSheet.Paste
4

5 回答 5

1

您的 UsedRange 仍然认为最后一行是 65536。添加此子例程,然后在设置 x 之前调用它。

Sub CorrectUsedRange()
Dim values
Dim usedRangeAddress As String
Dim r As Range
'Get UsedRange Address prior to deleting Range
usedRangeAddress = ActiveSheet.UsedRange.Address
'Store values of cells to array.
values = ActiveSheet.UsedRange
'Delete all cells in the sheet
ActiveSheet.Cells.Delete
'Restore values to their initial locations
Range(usedRangeAddress) = values
End Sub
于 2013-09-10T14:54:34.493 回答
0

我会使用 Find 循环来填充数组,然后在宏完成时输出数组。不需要“工作”表。这也用于Cells(Rows.Count, "A").End(xlUp)查找最后填充的行,而不是UsedRange.Rows.Count不可靠的行。

Sub tgr()

    Dim rngFound As Range
    Dim rngLookup As Range
    Dim arrResults() As Variant
    Dim ResultIndex As Long
    Dim strFirst As String

    With Sheets("Input").Columns("E")
        Set rngFound = .Find("*", .Cells(.Cells.Count), xlValues, xlWhole)
        If Not rngFound Is Nothing Then
            strFirst = rngFound.Address
            ReDim arrResults(1 To WorksheetFunction.CountA(.Cells), 1 To 2)
            Do
                If rngFound.Row > 1 Then
                    ResultIndex = ResultIndex + 1
                    On Error Resume Next    'Just in case the VLookup can't find the value on the 'Repository' sheet
                    arrResults(ResultIndex, 1) = Evaluate("VLOOKUP(""" & rngFound.Value & """,Repository!A:C,3,FALSE)")
                    arrResults(ResultIndex, 2) = .Parent.Cells(rngFound.Row, "K").Value
                    On Error GoTo 0         'Remove the On Error Resume Next condition
                End If
                Set rngFound = .Find("*", rngFound, xlValues, xlWhole)
            Loop While rngFound.Address <> strFirst
        End If
    End With

    If ResultIndex > 0 Then Sheets("Output").Cells(Rows.Count, "A").End(xlUp).Offset(1).Resize(ResultIndex, UBound(arrResults, 2)).Value = arrResults

    Set rngFound = Nothing
    Erase arrResults

End Sub
于 2013-09-10T15:26:34.487 回答
0

有时,使用范围通常会变大并且不会自行重置。发生这种情况时,我发现强制它正确重置的唯一方法是保存主题工作表所在的工作簿。无论如何,这对我有用,在 Excel 2010 上。由于您正在使用.Selectand Active<obj>(我不推荐),它只是这样:

ActiveWorkbook.Save
于 2013-09-10T15:04:13.750 回答
0

而不是使用范围检查已经用此代码填充了多少行:

X = WorksheetFunction.CountA(列(1))

当然,这只有在 A 列中没有空行时才有效,因为这些行将被忽略!

于 2013-09-11T15:20:51.563 回答
0

在代码底部附近替换:

Sheets("Output").Select

和:

Sheets("Output").Select
ActiveSheet.UsedRange

这应该“重新设置”UsedRange

于 2013-09-10T14:01:12.360 回答