简体   繁体   中英

VBA Excel Macro: Offsetting Cell Selection, Selection as Defined by Macro

I am writing a line of code as part of a more complex program that is not working correctly. I am new to VBA so please bear with me...

Essentially, I am prompting the user to select a cell which is then assigned to the variable 'celNm'.

I then perform the following actions:

celNm.EntireRow.Copy
celNm.EntireRow.Insert

Next, for reasons specific to the program (and I am assuming the celNm will be located at the same cell after the command [Not the same location]), I want to move the cell selection upwards, so it is now located at the row just recently copied above. I am using the following line to do this:

celNm.Offset(-1, 0).Select

This, however, does not work.

The next step in the program would be to create a list in this location. However the program still creates a list at the previous location (in the cell selected). Why is this?

celNm.Offset(-1,0).Select only selects the cell above celNm. It does not change the value of celNm. To change the value of celNm, you need the following:

Set celNm = celNm.Offset(-1,0)

I'm assuming celNm is declared as a Range.

I believe you'll need to work with ActiveCell . Here's an example (You'll want to change "A1" to your celNm variable):

Range("A1").Select
ActiveCell.Offset(-1, 0).Activate

Source

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