iT邦幫忙

1

抓三個月的資料(改寫)

  • 分享至 

  • xImage

根據小魚及這邊的邦友的指教 我成功執行了
以下是我改良 但我覺得還可以再改良 請指教

 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
    i = i + 1
   Else
    Cells(i, "Y") = Year(Cells(i, "F"))
    Cells(i, "Z") = Month(Cells(i, "F"))
   End If
 Next  '我沒修改  若不知道我要做甚麼  請看我發問那篇抓三個月的資料卻沒執行  謝謝
 If thismonth - 1 > 0 Or thismonth - 2 > 0 Or thismonth - 3 > 0 Then  '這個是我演算法自己想到若現在是1月ˋ2月ˋ3月就要抓去年的資料  如果以現在5月來看他3個判斷式都成立
   beforemonth1 = Month(Now) - 1  '上個月  (以5月來說是4月)
   beforemonth3 = Month(Now) - 3  '三個月之前  (以5月來說是2月)
    For j = lastrow To 6 Step -1  '第二個迴圈
    If Cells(j, "Y") = thisyear And beforemonth3 <= Cells(j, "Z") And Cells(j, "Z") <= beforemonth1 Then
    '判斷Y欄位是今年  Z欄位是2月~4月
    Else
      Rows(j).Delete  '若不是刪除整行
   End If
   Next'迴圈結束  這是照小魚邦友指教修改的
   
  ElseIf thismonth - 1 = 0 Then  '如果是1月
   lastyear = thisyear - 1  '去年
   beforemonth1 = Month(Now) + 12 - 1  '12月
   beforemonth3 = Month(Now) + 12 - 3  '10月
   For a = lastrow To 6 Step -1  '迴圈開始
    If Cells(a, "Y") = lastyear And beforemonth3 <= Cells(a, "Z") And Cells(a, "Z") <= beforemonth1 Then
    'Y欄位是去年Z欄位是10月~12月
    Else
      Rows(a).Delete  '刪除整行
    End If
   Next  '迴圈結束
  
  ElseIf thismonth - 2 = 0 Then  若是2月
  lastyear = thistear - 1  '去年
  beforemonth1 = Month(Now) - 1  '今年1月
  beforemonth3 = Month(Now) + 12 - 3  '去年11月
  For b = lastrow To 6 Step -1  迴圈
       If Cells(b, "Y") = lastyear And beforemonth3 >= Cells(b, "Z") Or Cells(b, "y") = thisyear And Cells(a, "Z") = beforemonth1 Then  去年11月到今年1月
    Else
      Rows(b).Delete  '不符合就刪除
    End If
   Next  '迴圈結束
   
    ElseIf thismonth - 3 = 0 Then  '3月的話
  lastyear = thistear - 1  去年
  beforemonth1 = Month(Now) - 1  '今年2月
  beforemonth3 = Month(Now) + 12 - 3  '去年12月
  For C = lastrow To 6 Step -1  '迴圈開始
   If Cells(C, "Y") = lastyear And beforemonth3 = Cells(C, "Z") Or Cells(C, "y") = thisyear And Cells(C, "Z") <= beforemonth1 Then  去年12月~今年2月
   Else
     Rows(C).Delete  '不符合就刪除
   End If
  Next'結束迴圈
  End If  '結束判斷

不知能否再改良

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中
0
paicheng0111
iT邦大師 5 級 ‧ 2019-05-24 23:22:21
最佳解答

我會用dateserial()讓VBA自己去計算日期
大概是這樣寫

dim date1 as date, date2 as date, i as long, lastRow as long

date1 = dateserial(year(now), month(now) - 3, 1)
date2 = dateserial(year(now), month(now) ,1) - 1

for i = lastRow to 1 step -1
    if isdate(cells(i, "F")) Then
        if cells(i, "F") < date1 or cells(i, "F") > date2 then
            rows(i).delete
        end if
    end if
next i

多謝告知 對了 如何選最佳解答

0
froce
iT邦大師 1 級 ‧ 2019-05-24 16:19:54

再改良喔...

import pandas as pd

data = pd.read_excel()
...

我現在看到要用VBA硬幹我就懶。XD

ccutmis iT邦高手 2 級 ‧ 2019-05-24 16:21:23 檢舉

/images/emoticon/emoticon30.gif

2
rogeryao
iT邦超人 8 級 ‧ 2019-05-24 16:21:29

你要的答案在前一篇 "要抓三個月的資料卻沒反應"
我的回答內 "另一種寫法 :"

有看到了 覺得很不錯 多謝

我要發表回答

立即登入回答