简体   繁体   中英

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.

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). 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. 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. Nothing happens when I click the CreateGrid-button. Any ideas how I can make this code work? Thanks a lot for all answers.

-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. 2.- Select any cell and type a value 3.- select a range of cells that you want 4.- Press the stop macro recording. 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.

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. 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 ?

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:

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.

EDIT: A shorter way, as pointed out by @Kyle, would be to use:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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