Donate SIGN UP

Visual Basic Macro Code

Avatar Image
Old_Geezer | 14:07 Thu 15th Jun 2017 | Computers
5 Answers
I'm probably being lazy but was hoping some expert could tell me how to fix the code Excel created. I've tried this a few times in the past for no success, and it seems simple enough that it should be easily editable.

Range("A2").Select
Selection.Cut
Range("F1").Select
ActiveSheet.Paste
Rows("2:2").Select
Selection.Delete Shift:=xlUp
Range("A2").Select

The above is a mess. It is defining specific cells when any normal default should be to note movements relative to start and encode them.

What I was trying to achieve was:

Start at whatever cell you are in. (Column A)
Drop down a row and cut the data in the cell there, which ought not be there.
Move back up a row and move across to 5 cells (Column F)
Paste the data there, where it should have been, instead.
Highlight the row below (from where data has just been removed).
Delete it as it is no longer of use.
The highlighted cell should now be in column A of the next line of data, i.e. one row below where we started, so that the macro can be run again on the next as yet unfixed data.

Can anyone clarify what it is I need to change in the code to stop these absolute references and turn them into relative ones ? (I'm using Office 2003 so I can't just tell it to act sensible when recording, as far as I know.)

Cheers.
Gravatar

Answers

1 to 5 of 5rss feed

Avatar Image
Use activecell.offset(1,0).activate to select the row below where you are and then ActiveCell.EntireRow.Delete (I think)
15:55 Thu 15th Jun 2017
OG, I thought we'd been through this before :-)
use offset to navigate, so offset(1,1) is one down and one to the right with negative numbers moving up and left.

So
activecell.offset(1,0).select - with cursor in A1 this will select B1
Negative numbers move up and left
etc. etc.
Question Author
I felt we did and I'd assigned an icon button to it, but one day I opened Excel, the button icon had gone, and the macro had stopped working. It's the way life is these days. As I have grown older the world has become less logical, less rational. Inexplicable things happen regularly. Ho hum ...

I'll give your suggestions a try. Thanks.
Question Author
Any thoughts on how to select the next row for deletion ? I'm unsure what Rows("2:2").Select means but it looks like an absolute reference to me. At least it doesn't sound very relative for the row below when it contains just 2s. I've tried a few things but they all crash out at run time.
Use activecell.offset(1,0).activate to select the row below where you are and then ActiveCell.EntireRow.Delete (I think)
Question Author
Router fell off the Net for ages so wasn't able to look much up.

Sub SORT_DATA()
'
' Macro recorded 15/06/2017 by Old_Geezer
'
ActiveCell.Offset(1, 0).Select
Selection.Cut
ActiveCell.Offset(-1, 5).Select
ActiveSheet.Paste
ActiveCell.Offset(1, 0).Activate
ActiveCell.EntireRow.Delete
ActiveCell.Offset(0, -5).Select
End Sub

Yay ! Thanks. Now I have to try not to lose it again.

1 to 5 of 5rss feed

Do you know the answer?

Visual Basic Macro Code

Answer Question >>

Related Questions

Sorry, we can't find any related questions. Try using the search bar at the top of the page to search for some keywords, or choose a topic and submit your own question.