使用VBA获取上个月的数据 [英] getting data for last month using VBA
问题描述
Sub currentm()
Dim i As Long,r As Long,l As Long,cool As Integer,mo As Integer,k As Integer,yr As Integer
cool =表格("Sheet2")。UsedRange.Columns.Count
r = WorksheetFunction.CountA(表格("Sheet2")。范围("B:B"))
年=年(日期)
mo =月(日期)
l = 1
对于i = 1至r $
'如果月(单元格(i,5))= mo - 1和年份(单元格(i,5))= yr然后
如果i = 1或(Cells(i,5)< = Date - Day(Date)And Cells(i,5)> = DateSerial(Year(Date - Day(Date)),Month(Date - Day(Date) )),1))。然后
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;
适用于k = 1要冷却¥b $ b
细胞(L,K)=表(" Sheet2的")。细胞(I,K)
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;&NBSP;&NBSP;&NBSP;&NBSP;
下一页k
l = l + 1
结束如果是
接下来我是
结束子
Sub currentm()
Dim i As Long, r As Long, l As Long, cool As Integer, mo As Integer, k As Integer, yr As Integer
cool = Sheets("Sheet2").UsedRange.Columns.Count
r = WorksheetFunction.CountA(Sheets("Sheet2").Range("B:B"))
yr = Year(Date)
mo = Month(Date)
l = 1
For i = 1 To r
' If Month(Cells(i, 5)) = mo - 1 And Year(Cells(i, 5)) = yr Then
If i = 1 Or (Cells(i, 5) <= Date - Day(Date) And Cells(i, 5) >= DateSerial(Year(Date - Day(Date)), Month(Date - Day(Date)), 1)) Then
For k = 1 To cool
Cells(l, k) = Sheets("Sheet2").Cells(i, k)
Next k
l = l + 1
End If
Next i
End Sub
推荐答案
您好
您可以尝试以下方法获取
You can try the following to get
DateAdd ("m",-1, Now())
干杯
Shasur
这篇关于使用VBA获取上个月的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!