2012-02-08 20 views
0

です。他の4つのセクションがある既存のブックで、新しいワークシート( "プル")としてSharePointから大量のデータをインポートします。私は実行時にマクロを開発しようとしています。)は、自動的にプル内のフィールドでデータをフィルタリングします。 b)そのフィルタリングされたデータのセルA5から始まる既存のシートへのコピー/「ペースト値」; c。)次のシートのためにプルでフィルターをリセットします。フラットファイルの人口は

たとえば、プル(デフォルトのワークシート名 "owssvr")では、各行のカラムには、その行の項目が作成された日時が表示されます。前月のすべてのアイテムをプルで自動的にフィルタリングするか(あるいは、月を選択するオプションをユーザに与えます)、フィルタリングされた結果の値をセルから開始する「月報」というワークシートにコピー/ペーストしますA5(ヘッダーは変更されません)?これは可能ですか?

答えて

0

これは、私はそれを書くだろうかです:

Option Explicit 

Sub MonthFilter() 
Dim LR As Long, MyDate As Date, d1 As Date, d2 As Date 

MyDate = Application.InputBox("Enter any date in the month you wish to pull", "Enter Date", Date - 30, Type:=2) 
If MyDate = 0 Then 
    Exit Sub 
Else 
    d1 = DateSerial(Year(MyDate), Month(MyDate), 1) 
    d2 = DateSerial(Year(MyDate), Month(MyDate) + 1, 1) - 1 
End If 

With Sheets("The Pull") 
    .AutoFilterMode = False 
    .Rows(1).AutoFilter 
    .Rows(1).AutoFilter 44, Criteria1:=">=" & d1, _ 
      Operator:=xlAnd, Criteria2:="<=" & d2 
    LR = .Cells(.Rows.Count, 44).End(xlUp).Row 
    If LR > 1 Then .Range("A2:A" & LR).EntireRow.Copy Sheets("Monthly Report").Range("A5") 
    .AutoFilterMode = False 
End With 

End Sub 
0

あなたはフィルタリングやunfilterしAutoFilterShowAllDataを使用することができます。ここに例があります。

Sub CopyLastMonthFromThePull(shtCopyTo As Worksheet) 
    Dim rngPullTable As Range, iColumnToFilter As Integer, strMonth As String 

    ' this assumes that the pull data is the first Excel Table on ThePull worksheet named owssvr 
    Set rngPullTable = ThisWorkbook.Worksheets("owssvr").ListObjects(1).Range 
    rngPullTable.Parent.Activate 

    ' determine the filter details 
    strMonth = CStr(DateSerial(Year(Date), Month(Date) - 1, Day(Date))) ' one month prior to today 
    iColumnToFilter = 44 ' Column AR is the 44th column 

    ' filter the table 
    rngPullTable.AutoFilter Field:=iColumnToFilter, Operator:=xlFilterValues _ 
        , Criteria2:=Array(1, strMonth) 
    DoEvents 

    ' copy the filtered results. (This also copies the header row.) 
    rngPullTable.Copy 
    With shtCopyTo 
     .Activate 
     .Range("A5").PasteSpecial xlPasteFormulasAndNumberFormats 
     .Columns.AutoFit 
     .Range("A1").Select 
    End With 
    Application.CutCopyMode = False 

    ' remove filter 
    With rngPullTable.Parent 
     .Activate 
     .ShowAllData 
    End With 
    rngPullTable.Range("A1").Select 

    ' End with the sheet being copied to active 
    shtCopyTo.Activate 

End Sub 
関連する問題