Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

Selecting a VBA range using cell variable

0

How do I select a range that is defined by cell variables. I've tried variants of this:

Sheets(ws_output).cells((1+next_row,next_col):(5+next_row:next_col)).Select

but I can't get it to work. This particular line is a sytax error and this one:

Sheets(ws_output).cells((1+next_row,next_col):(5+next_row:next_col)).Select

is "application defined or object defined error". 

Answer
Discuss

Discussion

I should have been more clear. "ws_ouput" is a worksheet. I have written a macro that transfers date, time,systolic, diastolic blood pressure and comments to a worksheet, "bp record" that I have set ws_output as a variable. That variable came from copying a macro from this forum, I'm pretty sure, for finding the last filled column. Everything works, I even have it so after 11 columns it moves to a ne set of rows, except I want to center the data in the columns and that's why i need to select the range using the variables so I can use "With Selection" to format them. The last answer is the one I am looking for. I used a ":" instead of a "," between the cells in the range.
     I am sorry if my terminology isn't 100%, I am new at this. Is there someplace that showsnothing but syntax for all of the VBA functions? My basic technique is to macro record what I want to do and start with that but that doesn't always work.
Thank youfor your time.
baddog1016 (rep: 12) Jun 22, '20 at 9:26 am
here's more of the code, I am getting a syntax error on the select line.

Sheets(ws_output).Cells(4 + next_row, next_col).Value = Range("tpulse").Value Sheets(ws_output).Cells(5 + next_row, next_col).Value = Range("tcomment").Value Sheets(ws_output).Cells(5 + next_row, next_col).WrapText = True   sheets(ws_output).Cells((next_row + 1,next_col), (Next_row + 5, next_col)).select      'this line has a syntax error
With Selection     .HorizontalAlignment = xlCenter         .VerticalAlignment = xlCenter  End With     Sheets("Mac write Input").Range("d8,d10,d12,d14").Select   Selection.ClearContents   Sheets(ws_output).Select Sheets(ws_output).Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
baddog1016 (rep: 12) Jun 22, '20 at 9:32 am
You want to select a range but the word "Range" is missing in your syntax. A range is defined by its first and last cells. That you have. But each cell is defined by its coordinates and for the second cell the word "Cells" is missing.
No need to select anything. The following will do the same job.
with Worksheets(ws_output).Cells(next_row + 1, next_col).Resize(2, 1)
     .HorizontalAlignment = xlCenter
     .VerticalAlignment = xlCenter
End With

Proceed analog for the "Mac write Input" tab.
You can look up the syntax for any expression by googling, like, "vba resize msdn". "vba" will exclude worksheet functions by the same name. "MSDN" will include Microsoft's developer notes.
Variatus (rep: 4889) Jun 22, '20 at 8:33 pm
Thank you, your solution works. I could not get my method to work at all. I haven't seen "resize" before, I am going to look that up now as I don't quite understand what that with what I guess to be the relative vell reference (2,1) is doing.
baddog1016 (rep: 12) Jun 23, '20 at 12:16 pm
The meaning of Resize(2, 1) is to take the original range and make it 2 rows high and 1 column wide. In your example, the "original range" is defined as Cells(next_row + 1, Next_Col).
Variatus (rep: 4889) Jun 23, '20 at 8:22 pm
Add to Discussion

Answers

0
Selected Answer

Note that there is a difference between the Sheets collection and the Worksheets collection. If you are interested, look it up on the Internet. But for the purpose of this thread I suggest you make it a rule to talk about the things you know rather than the things you don't understand. You know what a worksheet is.

Object collection items are addressed by their index number. The Worksheets collection is a collection of worksheet objects. Therefore you can address each item in the collection by its index number. Numbers always start from one and end with the Count property. The largest available index is, therefore, Worksheets.Count. If you call for a number greater than Count a Subscription Error will occur.

Instead of the Index number you can also use the Name property. In fact, for worksheets, this is more common. The name is a variable of String data type. It could be a hard-coded string or a variable of string data type. So, what is "ws_output" in your code? If you have declared ws_ouput as a string, Worksheets(ws_output) will identify a worksheet. If a sheet by that name doesn't, or the variable hasn't been assigned a value, exist a Subscription Error will result.

VBA allows you to use objects on the fly without naming them. But if you encounter a problem like the one that brought you here it's better to take none of the offered shortcuts. This is the code you should use. It would allow you to see, line by line, where the error occurs.

Dim ws_output As String
Dim Ws As Worksheet

ws_output = "My Output Tab"
Set Ws = Worksheets(ws_output)

All the cells in a worksheet are members of the Cells collection. Therefore you could address them by their index number. But it's easier to address them by their coordinates. This is the syntax: Cells([Row number], [Column number]). You can also use the column name but names, of course, are strings and strings are hard to do calculations with. Therefore a number is preferable. But the following two examples are exquivalent.

Cells(10, 2)
Cells(10, "B")

Each number can be represented by a reference or a formula. Commonly used is Rows.Count which you recognize. It returns the number of members in the Rows collection, which is 1048576. Therefore Cells(Rows.Count, "A") is the last cell in column A, or A1048576, and Cells(Rows.Count, "A").End(xlUp) is the first used cell looking from row 1048576 up, which is the last used cell in that column if you look from the top.

If you have a variable Next_Row (underscores in variable names went out of fashion in 1986), which should be of Long data type because an Integer cant hold the number 1048576, you can address a cell Cells(Next_Row, "A"). And if you also have a Long variable called SameClm you can address a cell as Cells(Next_Row, SameClm). Variations like Cells(Next_Row +5, SameClm) are legal and often used like Cells(Next_Row + R, SameClm). But it's essential that you maintain the basic syntax for cell addressing, e.g. Cells([Row number], [Column number]).

You can define a range by specifying its first and last cells. Range(Range("A1"), Range("B2")) would do that. But such an address is hard to calculate. Therefore one would use this instead:-

Set MyRange = Range(Cells(1, 1), Cells(2, 2))

Again, remember the basic syntax for each cell and for the range when you build complicated constructions.

And now, as to the last word in your code I'm happy to inform you that you don't need it. The Select statement is strictly for the user to communicate with the worksheet. Your PC owns the worksheet and knows exactly where every part of it is. You don't need to point at it. Just tell the computer which cell you mean and you can read from it or write to it without ever "selecting" anything. Use the Select statement when you want to hand control back to the user. Like "Please write your name in the selected cell", and then you select a cell for him.

So, your code might intend somethig like I show below. That would delete all contents of the range you specify.

Worksheets(ws_output).Cells((next_row + 1,next_col), (Next_row + 5, next_col)).ClearContents
Discuss


Answer the Question

You must create an account to use the forum. Create an Account or Login