Excel VBA:更改一行代码后无法过去正确的值

时间:2018-07-26 18:08:44

标签: excel-vba

VBA专家-

有人可以解释为什么以下代码无法正常工作吗? 我将一行VBA代码更改为

copyrange.SpecialCells(xlCellTypeVisible).Copy ThisWorkbook.Worksheets("Referred to testing").Range("A1")"

**"copyrange.SpecialCells(xlCellTypeVisible).Copy sht2.Range("A1")"**

当我检查粘贴在sheet2中的值时,它仅在第一行中显示值,所有其他值都消失了。感谢您的帮助:) 以下是完整的代码:

Sub Macro1()
Dim lrow As Integer
Dim copyrange As Range
Dim filterRange As Range
ActiveSheet.Name = "All Candidates"

Set sht = ThisWorkbook.Worksheets("All Candidates")

ActiveWorkbook.Sheets.Add Before:=Worksheets(Worksheets.Count)
ActiveSheet.Name = "Referred to testing"
Set sht2 = ThisWorkbook.Worksheets("Referred to testing")

LastColumn = sht.Range("A1").CurrentRegion.Columns.Count
lrow = Cells(Rows.Count, 4).End(xlUp).Row
Set copyrange = sht.Range("D1:E" & lrow)
Set filterRange = sht.Range("A1:K" & lrow)

    filterRange.AutoFilter field:=5, Criteria1:="Referred to Testing"
   ActiveWorkbook.Sheets.Add Before:=Worksheets(Worksheets.Count)
   ActiveSheet.Name = "Referred to testing"
   copyrange.SpecialCells(xlCellTypeVisible).Copy sht2.Range("A1")


sht2.Range("$A$1:$B$1045033").RemoveDuplicates Columns:=Array(1, 2), Header:=xlYes
End Sub

1 个答案:

答案 0 :(得分:0)

对于任何有兴趣的人,以下是正确的代码行:

Sub Macro1()
  Dim lrow As Integer
  Dim copyrange As Range
  Dim filterRange As Range
  ActiveSheet.Name = "All Candidates"
  Set sht = ThisWorkbook.Worksheets("All Candidates")

  ActiveWorkbook.Sheets.Add Before:=Worksheets(Worksheets.Count)
  ActiveSheet.Name = "Referred to testing"
  Set sht2 = ThisWorkbook.Worksheets("Referred to testing")

  LastColumn = sht.Range("A1").CurrentRegion.Columns.Count
  lrow = sht.Cells(Rows.Count, 4).End(xlUp).Row

  Set filterRange = sht.Range("A1:K" & lrow)
    filterRange.AutoFilter field:=5, Criteria1:="Referred to Testing"
  Set copyrange = sht.Range("D1:E" & lrow)
  copyrange.SpecialCells(xlCellTypeVisible).Copy sht2.Range("A1")


  sht2.Range("$A$1:$B$1045033").RemoveDuplicates Columns:=Array(1, 2), Header:=xlYes
  End Sub