简体   繁体   English

Excel VBA如何选择单元格的可变范围

[英]Excel VBA How to select variable range of cells

I tried to search this problem but found no similar issue. 我试图搜索此问题,但没有发现类似问题。

I am still newbie in VBA and I'm trying to create macro which chooses range of cells depending on the user's input and then creates an outlined grid out of those selected cells. 我仍然是VBA的新手,我正在尝试创建宏,该宏根据用户的输入选择单元格的范围,然后从这些选定的单元格中创建轮廓网格。

I have two ActiveX buttons in my Excel workbook which let the user to input how big the grid is they want to use (Width & Height). 我的Excel工作簿中有两个ActiveX按钮,用户可以使用它们输入要使用的网格大小(宽度和高度)。 I am struggling to include the above mentioned width and height to my code. 我正在努力将上述宽度和高度包含在我的代码中。 Here is the code for the buttons (nothing unclear about them): 这是按钮的代码(一点都不不清楚):

Private Sub Height_Click()
Dim Height As Integer
Height = InputBox("Syötä ruudukon korkeus", "Ruudukon korkeus", "Syötä tähän")
Range("E5") = Height
End Sub

And width button: 和宽度按钮:

Private Sub Width_Click()
Dim Width As Integer
Width = InputBox("Syötä ruudukon leveys", "Ruudukon leveys", "Syötä tähän")
Range("E2") = Width
End Sub

I want my grid to start from cell "G2" and expand right&down from there and change the size of the selected cells. 我希望我的网格从单元格“ G2”开始,并从那里开始向右和向下扩展,并更改所选单元格的大小。 However the code I have written isn't working at all (as I thought). 但是我写的代码根本不起作用(正如我所想)。 Here is the code: 这是代码:

Private Sub CreateGrid_Click()
Columns("G:G+E2").Select
    Selection.ColumnWidth = 1
Rows("2:2+E5").Select
    Selection.RowHeight = 1
End Sub

Cells "E2" and "E5" have the values of width and height printed, respectively. 单元格“ E2”和“ E5”分别具有宽度和高度值。 Nothing happens when I click the CreateGrid-button. 当我单击CreateGrid按钮时,没有任何反应。 Any ideas how I can make this code work? 有什么想法可以使这段代码起作用吗? Thanks a lot for all answers. 非常感谢您提供所有答案。

-Teemu -Teemu

The trick is use the record macro button. 诀窍是使用记录宏按钮。 This function will record all instruction that you are doing with the excel book while is recording Example: 1.- Start the record macro and type a name for your macro. 此函数将记录正在记录的所有您正在使用excel手册的指令。示例:1.-启动记录宏并为宏键入名称。 2.- Select any cell and type a value 3.- select a range of cells that you want 4.- Press the stop macro recording. 2.-选择任何单元格并键入一个值3.-选择所需的单元格范围4.-按停止宏录制。 5.- Press Alt +F11 and you will see that excel generate a code of what you did in excel while the macro recording was turned on, even you can know how to type a value inside a cell or select a range of it. 5.-按Alt + F11键,您会看到excel会生成打开宏录制时在excel中所做的代码,即使您知道如何在单元格中键入值或选择它的范围也是如此。

EDIT: 编辑:

Private Sub CreateGrid_Click()
Range("G2:" & Range("G2").Offset(Range("E5").Value,Range("E2").Value).Addresslocal).Select
End Sub

If this doesn't do what you expect, please let me know and I will try to help correct it. 如果这样做没有达到您的期望,请告诉我,我将尽力帮助纠正它。

the command you're looking for is Range().Select but you'll need to create the string that goes into the parenthesis. 您要查找的命令是Range().Select但您需要创建括号中的字符串。 So if I understand correct you have stored in variables the number of rows and number of columns you want to offset from G2 right ? 因此,如果我理解正确,那么您已经在变量中存储了要从G2偏移的行数和列数,对吗?

In order to get the column letter you can use the function supplied by microsoft here 为了获得列字母,您可以在此处使用微软提供的功能

Function ConvertToLetter(iCol As Integer) As String
   Dim iAlpha As Integer
   Dim iRemainder As Integer
   iAlpha = Int(iCol / 27)
   iRemainder = iCol - (iAlpha * 26)
   If iAlpha > 0 Then
      ConvertToLetter = Chr(iAlpha + 64)
   End If
   If iRemainder > 0 Then
      ConvertToLetter = ConvertToLetter & Chr(iRemainder + 64)
   End If
End Function

but since you're starting from G you'll have to call the function like this: 但是由于您是从G开始的,因此必须像下面这样调用该函数:

ConvertToLetter(7+numerColumns)

your final code will look like 您的最终代码将如下所示

Range("G" & 2 + numberLines & ":" & ConvertToLetter(7+numberCols) & numberLines).Select

where numberLines is the number of lines to offset and numberCols the number of columns. 其中numberLines是要偏移的行数,numberCols是列数。

EDIT: A shorter way, as pointed out by @Kyle, would be to use: 编辑:如@Kyle所指出的,一种较短的方法是使用:

Range("G2").Offset(numberLines,numberCols).Select

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

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