Excel VBA: How do I assign a range to a variable?

Sean Reeves

The current code is as follows:

With Sheets(sheetChk)
    a = .Range(chkCell, .Range(Left(chkCell, 1) & Rows.Count).End(xlUp)).Value
End With

sheetChk is defined as the desired sheet index. chkCell is defined as the desired "first cell" in the range.

I am attempting to assign a range to a. The problem is that the above code grabs the last cell in the column, entirely. Rather, I want to end at a specific row as I have other data in the same column after the desired range that needs to be ignored. How can I accomplish this?

PGSystemTester

First to assign a range, you need to have a Set first.

Example: Set a = Range("A1")

Another problem I see is that you're putting a .value at the end. That doesn't make sense for a range.

If you want to specify a specific row, then you need to include that in the code instead of using end(xlUp).

Example if it's row 50:

With Sheets(sheetChk)
    Set a = .Range(chkCell, .Range(Left(chkCell, 1) & 50).End(xlUp))
End With

What you're currently using is finding the bottom row being used, which doesn't sound like you want. If you want to go the other direction (i.e. from the starting cell down until there's an empty cell), you can use this code:

With Sheets(sheetChk)
    Set a = .Range(chkCell, .Range(Left(chkCell, 1) & chkCell.Row).End(xlDown))
End With

Based on your code, it looks like you might be putting an address in whatever cell chkCell is. If you have the row in that cell, and assuming you never exceed column z, then you could use this code to find the row:

With Sheets(sheetChk)
    Set a = .Range(chkCell, .Range(Left(chkCell, 1) & Right(chkCell,Len(chkCell)-1))
End With

If that doesn't work, you need to figure out some method determine what row to use. Hope that helps.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I determine if an Excel range is hidden?

How do I constrain a value to a range in excel?

Excel VBA AVERAGE(variable range)

How do I assign a jQuery object to a variable?

How can I print a range variable in the immediate window? Excel VBA

How do I copy some specific columns from a given range which is auto filtered in Excel VBA?

How do I copy a range from one workbook to another in excel WITHOUT having to name it in VBA?

How do I assign to a variable in a for loop in C?

How do I assign Named Ranges to Intersect Range for Worksheet Change?

How can I assign a dynamic range (of rows) to a checkbox in vba?

How do I assign an Interior-Object to a Range?

How do I prevent an excel table's formatting from overwriting a range's existing formatting in VBA?

React: How do I assign an element to a variable?

How to assign range in a string VBA?

If I am using pandas .to_excel, how do I assign a worksheet to a variable?

How do I store the range of a list as a variable?

How can I set a variable range to a table in excel VBA 2007?

how do I get a cell's location as a range in excel vba?

How do I Loop Through A Sorted Excel Range in VBA in the Order I Sorted It In?

excel/VBA how to assign the variable length of an array to integer variable

EXCEL VBA - how to pass # of filled cells inside a custom range, into the "i" variable using Application.WorksheetFunction.CountA?

How do I assign a variable to an object, not a reference to it?

How do I assign a string to an int variable?

How do I assign this modified array to a variable?

How do I define a range as a variable to use as an argument of an excel worksheet function?

How do I convert the Excel formula =STDEV.S(IF(FREQUENCY(range,range),range)) into VBA code?

How do I assign return variable in prolog?

How do I paste a range of cells from Excel into Word using VBA

How do I assign the result of a function to a variable?

TOP Ranking

HotTag

Archive