VBA Excel 从日期中获取日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18579536/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 23:06:52  来源:igfitidea点击:

VBA Excel Getting the Day from a Date

excelvbadateformatdayofweek

提问by cwiggo

I have the following VBA Code that generates a list of dates, what I want is as I print out every date, to print the date in the format of day i.e. 01/10/2002 would be Monday:

我有以下生成日期列表的 VBA 代码,我想要的是打印每个日期,以日期格式打印日期,即 01/10/2002 将是星期一:

Sub loopDates()
    Dim Stdt    As Date
    Dim Edt     As Date
    Dim n       As Date
    Dim c       As Long
    Stdt = Range("A1")
    Edt = Range("A2")
    For n = Stdt To Edt
            c = c + 1
            Range("C" & c) = n
            Range("B" & c).Formula = "=TEXT(C1,""dddd"")"
    Next n
End Sub

I want to change the line:

我想改变这一行:

Range("B" & c).Formula = "=TEXT(C1,""dddd"")"

So that C1 in this example is change every time the loop is iterated, so it would change with the value c, for example it would look like =TEXT(C " & c &, "dddd") but I cannot get the formatting right,

所以这个例子中的 C1 在每次循环迭代时都会改变,所以它会随着值 c 改变,例如它看起来像 =TEXT(C " & c &, "dddd") 但我无法正确格式化,

Can anyone propose a solution to this problem?

任何人都可以提出解决这个问题的方法吗?

采纳答案by cwiggo

I've managed to find from herethat you can just set the format of the cell. I changed the line mentioned in the original post to:

我设法从这里找到您可以设置单元格的格式。我将原始帖子中提到的行更改为:

Range("B" & c) = Format(n, "dddd")

This takes the date from column and index of C and prints the day in the right format to the column and index of B

这从 C 的列和索引中获取日期,并以正确的格式将日期打印到 B 的列和索引

Thanks for answering

谢谢回答