For the record, I’m using Excel 2010.
I’m trying to teach myself VBA for a job at work (they give the interns the VBA stuff…), and I’m getting stuck on the simplest of things.
I am trying to make a bordered table that is three columns wide. The amount of rows in the table has to be based on the amount of inputs the user has. In my code, that is the value given for Count by the user.
My issue is that I have no idea how to select the range I need. The only way I know how to select a range is using:
ActiveCell.Range("Top left cell:Bottom right cell").Select
If it were from A1 to C8, how do I make that work? I wish it would just work like:
ActiveCell.Range("A1:C(count)").Select
This is what I have so far:
Option Explicit
Dim Count As Long
Dim CFLArray() As Variant
Sub TableCreation1()
Range("A1").Select
ActiveCell.FormulaR1C1 = "Time (days)"
ActiveCell.Offset(0, 1).Range("A1").Select
ActiveCell.FormulaR1C1 = "CFL (measured)"
ActiveCell.Offset(0, 1).Range("A1").Select
ActiveCell.FormulaR1C1 = "De (estimated)"
ActiveCell.Offset(0, -2).Range("A1:C1").Select
Selection.Font.Bold = True
ActiveCell.Columns("A:A").EntireColumn.EntireColumn.AutoFit
ActiveCell.Offset(0,1).Columns("A:A").EntireColumn.EntireColumn.AutoFit
ActiveCell.Offset(0,2).Columns("A:A").EntireColumn.EntireColumn.AutoFit
ActiveCell.Select
End Sub
Sub FindRange()
Range("A2").Select
Count = InputBox("How many pairs of data do you have? ")
End Sub
I taught myself VBA a day or so ago using a book my work had, but I couldn’t find anything along these lines in there. My internet searches failed as well. The worst part is I know it’s going to be incredibly simple.
It is that simple! You were just off by a little bit.
Change:
ActiveCell.Range("A1:C(count)").SelectTo:
ActiveCell.Range("A1:C" & count).SelectThough, I don’t see any
countvariable in your code.If you know that it’s always the same column, you could do this:
Range("A1:C" & ActiveSheet.Range("C1048576").End(xlUp).Row).Select(
1048576is the max row in Excel 2010. This may not be perfect for your data, but hopefully it will get you started in the right direction…)Also, just as an added point about VBA, you don’t need to use
Selectin most cases. For example, this:Can be rewritten more simply as:
And if you are just using text:
I also want to point out that using
ActiveCell.Range("A1:C" & count).Selectis not the same asActiveSheet.Range("A1:C" & count).Select. Depending on what cell is currently selected, these will select different ranges. If you really meanA1:C8, and notA1:C8 (relative to the current cell), then you’ll want to use theActiveSheet(or omitted) version.