標題:
隨時新增欄位問題
[打印本頁]
作者:
g93353
時間:
2012-3-2 10:29
標題:
隨時新增欄位問題
想請問各位高手們
小弟今天分別在A1 B1 C1 D1 E1 F1 欄位 分別打上了每個月的第一天
也就是一月一日 至 六月一日
小弟想製作一個輸入數字並且按下按鈕後 就能夠以輸入的數字來分割出前面幾個月 每週的星期日
用說的可能很難理解 舉例一下
A B C D E F
1 1/1 2/1 3/1 4/1 5/1 6/1
假設今天輸入數字 : 2
代表前面兩個月都必須顯示出一月及二月的每個星期日 日期
以2012年來說
就變成這樣
A B C D E F G H I J K L M N
1 1/1 1/8 1/15 1/22 1/29 2/1 2/4 2/11 2/18 2/25 3/1 4/1 5/1 6/1
以此類推
若輸入的數字是3 那就顯示出前面三個月每個月的星期日的日期
想請問各位高手 由於輸入的數字不一定 每年每週的星期日也都不同
請教該如何寫出巨集>"< 困擾很久了 還請各位解答!!!!
作者:
hugh0620
時間:
2012-3-2 11:51
回復
1#
g93353
樓主~ 這個是可以被使用的~ 只是它的防呆~ 沒有寫的很好~
可以試看看~ 應該可以符合你要的結果~
Sub EX()
A = InputBox("請輸入年份")
B = InputBox("請輸入月份")
If Len(A) = 4 And (Len(B) = 1 Or Len(B) = 2) Then
A1 = DateValue(A & "年" & "1月" & "1日")
A2 = DateValue(A & "年" & "2月" & "1日")
A3 = DateValue(A & "年" & "3月" & "1日")
A4 = DateValue(A & "年" & "4月" & "1日")
A5 = DateValue(A & "年" & "5月" & "1日")
A6 = DateValue(A & "年" & "6月" & "1日")
I = 0
Sheet1.Rows("1:1").ClearContents
Select Case B
Case 1
Do Until Month(A1 + I) > 1
If Weekday(A1 + I, 2) = 7 Then
If Sheet1.Range("A1") = "" Then
Sheet1.Range("A1") = A1 + I
Else
Sheet1.Range("IV1").End(xlToLeft).Offset(0, 1) = A1 + I
End If
End If
I = I + 1
Loop
Sheet1.Range("IV1").End(xlToLeft).Offset(0, 1) = A2
Sheet1.Range("IV1").End(xlToLeft).Offset(0, 1) = A3
Sheet1.Range("IV1").End(xlToLeft).Offset(0, 1) = A4
Sheet1.Range("IV1").End(xlToLeft).Offset(0, 1) = A5
Sheet1.Range("IV1").End(xlToLeft).Offset(0, 1) = A6
Case 2
Do Until Month(A1 + I) > 2
If Weekday(A1 + I, 2) = 7 Then
If Sheet1.Range("A1") = "" Then
Sheet1.Range("A1") = A1 + I
Else
Sheet1.Range("IV1").End(xlToLeft).Offset(0, 1) = A1 + I
End If
End If
I = I + 1
Loop
Sheet1.Range("IV1").End(xlToLeft).Offset(0, 1) = A3
Sheet1.Range("IV1").End(xlToLeft).Offset(0, 1) = A4
Sheet1.Range("IV1").End(xlToLeft).Offset(0, 1) = A5
Sheet1.Range("IV1").End(xlToLeft).Offset(0, 1) = A6
Case 3
Do Until Month(A1 + I) > 3
If Weekday(A1 + I, 2) = 7 Then
If Sheet1.Range("A1") = "" Then
Sheet1.Range("A1") = A1 + I
Else
Sheet1.Range("IV1").End(xlToLeft).Offset(0, 1) = A1 + I
End If
End If
I = I + 1
Loop
Sheet1.Range("IV1").End(xlToLeft).Offset(0, 1) = A4
Sheet1.Range("IV1").End(xlToLeft).Offset(0, 1) = A5
Sheet1.Range("IV1").End(xlToLeft).Offset(0, 1) = A6
Case 4
Do Until Month(A1 + I) > 4
If Weekday(A1 + I, 2) = 7 Then
If Sheet1.Range("A1") = "" Then
Sheet1.Range("A1") = A1 + I
Else
Sheet1.Range("IV1").End(xlToLeft).Offset(0, 1) = A1 + I
End If
End If
I = I + 1
Loop
Sheet1.Range("IV1").End(xlToLeft).Offset(0, 1) = A5
Sheet1.Range("IV1").End(xlToLeft).Offset(0, 1) = A6
Case 5
Do Until Month(A1 + I) > 5
If Weekday(A1 + I, 2) = 7 Then
If Sheet1.Range("A1") = "" Then
Sheet1.Range("A1") = A1 + I
Else
Sheet1.Range("IV1").End(xlToLeft).Offset(0, 1) = A1 + I
End If
End If
I = I + 1
Loop
Sheet1.Range("IV1").End(xlToLeft).Offset(0, 1) = A6
Case 6
Do Until Month(A1 + I) > 6
If Weekday(A1 + I, 2) = 7 Then
If Sheet1.Range("A1") = "" Then
Sheet1.Range("A1") = A1 + I
Else
Sheet1.Range("IV1").End(xlToLeft).Offset(0, 1) = A1 + I
End If
End If
I = I + 1
Loop
End Select
End If
End Sub
複製代碼
作者:
g93353
時間:
2012-3-2 12:00
先來研究看看 謝謝hugh0620大大!!!!!!!!!
作者:
hugh0620
時間:
2012-3-2 12:10
回復
3#
g93353
有再稍微看過樓主要求~ 好像是要把每月的1號帶出來~
可使用下列的方式~
若不用帶出每月1號的資料~ 就採用上面第一個模組~
Sub EX()
A = InputBox("請輸入年份")
B = InputBox("請輸入月份")
If Len(A) = 4 And (Len(B) = 1 Or Len(B) = 2) Then
A1 = DateValue(A & "年" & "1月" & "1日")
A2 = DateValue(A & "年" & "2月" & "1日")
A3 = DateValue(A & "年" & "3月" & "1日")
A4 = DateValue(A & "年" & "4月" & "1日")
A5 = DateValue(A & "年" & "5月" & "1日")
A6 = DateValue(A & "年" & "6月" & "1日")
I = 0
Sheet1.Rows("1:1").ClearContents
Select Case B
Case 1
Do Until Month(A1 + I) > 1
If Weekday(A1 + I, 2) = 7 Or (Month(A1 + I) = 1 And Day(A1 + I) = 1) Then
If Sheet1.Range("A1") = "" Then
Sheet1.Range("A1") = A1 + I
Else
Sheet1.Range("IV1").End(xlToLeft).Offset(0, 1) = A1 + I
End If
End If
I = I + 1
Loop
Sheet1.Range("IV1").End(xlToLeft).Offset(0, 1) = A2
Sheet1.Range("IV1").End(xlToLeft).Offset(0, 1) = A3
Sheet1.Range("IV1").End(xlToLeft).Offset(0, 1) = A4
Sheet1.Range("IV1").End(xlToLeft).Offset(0, 1) = A5
Sheet1.Range("IV1").End(xlToLeft).Offset(0, 1) = A6
Case 2
Do Until Month(A1 + I) > 2
If Weekday(A1 + I, 2) = 7 Or (Month(A1 + I) = 2 And Day(A1 + I) = 1) Then
If Sheet1.Range("A1") = "" Then
Sheet1.Range("A1") = A1 + I
Else
Sheet1.Range("IV1").End(xlToLeft).Offset(0, 1) = A1 + I
End If
End If
I = I + 1
Loop
Sheet1.Range("IV1").End(xlToLeft).Offset(0, 1) = A3
Sheet1.Range("IV1").End(xlToLeft).Offset(0, 1) = A4
Sheet1.Range("IV1").End(xlToLeft).Offset(0, 1) = A5
Sheet1.Range("IV1").End(xlToLeft).Offset(0, 1) = A6
Case 3
Do Until Month(A1 + I) > 3
If Weekday(A1 + I, 2) = 7 Or (Month(A1 + I) = 3 And Day(A1 + I) = 1) Then
If Sheet1.Range("A1") = "" Then
Sheet1.Range("A1") = A1 + I
Else
Sheet1.Range("IV1").End(xlToLeft).Offset(0, 1) = A1 + I
End If
End If
I = I + 1
Loop
Sheet1.Range("IV1").End(xlToLeft).Offset(0, 1) = A4
Sheet1.Range("IV1").End(xlToLeft).Offset(0, 1) = A5
Sheet1.Range("IV1").End(xlToLeft).Offset(0, 1) = A6
Case 4
Do Until Month(A1 + I) > 4
If Weekday(A1 + I, 2) = 7 Or (Month(A1 + I) = 4 And Day(A1 + I) = 1) Then
If Sheet1.Range("A1") = "" Then
Sheet1.Range("A1") = A1 + I
Else
Sheet1.Range("IV1").End(xlToLeft).Offset(0, 1) = A1 + I
End If
End If
I = I + 1
Loop
Sheet1.Range("IV1").End(xlToLeft).Offset(0, 1) = A5
Sheet1.Range("IV1").End(xlToLeft).Offset(0, 1) = A6
Case 5
Do Until Month(A1 + I) > 5
If Weekday(A1 + I, 2) = 7 Or (Month(A1 + I) = 5 And Day(A1 + I) = 1) Then
If Sheet1.Range("A1") = "" Then
Sheet1.Range("A1") = A1 + I
Else
Sheet1.Range("IV1").End(xlToLeft).Offset(0, 1) = A1 + I
End If
End If
I = I + 1
Loop
Sheet1.Range("IV1").End(xlToLeft).Offset(0, 1) = A6
Case 6
Do Until Month(A1 + I) > 6
If Weekday(A1 + I, 2) = 7 Or (Month(A1 + I) = 6 And Day(A1 + I) = 1) Then
If Sheet1.Range("A1") = "" Then
Sheet1.Range("A1") = A1 + I
Else
Sheet1.Range("IV1").End(xlToLeft).Offset(0, 1) = A1 + I
End If
End If
I = I + 1
Loop
End Select
End If
End Sub
複製代碼
作者:
g93353
時間:
2012-3-2 13:17
感謝hugh0620 的回覆 小弟試過後發現 每個月的一號有些月份不會顯示出來說
像2011年就不行了>"<
作者:
register313
時間:
2012-3-2 13:24
本帖最後由 register313 於 2012-3-2 15:10 編輯
Sub YY()
Rows("2") = ""
MX = InputBox("請輸入月份數")
For M = 1 To MX
First = Cells(1, M) + (7 - Weekday(Cells(1, M), 2))
[A2].Offset(0, C) = First
C = C + 1
Do While Month(First + 7) = Month(Cells(1, M))
[A2].Offset(0, C) = First + 7
First = First + 7
C = C + 1
Loop
Next M
If MX <> 6 Then
Cells(1, MX + 1).Resize(1, 6 - MX).Copy [A2].Offset(0, C)
End If
Range([A2], [IV2].End(xlToLeft)).NumberFormatLocal = "m/d;@"
End Sub
複製代碼
作者:
hugh0620
時間:
2012-3-2 13:36
本帖最後由 hugh0620 於 2012-3-2 13:42 編輯
回復
5#
g93353
若樓主您的是該年度整年份的資料~ (僅限一個年度)
這樣下面的寫法~ 可以得到你需要的結果~
若您需要的設定某一個區間的資料~ 寫法又不一樣了~
case by case
Sub EX()
Dim B As Integer
A = InputBox("請輸入年份")
B = InputBox("請輸入月份")
If Len(A) = 4 And (Len(B) = 1 Or Len(B) = 2) Then
A1 = DateValue(A & "年" & "1月" & "1日")
I = 0
Sheet1.Rows("1:1").ClearContents
Do Until Month(A1 + I) > B
If Weekday(A1 + I, 2) = 7 Or (Month(A1 + I) = 1 And Day(A1 + I) = 1) Then
If Sheet1.Range("A1") = "" Then
Sheet1.Range("A1") = A1 + I
Else
Sheet1.Range("IV1").End(xlToLeft).Offset(0, 1) = A1 + I
End If
End If
I = I + 1
Loop
If B <> 12 Then
For I = B + 1 To 12
Sheet1.Range("IV1").End(xlToLeft).Offset(0, 1) = DateValue(A & "年" & I & "月" & "1日")
Next
End If
End If
End Sub
複製代碼
作者:
g93353
時間:
2012-3-2 14:48
謝謝 register313 跟 hugh0620 大大
想請問一下 hugh0620 大大
小弟已經自行把每月的第一天都先打在欄位上了(有可能跨年份 EX : 從2011 到2012)
那這樣寫法要如何作修改?
作者:
register313
時間:
2012-3-2 14:57
回復
8#
g93353
Sub YY()
Rows("2") = ""
MX = InputBox("請輸入月份數")
For M = 1 To MX
First = Cells(1, M) + (7 - Weekday(Cells(1, M), 2))
[A2].Offset(0, C) = First
C = C + 1
Do While Month(First + 7) = Month(Cells(1, M))
[A2].Offset(0, C) = First + 7
First = First + 7
C = C + 1
Loop
Next M
Cells(1, MX + 1).Resize(1, 6 - MX).Copy [A2].Offset(0, C)
Range([A2], [IV2].End(xlToLeft)).NumberFormatLocal = "m/d;@"
End Sub
複製代碼
作者:
g93353
時間:
2012-3-2 15:45
本帖最後由 g93353 於 2012-3-2 16:01 編輯
謝謝register313
您跟 hugh0620 大大寫法都是輸入年月份後會自動跑出
小弟已經分別把每個月1號都各別寫在A1 到 F1(假設是1到6月)
而輸入的數字是指前面(數字)個月都必須顯示出星期日(從每個月的1號後面依序顯示出)
大大們的作法都部不會有新增欄位的問題
小弟已先將每個月的第一天先打好
這樣欄位的新增就會不固定
所以麻煩再請教各位了
先前敘述有點糢糊 不好意思了>"<
作者:
register313
時間:
2012-3-2 16:29
回復
10#
g93353
Sub YY()
Rows("2") = ""
MX = InputBox("請輸入月份數")
For M = 1 To MX
If [A2] = "" Then
[A2] = Cells(1, M)
Else
[IV2].End(xlToLeft).Offset(0, 1) = Cells(1, M)
End If
First = Cells(1, M) + (7 - Weekday(Cells(1, M), 2))
If [IV2].End(xlToLeft) <> First Then
[IV2].End(xlToLeft).Offset(0, 1) = First
End If
Do While Month(First + 7) = Month(Cells(1, M))
[IV2].End(xlToLeft).Offset(0, 1) = First + 7
First = First + 7
Loop
Next M
If MX <> 6 Then
Cells(1, MX + 1).Resize(1, 6 - MX).Copy [IV2].End(xlToLeft).Offset(0, 1)
End If
Range([A2], [IV2].End(xlToLeft)).NumberFormatLocal = "m/d;@"
End Sub
複製代碼
作者:
Hsieh
時間:
2012-3-3 00:00
回復
1#
g93353
Sub Ex()
Dim k%, Ar(), j%, s%, ky, d As Object
k = InputBox("輸入月數", , 3)
Set d = CreateObject("Scripting.Dictionary")
For Each A In Rows(1).SpecialCells(xlCellTypeConstants)
d(Format(A, "yyyy/mm")) = DateValue(Format(A, "yyyy/mm/1"))
Next
For Each ky In d.keys
j = j + 1
If j <= k Then
For i = d(ky) To DateAdd("M", 1, d(ky)) - 1
If (Day(i) = 1 Or Weekday(i, vbMonday) = 7) Then
ReDim Preserve Ar(s)
Ar(s) = i
s = s + 1
End If
Next
Else
ReDim Preserve Ar(s)
Ar(s) = d(ky)
s = s + 1
End If
Next
Rows(1) = ""
[A1].Resize(, s) = Ar
End Sub
複製代碼
作者:
hugh0620
時間:
2012-3-3 09:00
回復
12#
Hsieh
H大大~ 小弟有測試過您的程式碼~
若像樓主有跨年度的情況~
ex. 若資料從20120101~20131201的話~
輸入2月份
程式碼僅會抓取 201201~201202的資料處理~
因為程式碼並未針對從哪一個年月的資料來處理~
而是僅處理第一個抓取的月份~
作者:
Hsieh
時間:
2012-3-3 10:35
回復
13#
hugh0620
基本上樓主的問題有一些不清楚
是要輸入處理幾個月,然後進行欄位重編的話,程式應可行
若是指定那些月份,那就要輸入2個參數,起始月份及處理月數或結束月份
所以,這只是提供另一種思路參考,至於如何符合個人需求,還是要自己動腦
歡迎光臨 麻辣家族討論版版 (http://forum.twbts.com/)