2017-01-18 7 views
1

テーブル/範囲をフィルタリングして結果をコピーし、新しいブックを作成してそこに貼り付けるコードを貼り付けます(貼り付け値は&です)。貼り付けデータ値 - シートを別のシートにコピー

また、終了後、ソーステーブルからフィルタをクリアしようとします。

A1(hedears included)から新しいブックにデータを貼り付けることはできません。それは立ち往生し、「範囲が定義されていません」と言います。(書式を貼り付けるだけで、値を貼り付けようとすると固まってしまいます)。

また、私はvbaに新しいですが、私はコードがから始まっていると感じています。Selection.Copy; Workbooks.Add行は基本的なものであり、エラーが発生しやすくなります。

助けてください、これはコードです:

Sub ExportRezAng() 
' 
' ExportRezAng Macro 


    Dim src As Worksheet 
    Dim tgt As Worksheet 
    Dim filterRange As range 
    Dim copyRange As range 
    Dim lastRow As Long 

    Set src = ThisWorkbook.Sheets("- - REZULTAT ANAF - -") 


    ' turn off any autofilters that are already set 
    src.AutoFilterMode = False 

    ' find the last row with data in column A 
    lastRow = src.range("D" & src.Rows.Count).End(xlUp).Row 

    ' the range that we are auto-filtering (all columns) 
    Set filterRange = src.range("A3:R" & lastRow) 

    ' the range we want to copy (only columns we want to copy) 
    ' in this case we are copying country from column A 
    ' we set the range to start in row 2 to prevent copying the header 
    Set copyRange = src.range("A3:N" & lastRow) 

    ' filter range based on column B 
    filterRange.AutoFilter Field:=9, Criteria1:="DA", _ 
     Operator:=xlOr, Criteria2:="=NU" 

    ' copy the visible cells to our target range 
    ' note that you can easily find the last populated row on this sheet 
    ' if you don't want to over-write your previous results 
    copyRange.SpecialCells(xlCellTypeVisible).Select 

抱える問題STARTこちらから!

Selection.Copy 
    Workbooks.Add 
    Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _ 
     SkipBlanks:=False, Transpose:=False 
    range("A1").Select 
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _ 
     :=False, Transpose:=False 


    Columns("M:N").Select 
    Application.CutCopyMode = False 
    Selection.NumberFormat = "m/d/yyyy" 
    Sheets("Sheet1").Select 
    Sheets("Sheet1").Name = "CREDIT_DOSARE_EXECUTORI" 

ThisWorkbook. _ 
     Activate 
    Rows("3:3").Select 
    Application.CutCopyMode = False 
    ThisWorkbook.Worksheets("- - REZULTAT ANAF - -").AutoFilter.Sort.SortFields. _ 
     Clear 
    ActiveSheet.ShowAllData 


End Sub 

答えて

0

あなたのコードは、あなたが参照のうえれたワークブックまたはシートを知りませんでした。あなたはright..Thankある

Sub ExportRezAng() 
' 
' ExportRezAng Macro 
    Dim Src As Worksheet 
    Dim tgt As Worksheet 
    Dim filterRange As Range 
    Dim copyRange As Range 
    Dim lastRow As Long 
    Dim wB As Workbook 

    Set Src = ThisWorkbook.Sheets("- - REZULTAT ANAF - -") 

    ' turn off any autofilters that are already set 
    Src.AutoFilterMode = False 

    ' find the last row with data in column A 
    lastRow = Src.Range("D" & Src.Rows.Count).End(xlUp).Row 

    ' the range that we are auto-filtering (all columns) 
    Set filterRange = Src.Range("A3:R" & lastRow) 

    ' the range we want to copy (only columns we want to copy) 
    ' in this case we are copying country from column A 
    ' we set the range to start in row 2 to prevent copying the header 
    Set copyRange = Src.Range("A3:N" & lastRow) 

    ' filter range based on column B 
    filterRange.AutoFilter Field:=9, Criteria1:="DA", _ 
     Operator:=xlOr, Criteria2:="=NU" 

    ' copy the visible cells to our target range 
    ' note that you can easily find the last populated row on this sheet 
    ' if you don't want to over-write your previous results 
    copyRange.SpecialCells(xlCellTypeVisible).Copy 

    Set wB = Workbooks.Add 
    With wB.Sheets(1) 
     With .Range("A1") 
      .PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _ 
       SkipBlanks:=False, Transpose:=False 
      .PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, _ 
       SkipBlanks:=False, Transpose:=False 
      Application.CutCopyMode = False 
     End With '.Range("A1") 
     .Columns("M:N").NumberFormat = "m/d/yyyy" 
     .Name = "CREDIT_DOSARE_EXECUTORI" 
    End With 'wB.Sheets(1) 

    Src.AutoFilter.Sort.SortFields.Clear 
    Src.ShowAllData 
End Sub 
+0

:) – MisterA

+0

最後に一つ質問してください、私は一度に2つの範囲をコピーしたい場合:

はこれを参照してください。? copyraste = Src.range( "A3:H、O3:R"&lastRow) - コピーペーストから特定の列をスキップするのと同じように、どうすればいいですか? – MisterA

+0

@mistera:私は知らない、一般的に私はマクロレコーダーを使用し、コードをカスタマイズ!しかし、それを貼り付けるのはおそらくかなり混乱しているので、おそらく2つのコピー/ペーストでそれをやります! ;) – R3uK

関連する問題