VBA 编译错误:预期的命名参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19817189/
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-12 00:18:46  来源:igfitidea点击:

VBA Compile Error: Expected Named Parameter

excel-vbavbaexcel

提问by Matt

I am new to VBA's and have pieced together this macro from various searches on "how to filter between two specific dates in excel." I am trying to have it read the dates from two cells and restrict the shown data to data between those dates. The input data to the workbook is a SQL table that will be growing over time, so I need the left bound of the range to be the last row with a value in it. Each line in my AutoFilter part returns an "Expected Named Parameter" error with the := sign highlighted. From the forums I have read this is due to using VBA reserved words as variable names, but I not think that is the case in this instance.
Here is my code:

我是 VBA 的新手,并从“如何在 excel 中的两个特定日期之间进行过滤”的各种搜索中拼凑了这个宏。我试图让它从两个单元格中读取日期并将显示的数据限制为这些日期之间的数据。工作簿的输入数据是一个 SQL 表,它会随着时间的推移而增长,因此我需要范围的左边界是其中包含值的最后一行。我的 AutoFilter 部分中的每一行都返回一个“预期命名参数”错误,其中突出显示了 := 符号。从我读过的论坛中,这是由于使用 VBA 保留字作为变量名,但我认为在这种情况下并非如此。
这是我的代码:

Public Sub MyFilter()
.AutoFilterMode = False
Dim datRight, datLeft As Date
Dim lastRow As Long
datLeft = Range("J1").Value
datRight = Range("J2").Value
lastRow = Range("A:A").Find("*", Range("A2"), searchdirection:=xlPrevious).Row
ActiveSheet.Range("F2:F" & lastRow).AutoFilter Field:=7,
Criteria1:=">=" & datLeft, _
Operator:= xlAnd,
Criteria2:="<=" & datRight, VisibleDropDown:=True
End Sub

采纳答案by Siddharth Rout

  1. The first error you will get is at the line .AutoFilterMode = FalseNotice the DOT before Autofilter. You have to qualify it with the relevant sheet. For example, ThisWorkbook.Sheets("Sheet1").AutoFilterMode = False

  2. When you are trying to find the lastrow using *, Always use the method as mentioned in THISpost else you will get an error if the worksheet is blank.

  3. Your declaration Dim datRight, datLeft As Date. In VBA only the last variable will be declared as Dateand the first one will be declared as Variant. Change it to Dim datRight As Date, datLeft As DateAlso if the J1and J2values are not date values then you will get an error.

  1. 您将得到的第一个错误是在.AutoFilterMode = FalseAutofilter 之前注意 DOT 行。您必须使用相关表格对其进行限定。例如,ThisWorkbook.Sheets("Sheet1").AutoFilterMode = False

  2. 当您尝试使用查找LASTROW *,始终使用一种方法,在提到岗位否则如果工作表是空白的,你会得到一个错误。

  3. 你的宣言Dim datRight, datLeft As Date。在 VBA 中,只有最后一个变量将被声明为Date,第一个将被声明为Variant. Dim datRight As Date, datLeft As Date如果J1J2值不是日期值,则将其更改为Also ,那么您将收到错误消息。

Now to your problem. You are getting that error because you the missing the continuation character _

现在解决你的问题。您收到该错误是因为您缺少延续字符_

Try this

尝试这个

ActiveSheet.Range("F2:F" & lastRow).AutoFilter Field:=7, _
Criteria1:=">=" & datLeft, _
Operator:=xlAnd, _
Criteria2:="<=" & datRight, VisibleDropDown:=True