如何将此excel范围设置为范围?(发生错误)

时间:2016-08-06 10:27:44

标签: excel vba excel-vba

基本信息:

Function Col_Letter(lngCol As Long) As String ' number to letter function 
Dim vArr
vArr = Split(Cells(1, lngCol).Address(True, False), "$")
Col_Letter = vArr
End Function

 'Worksheets ("button").cells(1,3).value   ' with value 12
    'Worksheets ("button").cells(2,3).value   ' with value 15

我想使用范围为L3:Q10的范围-dayayrng 总结范围 - mandayrng。

Dim mandayrng as range

    mandayrng = Worksheets("manday").Range(cells(Col_Letter(Worksheets ("button").cells(2,3).value),3), cells(Col_Letter(Worksheets ("button").cells(2,3).value),10))'the range is L3: Q10
With Worksheets("report")
row = worksheet.Range("C" & Rows.Count).End(xlUp).row
For k = 6 To row
    Worksheets("report").Cells(i, 6) = Application.WorksheetFunction.sum(mandayrng) 'sum with the range of mandayrng

    Next k
    End With

但是这里的代码不起作用。

如何解决?

3 个答案:

答案 0 :(得分:1)

无需使用Col_Letter。单元格带一个字母或数字。

enter image description here

一个Range可以采用以列字母开头的字符串地址,后跟行索引。如果Range有两个参数,则范围从第一个参数延伸到第二个参数,包括其间的所有单元格。

enter image description here

这些例子都涉及" L3:Q10"

  • Range("L3:Q10")
  • Range("$L$3:$Q$10")
  • Range("L3", "Q10")
  • Range("Q10", "L3")
  • Range(Cells(3,"L"), Cells(10,"Q"))
  • Range(Cells(3, 12), Cells(10, 17))
  • Range(Cells(10,"Q"), Cells(3,"l"))
  • Range(Cells(3, 17), Cells(10, 12))
  • Range("L3", Cells(10, 12))
  • Range(Cells(3, 17), "Q10")

范围不接受单个单元格作为参数(例如Range(Cells(1,1))无效)

范围及其单元格参数必须位于同一工作表中。

如果Worksheets("manday")是ActiveWorksheet,则下面的代码将起作用,因为如果范围不合格,则会自动假定它位于ActiveWorksheet上。

Worksheets("manday").Range(Cells(3,"L"), Cells(10,"Q"))

这是一种解决方法:

Worksheets("manday").Range(Cells(3,"L").address, Cells(10,"Q").address)

这也是有效的

Worksheets("manday").Range(Worksheets("manday").Cells(3,"L"), Worksheets("manday").Cells(10,"Q"))

但我更喜欢这个:

With Worksheets("manday")
    Set mandayrng = .Range(.Cells(3, FirstColumn), .Cells(10, LastColumn))

End With
Dim FirstColumn As Integer
Dim LastColumn As Integer

FirstColumn = Worksheets("button").Cells(2, 3).Value
LastColumn = Worksheets("button").Cells(2, 3).Value

With Worksheets("manday")
    Set mandayrng = .Range(.Cells(3, FirstColumn), .Cells(10, LastColumn))

End With

答案 1 :(得分:0)

试试这个,你总是必须使用带有范围的set命令,否则你得到一个未定义的对象或块变量错误。

Dim mandayrng as range

   set mandayrng = Worksheets("manday").Range(cells(Col_Letter(Worksheets ("button").cells(2,3).value),3), cells(Col_Letter(Worksheets ("button").cells(2,3).value),10))'the range is L3: Q10

编辑:对于with部分,您不必指定工作表:

With Worksheets("report")
    row = .Range("C" & Rows.Count).End(xlUp).row
    For k = 6 To row
        .Cells(i, 6) = Application.WorksheetFunction.sum(mandayrng) 'sum with the range of mandayrng
    Next k
End With

最后,关于下面的代码,您希望从函数返回一个数组。为此,请确保您拥有数组所需的括号:

Function Col_Letter(lngCol As Long) As String()
Dim vArr() as string

在其他新闻中,如果你把昏暗的vAr我不确定将会发生什么,具体并使用'作为长/字符串等'

答案 2 :(得分:0)

你可以简化如下:

Option Explicit

Sub main()
    Dim mySum As Double

    With Worksheets("button")
        mySum = Application.WorksheetFunction.Sum(Worksheets("manday").Range(Cells(3, .Cells(1, 3).value), Cells(3, .Cells(2, 3).value)))
    End With
    With Worksheets("report")
        .Range("F6:F" & .Range("C" & .Rows.Count).End(xlUp).row).value = mySum
    End With
End Sub

BTW,如果您想使用Function Col_Letter(),请按以下方式更改:

Function Col_Letter(lngCol As Long) As String ' number to letter function
    Col_Letter = Split(Cells(1, lngCol).Address(True, False), "$")(0)
End Function