列属性可以与多个列的数字一起使用吗?

时间:2016-09-29 15:42:38

标签: excel vba excel-vba

我正在尝试自学excel VBA,并且通常可以在网络上找到问题的解决方案,特别是这个论坛。但不是在这种情况下。

要求很简单 - 清除几个完整列的内容。

我的第一个代码

Dim home As Range, away As Range
Dim team As Variant
Dim srng As Range 'general range variable
Dim i As Integer, r As Integer
Dim c(1 To 4) As Integer 'Array with 4 variables
Dim lastrow As Long
Dim ws As Worksheet
Set ws = Worksheets("Fixtures")

'Finds last row even if clear contents & blank rows
lastrow = ws.Columns("A").Cells.Find("*", SearchOrder:=xlByRows, _
LookIn:=xlValues, SearchDirection:=xlPrevious).Row

Set home = ws.Range(Cells(1, 2), Cells(lastrow, 2)) 'Range imported from web
Set away = ws.Range(Cells(1, 4), Cells(lastrow, 4)) 'Range imported from web

'Clear output range
ws.Columns("F:I").ClearContents
'Write header
ws.Cells(1, 6) = "Home Fixtures"
i = 2
With ws
    For Each team In home
        Select Case team.Value
            Case "Charnwood 1", "Charnwood 2", "Charnwood 3", "Charnwood 4"
                r = team.Row
                .Range(Cells(r, 1), Cells(r, 4)).Copy .Cells(i, 6)
                i = i + 1
        End Select
    Next

使用ws.Columns("F:I").ClearContents

然后我想使用数字格式来保持一致性,以便稍后更改循环中的行和列。

 'Clear output range
 ws.Columns(6, 9).ClearContents

此返回的运行时错误'1004' Application defined or object-defined error

我最终使用

'Clear output range
ws.Range(Cells(1, 6), Cells(lastrow, 9)).ClearContents

如果在.columns之后无效,我已经尝试了我能想到的每种数字格式。

使用.columns有没有办法做到这一点?

1 个答案:

答案 0 :(得分:3)

range(columns(1),columns(10)).ClearContents