我想在这段代码中删除#NA,我知道您应该使用if语句,但是我不知道我应该在代码中何处以及如何编写
Sub Copy_Paste_Below_Last_Cell()
'Find the last used row in both sheets and copy and paste data below existing data.
Dim wsCopy As Worksheet
Dim wsDest As Worksheet
Dim lCopyLastRow As Long
Dim lDestLastRow As Long
Dim CopyColumns() As Variant
CopyColumns = Array("B", "E", "H", "K", "N")
Dim Col As Variant
Set wsCopy = ThisWorkbook.Worksheets("Ba pricing")
Set wsDest = ThisWorkbook.Worksheets("Loader")
For Each Col In CopyColumns
lDestLastRow = wsDest.Cells(wsDest.Rows.Count, "C").End(xlUp).Offset(1).Row
lCopyLastRow = wsCopy.Cells(wsCopy.Rows.Count, Col).End(xlUp).Row
wsCopy.Range(Col & "30:" & Col & lCopyLastRow).Copy
wsDest.Range("C" & lDestLastRow).PasteSpecial xlPasteValues
Next Col
End Sub
答案 0 :(得分:0)
尽管我不能清楚地理解要求,但我假设#NA仅从目的地删除。我希望仅在完成复制粘贴任务后进行删除。
Sub Copy_Paste_Below_Last_Cell()
'Find the last used row in both sheets and copy and paste data below existing data.
Dim wsCopy As Worksheet
Dim wsDest As Worksheet
Dim lCopyLastRow As Long
Dim lDestLastRow As Long
Dim CopyColumns() As Variant
CopyColumns = Array("B", "E", "H", "K", "N")
Dim Col As Variant
Dim FinalStartRow As Long
Dim FinalEndRow As Long
Dim Cel As Range
Set wsCopy = ThisWorkbook.Worksheets("Ba pricing")
Set wsDest = ThisWorkbook.Worksheets("Loader")
FinalStartRow = wsDest.Cells(wsDest.Rows.Count, "C").End(xlUp).Offset(1).Row
FinalEndRow = 0
For Each Col In CopyColumns
lDestLastRow = wsDest.Cells(wsDest.Rows.Count, "C").End(xlUp).Offset(1).Row
lCopyLastRow = wsCopy.Cells(wsCopy.Rows.Count, Col).End(xlUp).Row
wsCopy.Range(Col & "30:" & Col & lCopyLastRow).Copy
wsDest.Range("C" & lDestLastRow).PasteSpecial xlPasteValues
FinalEndRow = FinalEndRow + lCopyLastRow - 30 + 1
Next Col
For Each Cel In wsDest.Range("C" & FinalStartRow & ":C" & FinalEndRow).Cells
If Application.WorksheetFunction.IsNA(Cel) Then
Cel.Delete xlShiftUp ' if required to delete cell
'Cel.ClearContents ' if required to delete contents only
End If
Next
End Sub
编辑:如果要跳过复制包含#N / A的整个列,则只需像这样修改代码
For Each Col In CopyColumns
lDestLastRow = wsDest.Cells(wsDest.Rows.Count, "C").End(xlUp).Offset(1).Row
lCopyLastRow = wsCopy.Cells(wsCopy.Rows.Count, Col).End(xlUp).Row
If wsCopy.Range(Col & "30:" & Col & lCopyLastRow).Find("#N/A", LookIn:=xlValues) Is Nothing Then
wsCopy.Range(Col & "30:" & Col & lCopyLastRow).Copy
wsDest.Range("C" & lDestLastRow).PasteSpecial xlPasteValues
End If
Next Col
Edit2:添加了将第二组列复制到H列
Dim CopyRng As Range, CopyRng2 As Range
For Each Col In CopyColumns
lDestLastRow = wsDest.Cells(wsDest.Rows.Count, "C").End(xlUp).Offset(1).Row
lCopyLastRow = wsCopy.Cells(wsCopy.Rows.Count, Col).End(xlUp).Row
Set CopyRng = wsCopy.Range(Col & "30:" & Col & lCopyLastRow)
'Since the 2nd set of columns specified is just at the right of columns specified in the 1st set
Set CopyRng2 = wsCopy.Range(CopyRng(1, 1).Offset(0, 1), CopyRng(CopyRng.Rows.Count, 1).Offset(0, 1))
If CopyRng.Find("#N/A", LookIn:=xlValues) Is Nothing Then
CopyRng.Copy
wsDest.Range("C" & lDestLastRow).PasteSpecial xlPasteValues
CopyRng2.Copy
wsDest.Range("H" & lDestLastRow).PasteSpecial xlPasteValues
End If
Next Col
答案 1 :(得分:0)
如果要从一组列中的公式复制值后删除#N / A错误,为什么不简单地跳过复制#N / A错误的开头呢?
Sub valueOnlyCopy()
Dim a As Long
Dim copyColumns As Variant, col As Variant
Dim valRng As Range, wsCopy As Worksheet, wsDest As Worksheet
copyColumns = Array("B", "E", "H", "K", "N")
Set wsCopy = ThisWorkbook.Worksheets("Ba pricing")
Set wsDest = ThisWorkbook.Worksheets("Loader")
For Each col In copyColumns
With wsCopy
With .Range(.Cells(30, col), .Cells(.Rows.Count, col).End(xlUp))
Set valRng = .SpecialCells(xlCellTypeFormulas, xlNumbers + xlTextValues + xlLogical)
End With
End With
With wsDest
For a = 1 To valRng.Areas.Count
.Cells(.Rows.Count, "C").End(xlUp).Offset(1).Resize(valRng.Areas(a).Rows.Count, 1) = _
valRng.Areas(a).Value
Next a
End With
Next col
End Sub