简体   繁体   English

在VBA和Excel中使用范围

[英]Using Range in VBA and Excel

The following is valid in VBA 以下在VBA中有效

Range("K1:K5")

Why is the following not valid? 为什么以下无效?

Dim nb As Long
Range("K1:K" + nb)

I also tried Range("K1:K" & nb) and even tried making nb a String instead. 我还尝试了Range(“ K1:K”&nb),甚至尝试使nb成为String

Because the value of nb = 0 and the rows in excel start with 1 因为nb = 0且excel中的行以1开头

Try this 尝试这个

Dim nb As Long
Dim Rng As Range

nb = 2
Set Rng = Range("K1:K" & nb)

I'm not sure what the value is for nb. 我不确定nb的值是多少。

Try using the Cells(RowIndex, ColumnIndex) notation where RowIndexand ColumnIndex are integers. 尝试使用Cells(RowIndex,ColumnIndex)表示法,其中RowIndex和ColumnIndex是整数。

Sub RangeExemple()

Dim colIndex, colIndex2, rowIndex, rowIndex2 As Integer

colIndex = 7
colIndex2 = colIndex
rowIndex = 1
rowIndex2 = 5

Range(Cells(rowIndex, colIndex), Cells(rowIndex2, colIndex2)).Interior.ColorIndex = 37

End Sub 结束子

What do you want to do actually? 您实际上想做什么?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM