2017-03-04 9 views
1

私は何をしようとしていますか。 私はカレンダーが欲しい私は、予定が完了したときに予定を入れて、所得として私の予定に請求された金額を加え、また、私が購入した日に物資を購入するときに追加することができます。次に、その情報(所得/経費)を印刷して税金を勘案して会計士に引き渡すことができる別のタブに移入させます。は、VBAを使用したExcelのカレンダーでの入力になります。

私はカレンダーの部分に取り組んでいますが、正しい列に表示される日に問題があります。私は日ごとに3つの列を持っているので、後でデータを追加することができます。私はカレンダーに入れる日を得ることができますが、毎回2つの列をスキップする必要がありますが、そうではありません。

私はこの時点でどのようにコードが出力されているのかの抜粋を含めています。

Sub CreateCalendar() 
Dim csheet As Worksheet 
Set csheet = ThisWorkbook.Sheets("Sheet2") 

selDate = [b1] 
fMon = DateSerial(Year(selDate), Month(selDate), 1) 
lMon = CDate(Application.WorksheetFunction.EoMonth(fMon, 0)) 

stRow = 4 

'clear last cal 
Rows(4).ClearContents 
Rows(10).ClearContents 
Rows(16).ClearContents 
Rows(22).ClearContents 
Rows(28).ClearContents 
Rows(34).ClearContents 


'determine what weekday 1st is. . . 
If Weekday(fMon) = 1 Then 
    stCol = 4 
ElseIf Weekday(fMon) = 4 Then 
    stCol = 7 
ElseIf Weekday(fMon) = 7 Then 
    stCol = 10 
ElseIf Weekday(fMon) = 10 Then 
    stCol = 13 
ElseIf Weekday(fMon) = 13 Then 
    stCol = 16 
ElseIf Weekday(fMon) = 16 Then 
    stCol = 19 
ElseIf Weekday(fMon) = 19 Then 
    stCol = 22 
End If 

For x = 1 To Day(lMon) 
If FirstT = Empty Then 
    csheet.Cells(stRow, stCol) = fMon 
    FirstT = 1 
Else 
    fMon = fMon + 1 
    csheet.Cells(stRow, stCol) = fMon 
End If 

If stCol = 22 Then 
    stCol = 4 
    stRow = stRow + 8 
Else 
    stCol = stCol + 1 
End If 

Next x 

End Sub 

Calendar

+0

「毎回2つの列をスキップする必要があります」とはどういう意味ですか? –

+0

コードを実行して(実際に曜日を取得するために実際に変更する必要がある)、2011年3月1日の日付を使用した場合、1行〜16行4行(col GV)、17行〜31行12 col DR)。あなたは本当にそれが7日以上の幅であることを望んでいますか? –

+0

申し訳ありませんが間違った画像が掲載されています。それは7日だけになりますが、私は4日目が3日目、3日目が1日目だった2日目が必要です –

答えて

0

私はあなたのコードを修正し、私はあなたが望む通りに動作します信じています。 注:(1)テストの日付をハードコードしました。 (2)6行ごとに 'ClearContents'へのコードが8行ずつインクリメントするコードと異なります。私は6行に設定しました。 (3)私は最初の行に曜日名を置いた場所を削除することができます。

Option Explicit 

Sub CreateCalendar() 
Dim csheet As Worksheet 
Dim selDate As Date 
Dim fMon As Long 
Dim lMon As Long 
Dim stRow As Integer 
Dim stCol As Integer 
Dim FirstT As Integer 
Dim x  As Integer 
Dim iColOffset As Integer 



    Set csheet = ThisWorkbook.Sheets("Sheet2") 

    selDate = #1/1/2017#  '[b1] 
    fMon = DateSerial(Year(selDate), Month(selDate), 1) 
    lMon = CDate(Application.WorksheetFunction.EoMonth(fMon, 0)) 

    iColOffset = 4  ' Set default starting column 
    'I added the following code so I could keep track... you can delete 
    Cells(1, iColOffset) = "Sunday" 
    Cells(1, iColOffset + 3) = "Monday" 
    Cells(1, iColOffset + 6) = "Tuesday" 
    Cells(1, iColOffset + 9) = "Wednesday" 
    Cells(1, iColOffset + 12) = "Thursday" 
    Cells(1, iColOffset + 15) = "Friday" 
    Cells(1, iColOffset + 18) = "Saturday" 

    stRow = 4   ' Starting Row 

    'clear last cal 
    Rows(4).ClearContents 
    Rows(10).ClearContents 
    Rows(16).ClearContents 
    Rows(22).ClearContents 
    Rows(28).ClearContents 
    Rows(34).ClearContents 


    'determine what weekday 1st is. . . 
    Debug.Print "First DOW = " & Weekday(fMon) 
    stCol = Weekday(fMon)  ' Set starting column 
' If Weekday(fMon) = 1 Then 
'  stCol = 1 
' ElseIf Weekday(fMon) = 2 Then 
'  stCol = 2 
' ElseIf Weekday(fMon) = 3 Then 
'  stCol = 3 
' ElseIf Weekday(fMon) = 10 Then 
'  stCol = 4 
' ElseIf Weekday(fMon) = 13 Then 
'  stCol = 5 
' ElseIf Weekday(fMon) = 16 Then 
'  stCol = 6 
' ElseIf Weekday(fMon) = 19 Then 
'  stCol = 7 
' End If 

    For x = 1 To Day(lMon) 
     If FirstT = Empty Then 
      csheet.Cells(stRow, iColOffset + (stCol * 3) - 3) = Day(CDate(fMon)) 
      FirstT = 1 
     Else 
      fMon = fMon + 1 
      csheet.Cells(stRow, iColOffset + (stCol * 3) - 3) = Day(CDate(fMon)) 
     End If 

     'Debug.Print iColOffset + (stCol * 3) - 3 
     If iColOffset + (stCol * 3) - 3 = 22 Then 
      stCol = 1 
      ' *** NOTE!! Your code doesn't match. 
      ' Above, you clear every 6 Rows (4, 10, 16, 22...), but here you are incrementing by 8. 
      ' Which is it? 
      'stRow = stRow + 8 
      stRow = stRow + 6    ' I changed to 6 to match what you clear 
     Else 
      stCol = stCol + 1 
     End If 
    Next x 

End Sub 
+0

次回の作業には大変感謝しています。 –

関連する問題