VBA新手:编译错误无效限定符.

Tho*_*mas 1 excel vba excel-vba

此代码旨在获取原始数据的电子表格,省略多个列,并重新格式化剩余的内容.我欢迎对代码的任何批评,因为我是VBA的新手并且一无所知.关键问题是在下面用astriscs指示的行代码的末尾.这是出现"编译错误:无效限定符"的地方.我正在尝试将格式应用于列B和F,但我只希望它到最后一行日期.最后一行数据将从一张纸到另一张不同.

触发错误时,调试器会突出显示"count"一词.

在此先感谢您的帮助.

Sub Macro2()
'
' Macro2 Macro
'
Union(Range("A:A"), Range("F:F"), Range("K:Q"), Range("S:V")).Delete
Range("A1").Select
ActiveCell.FormulaR1C1 = "FIRST"
Range("B1").Select
ActiveCell.FormulaR1C1 = "LAST"
Range("C1").Select
ActiveCell.FormulaR1C1 = "G"
Range("D1").Select
ActiveCell.FormulaR1C1 = "PHONE"
Range("E1").Select
ActiveCell.FormulaR1C1 = "ADDRESS"
Range("F1").Select
ActiveCell.FormulaR1C1 = "CITY"
Range("G1").Select
ActiveCell.FormulaR1C1 = "STATE"
Range("H1").Select
ActiveCell.FormulaR1C1 = "ZIP"
Range("I1").Select
ActiveCell.FormulaR1C1 = "MONTH"
Range("J1").Select
ActiveCell.FormulaR1C1 = "YEAR"
Columns("e:h").Insert Shift:=xlToRight
Columns("A:B").ColumnWidth = 12
Columns("C:C").ColumnWidth = 2
Columns("D:d").ColumnWidth = 13
Columns("e:e").ColumnWidth = 0.38
Columns("F:F").ColumnWidth = 5
Columns("G:G").ColumnWidth = 11
Columns("H:H").ColumnWidth = 0.38
Columns("I:N").ColumnWidth = 14
**Union(Range("B:B"),Range("F:F")).Rows.Count.End(xlUp).Row**
Range("B1").Activate
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorAccent5
.TintAndShade = 0.599993896298105
.PatternTintAndShade = 0
End With


End Sub 
Run Code Online (Sandbox Code Playgroud)

Ton*_*ore 5

当我对通过字符串属性创建的对象类型感到困惑时,我使用了这种技术.

在Excel的Visual Basic编辑器中,如果您没有希望以这种方式使用的现有模块,请创建一个新模块.如果在Project Explorer中选择模块并单击F4,则可以将模块的名称更改为"Experiments".

输入或复制:

Option Explicit
Sub TestA()

End Sub
Run Code Online (Sandbox Code Playgroud)

总是用我的模块开始Option Explicit.Option Explicit在VBA帮助中查找,它会告诉您为什么这是一个好主意.

我还创建了一个空的子例程,我将在其中键入一些语句.

开始输入新语句,这样您就拥有:

Sub TestA()

  Debug.Print Range("B:B").

End Sub
Run Code Online (Sandbox Code Playgroud)

在此新行末尾键入句点时,弹出窗口将显示可用的方法和属性.该列表将按预期显示Range的所有方法和属性.输入"地址"或从列表中选择地址以获取:

Sub TestA()

  Debug.Print Range("B:B").Address

End Sub
Run Code Online (Sandbox Code Playgroud)

单击F5以运行此宏,以下将显示在立即窗口中:

$B:$B
Run Code Online (Sandbox Code Playgroud)

这是B列中所有行的地址,这是您所期望的.

现在向宏添加两个进一步的语句:

  Debug.Print Range("F:F").Address
  Debug.Print Union(Range("B:B"), Range("F:F")).Address
Run Code Online (Sandbox Code Playgroud)

再次运行此宏,您将获得:

$B:$B
$F:$F
$B:$B,$F:$F
Run Code Online (Sandbox Code Playgroud)

这也是预期的结果.

现在添加:

  Debug.Print Union(Range("B:B"), Range("F:F")).Rows.
Run Code Online (Sandbox Code Playgroud)

出现的弹出窗口将保持不变,因为Range.Rows它仍然是一个范围.

通过添加或选择"地址"完成该语句并再次运行宏以获取:

$B:$B
$F:$F
$B:$B,$F:$F
$B:$B,$F:$F
Run Code Online (Sandbox Code Playgroud)

这可能不是你的预期,但想一想. $B:$B,$F:$F是列B和F中的所有行,因此添加属性Rows不会更改地址.

现在将以下语句添加到宏:

  Debug.Print Union(Range("B:B"), Range("F:F")).Count
  Debug.Print Union(Range("B:B"), Range("F:F")).Rows.Count
Run Code Online (Sandbox Code Playgroud)

运行宏,这些语句将输出一个整数.我正在使用Excel 2003,所以我得到:

 131072 
 65536 
Run Code Online (Sandbox Code Playgroud)

如果您使用的是更高版本的Excel,则会得到更大的整数.第二个整数是Excel版本的工作表中的行数.第一个整数是您的Excel版本的工作表的两列中的单元格数.

现在添加:

  Debug.Print Union(Range("B:B"), Range("F:F")).Rows.Count.
Run Code Online (Sandbox Code Playgroud)

键入最后一个句点时,不会出现弹出窗口,因为整数没有可以通过这种方式选择的方法或属性.方法.End(xlUp)在一个范围内运行; 它不是Count你获得"无效限定符"的原因.

在将属性串联起来时,很容易让自己感到困惑.我个人避免使用字符串属性,因为即使它运行得更快,我理解和调试也需要更长的时间.在某些情况下,最小化运行时间是最优先考虑的问题,但这是其中一种情况吗?你用这种方法浪费了多少小时?

考虑:

  Dim Rng1 As Range
  Dim Rng2 As Range
  Dim Rng3 As Range
  Dim RowMax As Long

  Set Rng1 = Range("B:B")
  Set Rng2 = Range("F:F")
  Set Rng3 = Union(Rng1, Rng2)
  RowMax = Rng3.Count

  Debug.Print RowMax

  Debug.Print Rng3.Find("*", Range("B1"), xlValues, xlWhole, xlByRows, xlPrevious).Row
Run Code Online (Sandbox Code Playgroud)

你不需要,RowMax但我已经包含它,所以你绝对清楚什么Rng3.Count回报.我也用范围去了OTT.我很乐意输入:Set Rng3 = Union(Range("B:B"), Range("F:F"))因为我觉得它很容易理解.

方法.End(xlUp)对单元格进行操作. MultiCellRange.End(xlUp).Row是有效的语法,但我不能让它返回有用的信息.如果你想使用.End(xlUp)考虑:

  Dim RowMaxColB As Long
  Dim RowMaxColF As Long

  RowMaxColB = Cells(Rows.Count, "B").End(xlUp).Row
  RowMaxColF = Cells(Rows.Count, "F").End(xlUp).Row
Run Code Online (Sandbox Code Playgroud)

我同意Siddharth,Find在这种情况下似乎是最好的方法.但是,你应该看看我的这个答案,/sf/answers/1459491281/,以解决另一个问题.它包含一个宏,它演示了一系列查找最后一行和列的方法,并显示了它们失败的情况.