[英]Formatting a cell value to “MMMM” using excel vba
I have the below code that I need some assistance modifying. 我有以下代码,需要一些帮助进行修改。
Sub CopyDataBasedOnTimeRangeMonth()
Dim i, LastRow
Dim Cell As Range
LastRow = Sheets("OPA").Range("A" & Rows.Count).End(xlUp).Row
Sheets("Sheet2").Range("A3:U500").ClearContents
For i = 2 To LastRow
If Sheets("OPA").Cells(i, "G").Value >= Range("U1") And Sheets("OPA").Cells(i, "G").Value < Range("AC1") Then
Sheets("OPA").Cells(i, "R").EntireRow.Copy Destination:=Sheets("Sheet2").Range("A" & Rows.Count).End(xlUp).Offset(1)
End If
Next i
End Sub
For the calculation of "G" I want to use the format of "MMMM" of the value being calculated. 对于“ G”的计算,我要使用要计算的值的“ MMMM”格式。 In an excel formula I can use something like Text("G12","MMMM") and then continue with the formula but I don't know how to modify the about code to just to use the Month only value of "G".
在一个excel公式中,我可以使用诸如Text(“ G12”,“ MMMM”)之类的内容,然后继续使用该公式,但是我不知道如何修改about代码以仅使用Month only值“ G”。
Thanks in advance for any help you can provide. 在此先感谢您提供的任何帮助。
最好也将[U1]作为日期,然后将其格式化为月
If Month(Sheets("OPA").Cells(i, "G")) >= Month(Range("U1"))
you can see below the variant of your code, which has been updated using variant provided by Davesexcel
(+1), and also some correction from my side, just to simplify readability: 您可以在下面看到代码的变体,该变体已使用
Davesexcel
(+1)提供的变体进行了更新,并且在我这一边也做了一些更正,以简化可读性:
1) absolute reference to Range()
replaced to []
shorthand method; 1)将对
Range()
)的绝对引用替换为[]
速记方法;
2) removed Destination:=
as excessive, also destination range replaced by row, because when you copy the row then destination shall be the row; 2)删除了
Destination:=
过多,目标范围也被行替换,因为当您复制行时,目标应为该行;
3) applied with (object)
method; 3)应用
with (object)
方法;
4) added type of the variables, eg i
replaced by i&
(means i as long
) 4)添加变量的类型,例如,
i
替换为i&
(表示i as long
)
Sub CopyDataBasedOnTimeRangeMonth()
Dim i&, LastRow&, Cl As Range
LastRow = Sheets("OPA").Range("A" & Rows.Count).End(xlUp).Row
Sheets("Sheet2").[A3:U500].ClearContents
With Sheets("OPA")
For i = 2 To LastRow
If Month(.Cells(i, "G")) >= Month(.[U1]) And _
Month(.Cells(i, "G")) < Month(.[AC1]) Then
.Rows(i).Copy Sheets("Sheet2").Rows(Sheets("Sheet2").Range("A" & Rows.Count).End(xlUp).Row + 1)
End If
Next i
End With
End Sub
tested, works fine. 经过测试,工作正常。
source: 资源:
destination: 目的地:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.