如何设置此范围的excel范围?(发生错误)
基本信息:
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的范围-mandayrng,并总结范围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
但是这里的代码不起作用。
怎么修 ?
没有必要使用Col_Letter
。 细胞为一个字母或数字。
一个范围可以采取一个string地址开始与一个列字母,并跟着行索引。 如果Range有两个参数,那么范围从第一个参数延伸到第二个参数,包括其间的所有单元格。
这些例子都是指“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))
无效)
范围和它的Cells参数必须在同一个工作表上。
如果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
试试这个,你总是要用一个范围来使用set命令,否则你会得到一个对象或块variables未定义的错误。
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 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
在其他新闻,如果你把昏暗的vArr我不知道会发生什么,具体和使用'长/string等'
你可以简化如下:
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
顺便说一句,如果你想使用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