大选择的VBA语法

时间:2019-06-20 16:03:26

标签: excel vba

我有一张桌子,每天定期向其中添加数据。有时需要一个新的数据行,所以我插入了一个新行。然后,我在表上使用VLOOKUPS插入列的数据。

但是,当我在整个列中携带VLOOKUP公式时,每个单元格的格式与具有原始公式的单元格相同。

我必须选择每一行并跳过一行,然后选择所需的格式。与其选择每一行,然后将格式更改为不突出显示或突出显示蓝色,我决定编写一个宏,该宏将选择表中的所有偶数行并格式化不突出显示的行(所有奇数行都突出显示为蓝色)。

由于行的集合如此之大,如何保存我的VBA代码以使其适合可读格式,而不是较长的水平列表?

请在下面查看我的代码和错误:

Range("E12:W12,E14:W14,E16:W16,E18:W18,E20:W20,E22:W22,E24:W24,E26:W26,E28:W28, _
    E30:W30,E32:W32,E34:W34,E36:W36,E38:W38,E40:W40, _
    E42:W42,E44:W44,E46:W46,E48:W48,E50:W50,E52:W52,E54:W54,E54:W54,E56:W56,E58:W58, _
    E60:W60,E62:W62,E64:W64,E66:W66,E68:W68,E70:W70,E72:W72,E74:W74,E76:W76,E78:W78, _
    E80:W80,E82:W82,E84:W84,E86:W86,E88:W88,E90:W90,E92:W92").Select

With Selection.Interior
        .Pattern = xlNone
        .TintAndShade = 0
        .PatternTintAndShade = 0

ERROR: Syntax Error

4 个答案:

答案 0 :(得分:3)

不清楚您想从该代码中实现什么...为这些范围创建公式,或为表格设置格式...可能有助于阐明您的问题。

同时查看是否有帮助:

Sub loopnsub()
Dim ws As Worksheet: Set ws = ActiveWorkbook.Sheets("Sheet1")
Dim lRow As Long: lRow = ws.Cells(Rows.Count, "E").End(xlUp).Row
Dim R As Long

For R = 12 To lRow Step 2
    With ws.Range(ws.Cells(R, "E"), ws.Cells(R, "W"))
        .Interior = 65535 'set your background colour here
        .Font.Color = -16776961 'set your font colour here
        'etc, etc, etc
    End With
Next R

End Sub

答案 1 :(得分:1)

类似的事情应该对您有用:

Sub tgr()

    'Change these as necessary
    Const StartRow As Long = 12
    Const FinalRow As Long = 92
    Const RangeCols As String = "E:W"
    Const RowStep As Long = 2

    Dim ws As Worksheet
    Dim rFormula As Range
    Dim i As Long

    Set ws = ActiveWorkbook.ActiveSheet
    Set rFormula = Intersect(ws.Columns(RangeCols), ws.Rows(StartRow))

    For i = StartRow + RowStep To FinalRow Step RowStep
        Set rFormula = Union(rFormula, Intersect(ws.Columns("E:W"), ws.Rows(i)))
    Next i

    'Do something with your created range
    rFormula.Formula = "=""MyFormulaHere"""

End Sub

答案 2 :(得分:1)

每个新行都必须以"开头,在下划线之前,您需要一个" &来连接这样的大字符串。但是,我认为这仍然行不通,因为您仍然可以定义一个范围很长的范围地址,因为有些undocumented limitations to the length of the string可以传递给Range,并且构造超出了此限制。

请注意,您实际上可以构建超出此限制的范围(例如,使用Union),但是如果您随后查询该范围的Address,则根据上述问题,它将被默默地截断。 / p>

Dim addr As String

addr = "E12:W12,E14:W14,E16:W16,E18:W18,E20:W20,E22:W22,E24:W24,E26:W26,E28:W28," & _
    "E30:W30,E32:W32,E34:W34,E36:W36,E38:W38,E40:W40," & _
    "E42:W42,E44:W44,E46:W46,E48:W48,E50:W50,E52:W52,E54:W54,E54:W54,E56:W56,E58:W58," & _
    "E60:W60,E62:W62,E64:W64,E66:W66,E68:W68,E70:W70,E72:W72,E74:W74,E76:W76,E78:W78," & _
    "E80:W80,E82:W82,E84:W84,E86:W86,E88:W88,E90:W90,E92:W92"

Debug.Print Len(addr)  '### This will print 335, versus the limit of 255

因此,即使您正确地构造了地址(上面),也仍然无法使用它来定义范围。

最终,应该通过Union或逐步迭代来处理。但目前尚不清楚您需要如何处理这些范围。

答案 3 :(得分:0)

只要与您的原始帖子尽可能接近:合并范围怎么办

Union(Range("E12:W12,E14:W14,E16:W16,E18:W18,E20:W20,E22:W22,E24:W24,E26:W26,E28:W28," & _
    "E30:W30,E32:W32,E34:W34,E36:W36,E38:W38,E40:W40," & _
    "E42:W42,E44:W44,E46:W46,E48:W48,E50:W50,E52:W52,E54:W54,E54:W54,E56:W56,E58:W58"), _
    Range("E60:W60,E62:W62,E64:W64,E66:W66,E68:W68,E70:W70,E72:W72,E74:W74,E76:W76,E78:W78," & _
    "E80:W80,E82:W82,E84:W84,E86:W86,E88:W88,E90:W90,E92:W92")).Select

旁注:

您在OP中的版本以"结尾,虚线必须至少反映必须由&连接的完整字符串部分,并且您必须注意255个字符的限制@BigBen提到。另一种方法是将范围部分输入几个单元格并将它们连接起来。

考虑避免使用.Select,因为在大多数情况下不需要使用public partial class Test { [JsonProperty("removed")] public Removed Removed { get; set; } } public partial class Removed { [JsonProperty("series")] public List<long> Series { get; set; } [JsonProperty("unit")] public List<long> Unit { get; set; } } ,并且可以直接解决。