我需要创建一个Excel函数,在range(range1)
中找到一个值,并在找到值时与其他ranges(2,3,4)
生成一个产品,如下所示:
function PRODUCT_IF_RANGE=(obj as string,range1 as range,range2 as
range,range3 as range,range4 as range) as string
“范围”不一定共享相同的“工作表”,函数的输出类型是“字符串”,因为我需要这种符号。
我的问题是我如何知道sheet(name or index)
,首先是最后一行,以及范围的第一列和最后一列?有了它,我可以迭代范围并使函数运行,如:
dim row11 as integer 'first row´s number of range1
dim row12 as integer 'last row´s number of range1
dim column11 as integer 'first column´s number of range1
dim column12 as integer 'first column´s number of range1
........
for i=row11 to row12
for j=column11 to column12
if sheets(sheetr1).cells(i,j)=obj then
.......
next j
next i
........
........
所以我只需要知道第一行和最后一行的值(数字),第一行和最后一列以及范围表。
答案 0 :(得分:0)
您可以从Range
对象本身获取所有信息。
Set sheet1 = range1.Worksheet
row11 = range1.Row
row12 = range1.Row + range1.Rows.Count 'Start row + the number of rows in the range
column11 = range1.Column
column12 = range1.Column + range1.Columns.Count 'Start col + the number of cols in the range
答案 1 :(得分:0)
您可以使用此
With range1
range1SheetName = .Parent.Name
Set range1Sheet= .Parent
row11 = .Rows(1).Row
row12 = .Rows(.Rows.Count).Row
column11 = .Columns(1).Column
column12 = .Columns(.Columns.Count).Column
End With
但您也可以直接遍历行或列
Dim cell As Range
For Each cell in range1.Columns(1).Cells ‘iterate through first column cells -> iterate through rows
Msgbox cell.Row
Next
For Each cell in range1.Rows(1).Cells ‘iterate through first row cells -> iterate through columns
Msgbox cell.Column
Next
For Each cell in range1.Columns ‘iterate through columns
Msgbox cell.Address ‘ cell is the entire current column
Next
For Each cell in range1.Rows ‘iterate through rows
Msgbox cell.Address ‘ cell is the entire current row
Next