简体   繁体   English

VBA代码从给定日期获得周数

[英]VBA code to get week number from given date

I am working with Excel 2010. 我正在使用Excel 2010。

I wish to convert a given date from the format mm/dd/yyyy to the format Wyy"weeknumber" 我希望将给定日期从格式mm/dd/yyyy转换为格式Wyy"weeknumber"

For example, 4/10/2017 would become W1715 , since it is week 15 of 2017. 例如, 4/10/2017将成为W1715 ,因为它是一周的2017年15。

The below shown image is of the excel table I am working on. 下面显示的图像是我正在处理的excel表。 I want to convert the dates in column LT Verification - Planned Date to the week number format mentioned above, in column LT Verification - Planned Week Numbers . 我想将列LT Verification - Planned Date中的LT Verification - Planned Date转换为上面提到的周数格式,在LT Verification - Planned Week NumbersLT Verification - Planned Week Numbers

Edit: Because this is part of a larger VBA process, I need it to be in VBA, not a cell formula. 编辑:因为这是更大的VBA流程的一部分,我需要它在VBA中,而不是单元格公式。

I have written the following code: 我写了以下代码:

Public Sub WeekNumbers()

Dim lastRow As Integer
    lastRow = Range("A1:AZ1").Find("*", , , , xlByRows, xlPrevious).Row

Dim myRange As Range
    Set myRange = Range("A1:AZ1" & lastRow)

Dim myCell As Range
    For Each myCell In myRange

myCell.Offset(0, 1).Value = "W" & Right(Year(myCell.Value), 2) & Application.WorksheetFunction.WeekNum(myCell.Value)**

 Next myCell

 End Sub

This code gives me error at myCell.Offset(0, 1).Value = "W" & Right(Year(myCell.Value), 2) & Application.WorksheetFunction.WeekNum(myCell.Value) 此代码在myCell.Offset(0, 1).Value = "W" & Right(Year(myCell.Value), 2) & Application.WorksheetFunction.WeekNum(myCell.Value)给出了错误myCell.Offset(0, 1).Value = "W" & Right(Year(myCell.Value), 2) & Application.WorksheetFunction.WeekNum(myCell.Value)

Here I have a excel workbook which will be updated every week. 在这里,我有一个excel工作簿,每周都会更新。 So, each time it is updated, it runs a macro to import data from another file & perform the week number activity & create a pivot table. 因此,每次更新时,它都会运行一个宏来从另一个文件导入数据并执行周数活动并创建一个数据透视表。

So, the sheet name changes every week. 因此,工作表名称每周都会更改。 Also, the column headers may be in different columns in different weeks. 此外,列标题可能在不同的周内位于不同的列中。 Also, the number of rows may also change every week. 此外,行数也可能每周都会改变。

So, I need to specify column & row range dynamically based on that weeks data. 因此,我需要根据周数据动态指定列和行范围。 And have the week numbers in the column based on the column header rather than the column name (A or B or Z...) 并根据列标题而不是列名称(A或B或Z ...)列中的周数

Excel表

This can be achieved easily with a cell formula: 使用单元格公式可以轻松实现:

="W" & RIGHT(YEAR(A1),2) & WEEKNUM(A1)

Where A1 can be replaced by the cell containing the date. A1可以由包含日期的单元格替换。

In VBA this is equivalent to 在VBA中,这相当于

With Thisworkbook.Sheets("Sheet1")
    .Range("A2").Value = "W" & Right(Year(.Range("A1").Value), 2) & Application.WorksheetFunction.WeekNum(.Range("A1").Value)
End With

Edit : 编辑

To fill an entire range, you could loop over the cells, apply the VBA calculation as above. 要填充整个范围,您可以循环遍历单元格,如上所述应用VBA计算。

Dim myRange as Range
Set myRange = Thisworkbook.Sheets("Sheet1").Range("A1:A10")

Dim myCell as Range
For Each myCell in myRange
    myCell.Offset(0,1).Value = "W" & Right(Year(myCell.Value), 2) & Application.WorksheetFunction.WeekNum(myCell.Value)
Next myCell

There are many methods for finding the last row in a range , so I'll leave that to you if you don't know your range. 很多方法可以找到范围中的最后一行 ,所以如果你不知道你的范围 ,我会把它留给你。


Edit 2 : in response to your error edit. 编辑2 :响应您的错误编辑。

You have used the following line to define your range: 您已使用以下行来定义范围:

Set myRange = Range("A1:AZ1" & lastRow)

Let's imaging you have lastRow = 20 , you now have 让我们想象你有lastRow = 20 ,你现在有了

myRange.Address = "A1:AZ120"

Which is clearly wrong, you shouldn't have the 1 after the AZ . 这显然是错误的,你不应该在AZ之后拥有1 Also I don't know why you've gone to column AZ , if all of your date data is in column A , you should use 另外我不知道为什么你去了AZ列,如果你的所有日期数据都在A列,你应该使用

Set myRange = Range("A1:A" & lastRow)

The loop you've implemented uses an offset, so the values in column B are changed to reflect those in column A. You can't then set column C according to column B ! 您实现的循环使用偏移量,因此B列中的值会更改以反映A列中的值。您不能根据B列设置C列!

In VBA , you can get your string by using the Format function. VBA ,您可以使用Format函数获取字符串。 "\\Wyyww" is the format you are looking for, where \\ is used to escape the interpretation of the first W character and to take it as a litteral. "\\Wyyww"是您正在寻找的格式,其中\\用于逃避对第一个W字符的解释并将其视为一个字符。

myCell.Offset(0,1).Value = Format(myCell.Value, "\Wyyww")

More 更多

You have to setup correctly the range for your loop. 您必须正确设置循环的范围。 If your dates are in some column with header "LT Verificiation - Planned Date" , you can try this: 如果您的日期位于标题为"LT Verificiation - Planned Date"某列中,您可以尝试以下操作:

Dim ws As Worksheet
Set ws = ActiveSheet ' <-- you can change this into something explicit like Sheets(someIndex)...

Dim myCell As Range
Set myCell = ws.Rows(1).Find("LT Verificiation - Planned Date")
For Each myCell In ws.Range(myCell.Offset(1), ws.Cells(ws.Rows.Count, myCell.Column).End(xlUp))
    If IsDate(myCell.value) Then myCell.Offset(, 1).value = Format(myCell.value, "\Wyyww")
Next myCell

I don't think you need VBA for this, try this formula: 我不认为你需要VBA,试试这个公式:

=RIGHT(YEAR(A1),2)&WEEKNUM(A1)&"W"

Of course, if you insist on VBA, you can always turn Excel Formulas into VBA code. 当然,如果您坚持使用VBA,则可以随时将Excel公式转换为VBA代码。 In this case: 在这种情况下:

Dim rngInput As Range
Dim rngOutput As Range
With Application.WorksheetFunction
    rngOutput.Value = .Right(.Year(rngInput.Value), 2) & .Weeknum(rngInput.Value) & "W"
End With

Or you may even set the Formula, and Insert the Value, like this 或者您甚至可以设置公式,并插入值,如下所示

Dim rngInput As Range
Dim rngOutput As Range
rngOutput.Formula = "=RIGHT(YEAR(" & rngInput.Address(False, False) & "),2)&WEEKNUM(" & rngInput.Address(False, False) & ")&""W"""
rngOutput.Value = rngOutput.Value

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM