iT邦幫忙

0

要抓三個月的資料卻沒反應

  • 分享至 

  • xImage

不好意思 照我的邏輯寫 應該呈現2月到4月資料 但是結果卻全部的月份都呈現

 thisyear = Year(Now)  '今年
 thismonth = Month(Now) '這個月
 lastrow = ActiveSheet.UsedRange.Rows.Count + ActiveSheet.UsedRange.Rows(1).Row - 1  '最後一行
 Worksheets("三個月變動工作計畫表").Activate  '指定工作表
 For i = 1 To lastrow   '迴圈
   If IsDate(Cells(i, "F")) = False Then  判斷F欄位是否日期
    i = i + 1     '不是日期  下一筆
   Else
    Cells(i, "Y") = Year(Cells(i, "F"))  Y欄位等於年的值
    Cells(i, "Z") = Month(Cells(i, "F"))  Z欄位等於月的值
   End If
 Next  '第一個迴圈是先把我工作表的年ˋ月的值分類好
 
 If thismonth - 1 > 0 Or thismonth - 3 > 0 Then  '先判斷這個月的數字-1或-3  是不是大於0
   beforemonth1 = Month(Now) - 1  '這個月的前1個月  以五月來說是四月
   beforemonth3 = Month(Now) - 3   '這個月的前3個月  以五月來說是二月
  For j = lastrow To 6 Step -1   '第二個迴圈
   If Cells(j, "Y") = thisyear And beforemonth1 <= Cells(j, "Z") <= beforemonth3 Then   '判斷Y欄位是今年且Z欄位是2月~4月
   Else
     Rows(j).Delete   '若不是就刪除
   End If
  Next
End If
目前只有刪除標題跟空白  其他資料都在  包括2月到全部的資料都在
我這樣寫  哪裡不對  請指教
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

2 個回答

2
rogeryao
iT邦超人 8 級 ‧ 2019-05-24 14:36:01
最佳解答

依原程式修改 :

 thisyear = Year(Now)  '今年
 thismonth = Month(Now) '這個月
 lastrow = ActiveSheet.UsedRange.Rows.Count + ActiveSheet.UsedRange.Rows(1).Row - 1  '最後一行
 Worksheets("三個月變動工作計畫表").Activate  '指定工作表
 For i = 1 To lastrow   '迴圈
   If IsDate(Cells(i, "F")) = False Then
   '判斷F欄位是否日期
    i = i + 1     '不是日期  下一筆
   Else
    Cells(i, "Y") = Year(Cells(i, "F"))
    'Y欄位等於年的值
    Cells(i, "Z") = Month(Cells(i, "F"))
    'Z欄位等於月的值
   End If
 Next  '第一個迴圈是先把我工作表的年ˋ月的值分類好
 
 If thismonth - 1 > 0 Or thismonth - 3 > 0 Then  '先判斷這個月的數字-1或-3  是不是大於0
   beforemonth1 = Month(Now) - 1  '這個月的前1個月  以五月來說是四月
   beforemonth3 = Month(Now) - 3   '這個月的前3個月  以五月來說是二月
  For j = lastrow To 1 Step -1   '第二個迴圈
   If Cells(j, "Y") = thisyear And beforemonth1 >= Cells(j, "Z") And Cells(j, "Z") >= beforemonth3 Then    '判斷Y欄位是今年且Z欄位是2月~4月
   Else
     Rows(j).Delete   '若不是就刪除
   End If
  Next
End If

另一種寫法 :

' 基準日
BaseDate = Date
' 2 月第一天
Dim FirstDate As Date
FirstDate = VBA.DateSerial(Year(BaseDate), Month(BaseDate) - 3, 1)
FirstDate_Value = Format(FirstDate, "yyyymmdd")
' 4 月最後一天
Dim LastDate As Date
LastDate = VBA.DateSerial(Year(BaseDate), Month(BaseDate), 1) - 1
LastDate_Value = Format(LastDate, "yyyymmdd")

'最後一行
lastrow = ActiveSheet.UsedRange.Rows.Count + ActiveSheet.UsedRange.Rows(1).Row - 1

For j = lastrow To 1 Step -1
   If Not (Format(Cells(j, "F"), "yyyymmdd") >= FirstDate_Value And Format(Cells(j, "F"), "yyyymmdd") <= LastDate_Value) Then
     Rows(j).Delete
   End If
Next

變更基準日(BaseDate)即可動態計算 3 個月

1
小魚
iT邦大師 1 級 ‧ 2019-05-24 13:32:42
beforemonth1 <= Cells(j, "Z") <= beforemonth3

這我覺得應該要分開寫

beforemonth1 <= Cells(j, "Z") AND Cells(j, "Z") <= beforemonth3

印象中程式是不能這樣執行的,
我猜你原本的程式會先執行

beforemonth1 <= Cells(j, "Z")

得到的結果會是0或1,
0是false, 1是true,
最後再執行

1 <= beforemonth3

所以永遠都是對的.
而且你好像寫顛倒了,
beforemonth3不是應該放前面嗎?
所以應該是

beforemonth3 <= Cells(j, "Z") AND Cells(j, "Z") <= beforemonth1
看更多先前的回應...收起先前的回應...

可以了 原來要分開 還好有邦友 告知 不過為甚麼 若照JAVA 可以不用分開 VBA就必須分開 明明以我們數學來看 可以成立

小魚 iT邦大師 1 級 ‧ 2019-05-24 15:14:36 檢舉

JAVA不用分開喔?
我沒寫JAVA這我就不清楚了,
(不過你真的確定結果不是跟我上面說的一樣嗎?)
有些程式語言是不允許這樣寫的..
連編譯都不會過.

真的可以執行了

ant1017 iT邦新手 2 級 ‧ 2019-05-24 15:31:05 檢舉

JAVA不用分開嗎= =?
我寫那麼久還真沒用過這方式...

froce iT邦大師 1 級 ‧ 2019-05-24 15:51:00 檢舉

不用分開的目前我只看到python,VBA不太可能不用分開。
JAVA沒在寫不知道。

我要發表回答

立即登入回答