2016-03-27 11 views
0

私は列Aの日付を調べ、列Fにリストされた毎月を印刷するExcelマクロを作成しようとしています。forループを使用しようとしています。If /それ以外の文は正しく動作するようには見えません。Excel VBAでIfステートメントでMONTHを使用する

私がこれまで持っていると、それが最後の上記の1行に到達するまで、他のすべての日付を通過した後、F2(作品)のセルにセルA3で見つかった最初の月を印刷する必要がありますものです
y = 2 

Range("F2").Formula = "=MONTH(A3)" 

For x = 4 To RowLast - 1 
    If Range("A" & x).Month = Range("F" & y) Then 
    Else 
     y = y + 1 
     Range("F" & y).Formula = "=MONTH(A" & x & ")" 
    End If 
Next 

。 if文は新しい月であることを確認し、月のF列の次のセルに月を出力するかどうかを確認する必要があります。

質問がある場合はお知らせください。ありがとうございました。

答えて

0
RowLast = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row 

y = 2 

Range("F2").Formula = "=MONTH(A3)" 

For x = 4 To RowLast - 1 
    Range("Z2").Formula = "=MONTH(A" & x & ")" 
    If Range("Z2").Value = Range("F" & y).Value Then 
    Else 
     y = y + 1 
     Range("F" & y).Formula = "=MONTH(A" & x & ")" 
    End If 
Next 
+0

範囲( "D:G")のようなMonth(1)を持つ列をすべて非表示にする関数と組み合わせることはできますか? 列( "D:BC") .Select 列( "D:G")の場合。隠し= trueの場合、 Selection.EntireColumn.Hidden = Falseの エルス Selection.EntireColumn.Hidden = Trueの END IF – flowers1234

0

if文が問題を引き起こしていると思います。今月だけ印刷している場合は、ここでif文が必要ですか?

+0

問題は、列Aに複数の日付があり、そのうちのいくつかは同じ月になりますが、列Fに一度だけ表示して、その月に基づいて別の列を合計できるようにします。 – user3709645

+0

ループを最初に実行すると、A4 = Y2が正しいことが確認されます – ccjjmartin

+0

定義する前にF月をチェックしている可能性があります。 – ccjjmartin

0

具体的な質問にお答えします:Month(date)は、日付引数の月に対応する整数を返す関数です。従ってMonth(Now)は3を返します。

.Monthはのプロパティではありません。 Rangeオブジェクトであるため、コードでエラーが発生します(「オブジェクトはこのプロパティまたはメソッドをサポートしていません」)。以下のコードは、あなたが望む方法でMonth()関数を使用する方法を示しています。

しかし、あなたのコードはより広い質問を提起します。数式作成を自動化するためにVBAを使用していますか?あなたがいれば、すべての井戸と良い。しかし、実際には、VBAがより良いサービスを提供するときに、ワークシート関数を使用している可能性はありますか?たとえば、VBAを使用して目標月を特定し、その目標月をExcel式を使用してワークシートに書き込む理由はありますか?

VBAは想像以上に可能性があるのに対し、Excelの機能を自動化する方法(最近はマクロを記録した結果)の範囲が限られているため、

いずれにしても、同じタスクの2つの非常に似たバージョンがあります。最初は数式を書き込み、2番目は月を書き込みます。

Public Sub OutputGenerator() 
    Dim ws As Worksheet 
    Dim firstRow As Long 
    Dim lastRow As Long 
    Dim dates As Variant 
    Dim hitList As Collection 
    Dim refMonth As Integer 
    Dim thisMonth As Integer 
    Dim r As Long 
    Dim output() As Integer 
    Dim item As Variant 

    'Set these for your own task. 
    Set ws = ThisWorkbook.Worksheets("Sheet1") 
    firstRow = 3 
    lastRow = 23 

    'Read the dates into an array 
    dates = ws.Range(ws.Cells(firstRow, "A"), ws.Cells(lastRow, "A")).Value 

    'Loop through the array to acquire each new date 
    Set hitList = New Collection 
    For r = 1 To UBound(dates, 1) 
     thisMonth = Month(dates(r, 1)) 
     If thisMonth <> refMonth Then 
      'It's a new date so populate the collection with the month integer 
      hitList.Add thisMonth 
      refMonth = thisMonth 
     End If 
    Next 

    'Populate the output array 
    ReDim output(1 To hitList.Count, 1 To 1) 
    r = 1 
    For Each item In hitList 
     output(r, 1) = item 
     r = r + 1 
    Next 

    'Write the output array starting at cell "F2" 
    ws.Cells(2, "F").Resize(UBound(output, 1)).Value = output 
End Sub 
:整数としてヶ月を書くこと

Public Sub FormulaGenerator() 
    Dim ws As Worksheet 
    Dim firstRow As Long 
    Dim lastRow As Long 
    Dim dateRange As Range 
    Dim cell As Range 
    Dim hitList As Collection 
    Dim refMonth As Integer 
    Dim thisMonth As Integer 
    Dim r As Long 
    Dim output() As Variant 
    Dim item As Variant 

    'Set these for your own task. 
    Set ws = ThisWorkbook.Worksheets("Sheet1") 
    firstRow = 3 
    lastRow = 20 

    'Read the values cell by cell 
    Set dateRange = ws.Range(ws.Cells(firstRow, "A"), ws.Cells(lastRow, "A")) 
    Set hitList = New Collection 
    For Each cell In dateRange.Cells 
     item = cell.Month 
     thisMonth = Month(cell.Value) 
     If thisMonth <> refMonth Then 
      'It's a new month so populate the collection with the cell address 
      hitList.Add cell.Address(False, False) 
      refMonth = thisMonth 
     End If 
    Next 

    'Populate the output array values 
    ReDim output(1 To hitList.Count, 1 To 1) 
    r = 1 
    For Each item In hitList 
     output(r, 1) = "=MONTH(" & item & ")" 
     r = r + 1 
    Next 

    'Write the output array starting at cell "F2" 
    ws.Cells(2, "F").Resize(UBound(output, 1)).Formula = output 
End Sub 

コード:数式を書くこと

コード:私はこれにオートメーションタイプがニーズに合ったとして、それはいくつかの考えを挑発いただければ幸いです

関連する問題