尝试根据单元格的当前值运行特定的宏

时间:2019-07-11 15:00:57

标签: excel vba

我正在尝试设置一个工作簿,该工作簿允许我根据一周中的某一天将一张纸上的单元格中的数据记录到另一张纸上。基本上,它是每周跟踪器,一周的每一天都有单独的工作表。我有单独的宏,可以将所需的数据从“当前”工作表移至一周中的其他日期,并且每个工作都可以按需进行。

编辑:我将此宏绑定到主表上的一个按钮,而不是使用热键或列表。

Sub Lookup()
    Dim day As String

    day = ThisWorkbook.Sheets("Data").Cells(2, "x").Value2

    If day = "Mon" Then
        Call Mon

    ElseIf day = "Tue" Then
        Call Tue

    ElseIf day = "Wed" Then
        Call Wed

    ElseIf day = "Thu" Then
        Call Thu

    ElseIf day = "Fri" Then
        Call Fri
    End If
End Sub

我正在尝试创建一个单独的宏,以查看特定工作表中特定单元格中的数据(使用特定单元格中的“ Now()”函数,其格式设置为仅显示星期几),只希望它查看该数据并运行相应的宏。现在,它会在第一步停止并出现错误

  

“编译错误:预期的函数或变量”

编辑:我意识到,如果我引用了任何一天我手动键入的单元格,它将起作用。因此,看起来宏仅在看公式而不是结果。将单元格中的公式更改为

=TEXT(NOW(),"ddd")

解决了该问题。谢谢您的所有帮助!

2 个答案:

答案 0 :(得分:1)

我的猜测是,您需要对要比较的输出进行格式化才能运行宏。您声明自己在单元格中有tab-pane,例如不会返回<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css" integrity="sha384-9gVQ4dYFwwWSjIDZnLEWnxCjeSWFphJiwGPXr1jddIhOegiu1FwO5qRGvFXOdJZ4" crossorigin="anonymous"> <script src="https://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script> <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js" integrity="sha384-uefMccjFJAIv6A+rW+L4AHf99KvxDjWSu1z9VI8SKNVmz4sk7buKt/6v9KI65qnm" crossorigin="anonymous"></script> </head> <body> <div class="d-flex flex-row" style="min-height: 100vh; max-height: 100vh;"> <div class="d-flex flex-column flex-grow-1"> <nav> <div class="nav nav-tabs" id="nav-tab" role="tablist"> <a class="nav-item nav-link active" id="nav-home-tab" data-toggle="tab" href="#nav-home" role="tab" aria-controls="nav-home" aria-selected="true">Home</a> </div> </nav> <div class="tab-content flex-grow-1" id="nav-tabContent"> <div class="tab-pane fade show active d-flex flex-column h-100" id="nav-home" role="tabpanel" aria-labelledby="nav-home-tab"> <div class="flex-grow-1" style="overflow-y: scroll"> <p>Ethical Kickstarter PBR asymmetrical lo-fi. Dreamcatcher street art Carles, stumptown gluten-free Kickstarter artisan Wes Anderson wolf pug. Godard sustainable you probably haven't heard of them, vegan farm-to-table Williamsburg slow-carb readymade disrupt deep v. Meggings seitan Wes Anderson semiotics, cliche American Apparel whatever. Helvetica cray plaid, vegan brunch Banksy leggings +1 direct trade. Wayfarers codeply PBR selfies. Banh mi McSweeney's Shoreditch selfies, forage fingerstache food truck occupy YOLO Pitchfork fixie iPhone fanny pack art party Portland. Ethical Kickstarter PBR asymmetrical lo-fi. Dreamcatcher street art Carles, stumptown gluten-free Kickstarter </p> </div> </div> </div> </div> </div> </body> </html>。因此,您可以使用=NOW()方法来获取所需的输出。

这是一种验证X2单元格中的内容实际上是否为日期,以及该日期是否在周一至周五(不包括周末)的方法,因此您可以通过将其传递给Application.Run来调用周一至周五宏。 :

Mon

答案 1 :(得分:0)

您可以将字符串值传递给Application.Run,以便将代码缩短为

Sub Lookup()
    Dim day As String

    day = ThisWorkbook.Sheets(8).Cells(2, "x").Value

    Application.Run day
End Sub

,然后用您的Sheets(8).cells(2,"x")将数据验证添加到列表值为Mon, Tue, Wed, Thu, Fri或您的宏名称为任意值的单元格中,以便用户只能从定义的选项列表中进行选择。

此外,您需要显式引用工作表,而不是引用其位置。

day = ThisWorkbook.Sheets(8).Cells(2, "x").Value

成为

day = ThisWorkbook.Sheets("YourSheetNameHere").Cells(2, "x").Value2

或在您的宏中处理

Sub Lookup()
    Dim day As String
    Dim ValidSubs As Variant
    Dim RunSub As Boolean: RunSub = False
    Dim c

    ValidSubs = Array("Mon", "Tue", "Wed", "Thu", "Fri")

    day = ThisWorkbook.Sheets(8).Cells(2, "x").Value

    For Each c In ValidSubs
        If c = day Then
            RunSub = True
            Exit For
        End If
    Next c

    If RunSub Then
        Application.Run day
    Else
        MsgBox "You have selected an invalid choice - only the following are allowed:" & vbNewLine & Join(ValidSubs, vbNewLine)
    End If
End Sub