Copy Occupied cells within a range and Paste to a different cell

cbye

New Member
Joined
Feb 2, 2022
Messages
23
Office Version
  1. 365
Platform
  1. Windows
Hi, I get reports that we need to move certain cells around to make them more readable. The Header is in Row4. The occupied cells are somewhere between A5 and A300, each report is different. I need to copy or cut whichever cell is occupied and paste it one column over and one row down. IE CellA4 is hostname and CellB4 is Machine Type. I don't want these to move. Cell A5 is hostnameA. This needs to move to B6, right under the machine type. They are spaced random within A5 and A300 but they always will be pasted one column over and one row down. I am fairly new to VBA and have tried several Range commands but I always seem to copy all the cells and when I paste them in colB, they write over the existing data. Thanks in advance.
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Welcome to the MrExcel board!

Could we have a small set of sample dummy data and then the expected results with XL2BB?
 
Upvote 0
Book1
ABCDEF
4HostnameMachine TypeMachine data AMachine data BMachine data CMachine data D
5hoatnameA1234datadatadatadata
6datadatadatadata
7datadatadatadata
8datadatadatadata
9datadatadatadata
10datadatadatadata
11
12hostnameB2000datadatadatadata
13datadatadatadata
14datadatadatadata
15
16hostnameC4567datadatadatadata
17datadatadatadata
18datadatadatadata
19datadatadatadata
20datadatadatadata
21datadatadatadata
22datadatadatadata
23datadatadatadata
24datadatadatadata
25datadatadatadata
26datadatadatadata
27datadatadatadata
28datadatadatadata
29datadatadatadata
30datadatadatadata
31datadatadatadata
32datadatadatadata
33datadatadatadata
Sheet1




Book1
ABCDEF
4HostnameMachine TypeMachine data AMachine data BMachine data CMachine data D
51234datadatadatadata
6hoatnameAdatadatadatadata
7datadatadatadata
8datadatadatadata
9datadatadatadata
10datadatadatadata
11
122000datadatadatadata
13hostnameBdatadatadatadata
14datadatadatadata
15
164567datadatadatadata
17hostnameCdatadatadatadata
18datadatadatadata
19datadatadatadata
20datadatadatadata
21datadatadatadata
22datadatadatadata
23datadatadatadata
24datadatadatadata
25datadatadatadata
26datadatadatadata
27datadatadatadata
28datadatadatadata
29datadatadatadata
30datadatadatadata
31datadatadatadata
32datadatadatadata
33datadatadatadata
Sheet1
 
Upvote 0
I don't think I mentioned this in the inital post. Each report is different. IE some reports only have 1 hostname. Some have up to 15. And they could be anywhere between A4 and A300.
 
Upvote 0
Thanks for the samples. (y)

Try this with a copy of your workbook.

VBA Code:
Sub MoveHostName()
  Dim r As Range
  
  For Each r In Range("A5", Range("A" & Rows.Count).End(xlUp)).SpecialCells(xlConstants)
    r.Cut Destination:=r.Offset(1, 1)
  Next r
End Sub
 
Upvote 0
AAAAAAAAAANd thats why you make the big bucks Peter_SSs!!! I appreciate you! Works like a champ. Gunna save me tons of time. VBA, work smart not hard yea? Thanks Again.
 
Upvote 0
? You're welcome. Thanks for the follow-up. :)
 
Upvote 0
Hi Peter_SSs. Im reaching out to you re the code you made for me previously. I have a very similar situation that I need to move a cell's contents if occupied to a specific cell. Much like your other code going over 1 column and down 1 row. I have some data that needs to move from column H to column A and then down 3 rows. IE data is in H5, end result would be A8. I thought I had a grasp on what you did but when I made changes, it didn't work. This is what I have.

VBA Code:
Sub MoveUAKdate()
  Dim r As Range
 
  For Each r In Range("H5", Range("H" & Rows.Count).End(xlUp)).SpecialCells(xlConstants)
    r.Cut Destination:=r.Offset(-7, 3)
  Next r
  
 
End Sub

I get a runtime error 1004 and when I debug it highlights " r.Cut Destination:=r.Offset(-7, 3)"

It works if I have positive numbers for the offset. How do I move backwards with this? All of the references that I found always moved the data in a positive direction.
 
Last edited by a moderator:
Upvote 0
When posting code in the forum, please use the available code tags to preserve the code formatting.
I have fixed that for you in the above post. My signature block below has more details.

In Offset, the first argument is rows and the second is columns so you just have your numbers the wrong way around. :)
It should be
VBA Code:
r.Cut Destination:=r.Offset(3, -7)
 
Upvote 0
Solution
Ah, ok got it. Works great. Ill remember to use the code tags. Thanks again.
 
Upvote 0

Forum statistics

Threads
1,215,615
Messages
6,125,854
Members
449,266
Latest member
davinroach

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top