我无法让我的副本正常工作。我需要它使用“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
答案 0 :(得分: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)
现在,应该将rngToCopy
设置为aCell
列中的单元格范围,从第1行到aCell
所在的行。