-1

我无法让我的副本正常工作。我需要它使用“A”单元格的位置作为要复制的起始单元格,然后让 .end(xlup) 从下到上查找单元格到“A”单元格位置,并仅将带有数据的单元格复制到 wbVer(省略空白单元格)。

With wbVer.ws(1)
    for each ws in wbCs.sheets
If instr(1, ws.name, "value") then
     Set acell = ws.Cells.Find(What:=strPhone, After:=ActiveCell, LookIn:=xlValues, _
     LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
     MatchCase:=False, SearchFormat:=False)
If Not acell Is Nothing Then
     Set firstRange = wbkVer.ws("CSV").Range("A" & .Rows.Count).End(xlUp).Offset(1,                             0).Resize(acell.Rows.Count, 1)
     Set rngCSPhcell = .Range(acell.Offset(1, 0)).End(xlUp).offset(-1,0)  '<---this isn't right
     rngCSPhcell.Copy
     firstRange.PasteSpecial xlPasteValues
End if
End if

End With    
4

1 回答 1

1

在没有看到更多代码或工作表的情况下,我只能建议:

除了您已经拥有的任何变量之外,还对这些变量进行标注:

Dim aCell As Range
Dim rngToCopy As Range      'Use this to contain the ENTIRE range of cells to be copied
Dim cl As Range
Dim c As Long: c = 1

然后,从 开始If Not aCell is Nothing,将您的代码替换为以下内容:

If Not aCell Is Nothing Then
'Commented out per revision:'
    'Set rngToCopy = Range(Cells(1, aCell.Column).Address, Cells(aCell.Row, aCell.Column).Address)  '
    Set rngToCopy = Range(acell.Offset(1, 0), Range("A1048576").End(xlUp))

    'Assuming this evaluates to a single cell range...
    'find the next empty cell in column A of wbkVer.ws("CSV")
    Set firstRange = wbkVer.ws("CSV").Range("A1048576").End(xlUp).Offset(1, 0) '<< Set this Range to the FIRST cell address that you want to paste to.

    For Each cl In rngToCopy
        'If statement to ignore blank cells:
        If Not cl.Value = vbNullString Then
            'This puts the cell's value in firstRange, row "c", column 1.'
            'Adjust as necessary.'
            'assuming firstRange is a single cell, we can just use .Offset with the '
            ' incrementer variable, "c":
            firstRange.Offset(c, 0).Value = cl.Value
            c = c + 1
        End If
    Next
End If

请注意,我不确定您是如何尝试设置的aCell,因此可能需要修改此行:

Set rngToCopy = Range(Cells(1, aCell.Column).Address, Cells(aCell.Row, aCell.Column).Address)

现在,它应该设置rngToCopyaCell' 列中的单元格范围,从第 1 行到任何行aCell

于 2013-04-02T15:37:22.107 回答