Excel Vba工作表数据更改

时间:2016-11-29 14:39:55

标签: excel vba excel-vba

**** *** EDIT

我已将我的日期更新为字符串,现在我遇到一个问题,将DayNum值作为整数重新获取。我的if if else声明一直在下降到星期六。

我不确定这是否可行。但我正在尝试根据它的哪一天更改我从哪个工作表中提取数据。 “天”是变量。

我遇到麻烦的代码行重复是

Worksheets("Daily Info").Range("D24").Value = Worksheets(Day).Range("AB34").Value

工作表(日)是问题,我希望它在sun-sat之间改变

我的工作表名为sun-sat。我需要数据的单元格在工作表之间是相同的,但我需要根据会议更新打印输出的日期将数据拉到另一张表。

       DayNum = Weekday(Today)
   If DayNum = 1 Then
        Day = "Sun"
    ElseIf DayNum = 2 Then
        Day = "Mon"
    ElseIf DayNum = 3 Then
        Day = "Tues"
    ElseIf DayNum = 4 Then
        Day = "Wed"
    ElseIf DayNum = 5 Then
        Day = "Thur"
    ElseIf DayNum = 6 Then
        Day = "Fri"
    Else: Day = "Sat"

    End If

    Worksheets("Daily Info").Range("D24").Value = Worksheets(Day).Range("AB34").Value
    Worksheets("Daily Info").Range("D25").Value = Worksheets(Day).Range("AB27").Value
    Worksheets("Daily Info").Range("D26").Value = Worksheets(Day).Range("AB31").Value
    Worksheets("Daily Info").Range("D27").Value = Worksheets(Day).Range("AB37").Value
    Worksheets("Daily Info").Range("D28").Value = Worksheets(Day).Range("AB8").Value
    Worksheets("Daily Info").Range("D29").Value = Worksheets(Day).Range("AB3").Value
    Worksheets("Daily Info").Range("D30").Value = Worksheets(Day).Range("AB20").Value

5 个答案:

答案 0 :(得分:2)

尝试在if区块中的日期名称周围添加引号。

像这样:

If DayNum = 1 Then
    Day = "Sun"
ElseIf DayNum = 2 Then
    Day = "Mon"
ElseIf DayNum = 3 Then
    Day = "Tues"
ElseIf DayNum = 4 Then
    Day = "Wed"
ElseIf DayNum = 5 Then
    Day = "Thur"
ElseIf DayNum = 6 Then
    Day = "Fri"
Else: Day = "Sat"

End If

答案 1 :(得分:1)

您似乎忘记将日期初始化为字符串。

Dim Day As String
Dim DayNum As String
   DayNum = Weekday(Today)
   If DayNum = 1 Then
        Day = "Sun"
    ElseIf DayNum = 2 Then
        Day = "Mon"
    ElseIf DayNum = 3 Then
        Day = "Tues"
    ElseIf DayNum = 4 Then
        Day = "Wed"
    ElseIf DayNum = 5 Then
        Day = "Thur"
    ElseIf DayNum = 6 Then
        Day = Fri
    Else: Day = "Sat"

    End If

    Worksheets("Daily Info").Range("D24").Value = Worksheets(Day).Range("AB34").Value
    Worksheets("Daily Info").Range("D25").Value = Worksheets(Day).Range("AB27").Value
    Worksheets("Daily Info").Range("D26").Value = Worksheets(Day).Range("AB31").Value
    Worksheets("Daily Info").Range("D27").Value = Worksheets(Day).Range("AB37").Value
    Worksheets("Daily Info").Range("D28").Value = Worksheets(Day).Range("AB8").Value
    Worksheets("Daily Info").Range("D29").Value = Worksheets(Day).Range("AB3").Value
    Worksheets("Daily Info").Range("D30").Value = Worksheets(Day).Range("AB20").Value

答案 2 :(得分:1)

这是获取Day字符串的一种非常简单的方法:

Sub dural()
    Dim Day As String
    Day = LCase(Format(Date, "ddd"))
    Worksheets("Daily Info").Range("D24").Value = Worksheets(Day).Range("AB34").Value
End Sub

这假设标签名都是小写。

答案 3 :(得分:1)

看来你正在使用没有引号的字符串。当你设置Day时,它应该是工作表的名称,因此" Tues"不是周二。

工作日(今天)也不会给你正确的输出,请尝试工作日(现在)。

Sub GatherData()

Dim Day As String
Dim DayNum As String
   DayNum = Weekday(Now)
   If DayNum = 1 Then
        Day = "Sun"
    ElseIf DayNum = 2 Then
        Day = "Mon"
    ElseIf DayNum = 3 Then
        Day = "Tues"
    ElseIf DayNum = 4 Then
        Day = "Wed"
    ElseIf DayNum = 5 Then
        Day = "Thur"
    ElseIf DayNum = 6 Then
        Day = "Fri"
    Else: Day = "Sat"

    End If

    Worksheets("Daily Info").Range("D24").Value = Worksheets(Day).Range("AB34").Value
    Worksheets("Daily Info").Range("D25").Value = Worksheets(Day).Range("AB27").Value
    Worksheets("Daily Info").Range("D26").Value = Worksheets(Day).Range("AB31").Value
    Worksheets("Daily Info").Range("D27").Value = Worksheets(Day).Range("AB37").Value
    Worksheets("Daily Info").Range("D28").Value = Worksheets(Day).Range("AB8").Value
    Worksheets("Daily Info").Range("D29").Value = Worksheets(Day).Range("AB3").Value
    Worksheets("Daily Info").Range("D30").Value = Worksheets(Day).Range("AB20").Value
End Sub

答案 4 :(得分:0)

与已经解释的其他评论一样,您需要使用打包Strings

您目前正在StringInteger中的if-statments进行比较,导致您的if结构始终转到else声明。< / p>

无论如何,我还建议您使用Select Case Statment代替重复If Then ElseIf Then

Dim Day As String
Select Case Weekday(Now)
    Case 1: Day = "Sun"
    Case 2: Day = "Mon"
    Case 3: Day = "Tues"
    Case 4: Day = "Wed"
    Case 5: Day = "Thur"
    Case 6: Day = "Fri"
    Case Else: Day = "Sat"
End Select

....

由于您的DayNumString,您必须按照以下方式进行比较:

If DayNum = "1" Then

或者您只是将DayNum声明为Integer,因为return value的{​​{1}}无论如何都是weekday()