Help with a macro to copy adjacent cells

Busscheduler

New Member
Joined
Nov 23, 2020
Messages
41
Office Version
  1. 2016
Platform
  1. Windows
Hi, I’m working on a macro to copy and paste different cells:

Cells(rows.count, “AF”). End(xlup).select
Active cell. Offset(0,-1).select

I have the macro finding the last cell in column AF, coming back up to the first cell with data and then moving one cell to the left “AE” and selecting that cell. I need it to copy cells AE:AG of that row and insert that into AH and shift the cells down to insert. I need it to loop through the rows from bottom to top and do this for each row, if cells in AF have data in them.

Thanks for any help I can get on this. I just can’t find the right code to make all of this happen in the short time I have to get this done.
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Does your explanation mean that you want to copy two cells in Columns AE:AF, if the cell in Column AF has a value, and insert these two cells into AH:AI in the same Row?

Upload a mini sheet with before and after would be a good idea.
 
Upvote 0
I would like to copy three cells in AE,AF and AG, if column AF has a value and insert them into AH in the same row. AH would have other data which would be shifted down for the insert.

My apologies for not being able to upload a sheet. I am doing this with my cell.
 
Upvote 0
If I am remotely close to understanding your requirement, this might be what you're after.
Change references if and where required. I also assume you know the "golden" rule. Try on a copy of your original.
Code:
Sub Maybe()
Dim i As Long
Application.ScreenUpdating = False
For i = Cells(Rows.Count, 32).End(xlUp).Row To IIf(Cells(2, 32) <> "", 2, Cells(1, 32).End(xlDown).Row) Step -1
    If Len(Cells(i, 32)) <> 0 Then
        With Cells(i, 31)
            .Resize(, 3).Copy
            .Offset(, 3).Insert Shift:=xlDown
        End With
    End If
Next i
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution
Yes, I have learned the hard way about not using a copy of the original sheet. I will try this as soon as I get back home and let you know how it worked. Thanks.
 
Upvote 0
jolivanes, your code worked perfectly. Thanks so much for your help. I am marking this as solved.
 
Upvote 0
jolivanes, here is the sheet to show you what the code did. Again, it worked perfectly to give me the result I was looking for. Thanks again.

Copy of TUI - Departures - 4-11-22 (20220406).xls
AEAFAGAHAIAJAKALAMAN
1Time
2BUS 1 (16)10:35Clarion Lake Buena VistaBUS 1 (16)10:35Clarion Lake Buena Vista
3FOWLER - 90050 - Party of 4
411:00Disney's All Star Music ResortN. Alexander - 87677 - Party of 6
511:00Disney's All Star Music Resort
6BUS 2 (34)10:50Coco Key Hotel and Water Park ResortL. BENBOW - 87672 - Party of 3
7T. Gibbon - 88298 - Party of 3
8BUS 2 (34)10:50Coco Key Hotel and Water Park Resort
910:55Rosen Inn InternationalH. Mcgowan - 87684 - Party of 4
10A. Loveridge - 87909 - Party of 2
11T. Gambiza - 88308 - Party of 2
1211:05Sonesta ES Suites Orlando10:55Rosen Inn International
1311:15Rosen Inn Pointe OrlandoA. Grayson - 87682 - Party of 5
14D. Thomson - 87904 - Party of 3
15S. ****er - 87683 - Party of 2
1611:20Ramada Plaza Resort and Suites International Drive11:05Sonesta ES Suites Orlando
17MORRIS - 89407 - Party of 2
18BUS 3 (24)11:10The Avanti Resort11:15Rosen Inn Pointe Orlando
19C. Baines - 87688 - Party of 2
2011:15Universal's Endless Summer ResortK. Eckersley - 87911 - Party of 4
21K. Graham - 87659 - Party of 4
2211:20Ramada Plaza Resort and Suites International Drive
23M. HARRIS - 88431 - Party of 2
24S. newham - 88577 - Party of 2
25BUS 4 (16)12:50Disney's Caribbean BeachBUS 3 (24)11:10The Avanti Resort
2613:10Clarion Lake Buena VistaK. Bernardi - 87657 - Party of 6
2713:30Disney's Coronado Springs ResortP. Rennison - 87902 - Party of 4
2811:15Universal's Endless Summer Resort
29BUS 5 (25)13:25Rosen Inn InternationalA. Browning - 87709 - Party of 4
30D. Forbes - 87912 - Party of 4
3113:35Sonesta ES Suites OrlandoS. Lindner - 88253 - Party of 2
3213:40Rosen Inn Pointe OrlandoT. Christopher - 88307 - Party of 4
3313:45Ramada Plaza Resort and Suites International Drive
34BUS 4 (16)12:50Disney's Caribbean Beach
35P. Newton - 88256 - Party of 5
36BUS 6 (19)13:40Loews Royal Pacific Resort at Universal Orlando13:10Clarion Lake Buena Vista
3713:45Universal's Endless Summer ResortE. Kelly - 88239 - Party of 3
3813:30Disney's Coronado Springs Resort
39J. Quinn - 88002 - Party of 4
4013:50Rosen Shingle CreekM. Williams - 87988 - Party of 4
41BUS 5 (25)13:25Rosen Inn International
42L. Norris - 87987 - Party of 3
43S. Glover - 88305 - Party of 4
4413:35Sonesta ES Suites Orlando
45M. BOWEN - 87658 - Party of 4
4613:40Rosen Inn Pointe Orlando
47J. Kenney - 87786 - Party of 2
4813:45Ramada Plaza Resort and Suites International Drive
49A. Lopez - 87696 - Party of 4
50E. Lawrence - 88824 - Party of 4
51K. Doughty - 89822 - Party of 4
52BUS 6 (19)13:40Loews Royal Pacific Resort at Universal Orlando
53J. Cousins - 87961 - Party of 3
5413:45Universal's Endless Summer Resort
55A. Barritt - 88295 - Party of 4
56E. tranter - 89193 - Party of 5
57S. Selby - 88309 - Party of 5
5813:50Rosen Shingle Creek
59H. Payne - 87676 - Party of 2
60
Sheet0 (4)
 
Upvote 0
Thanks for letting us know that you're happy with it and good luck.
 
Upvote 0
jolivanes, might you help with one more item? I am trying to copy a column of data that has formulas in it and paste it as values. I tried several renditions of the following code with no luck:

.PasteSpecial xlPasteValues

Here is the line of code that I am trying to paste values with:

Cells(r - 1, "V").Copy Cells(r - 1, "AH")

I know that there is a line of code that will ignore formulas and only paste values but I just can't get it to work. Thanks.

Here is the entire code:

VBA Code:
Sub Tui()
 Dim lr As Long, r As Long, x As Integer
 Application.ScreenUpdating = False
 lr = Cells(Rows.Count, "R").End(xlUp).Row
     Cells(2, "R").Copy Cells(2, "Af")
     Cells(2, "T").Copy Cells(2, "AE")
     Cells(2, "S").Copy Cells(2, "AG")
 For r = 3 To lr + 1
    If Cells(r, "S") <> Cells(r - 1, "S") Then
    'if cell 3 in column s is not equal to cell 2 in column s
        Cells(r, "R").Copy Cells(r, "AF")
    'copy cell 3 in column R and paste it into column AF
        Cells(r, "S").Copy Cells(r, "AG")
    'also copy column s and paste into column AG
        Cells(r, "T").Copy Cells(r, "AE")
    
    End If
 

 Cells(r - 1, "V").Copy Cells(r - 1, "AH")
 
 Next r
 
 Dim i As Long
Application.ScreenUpdating = False
For i = Cells(Rows.Count, 32).End(xlUp).Row To IIf(Cells(2, 32) <> "", 2, Cells(1, 32).End(xlDown).Row) Step -1
    If Len(Cells(i, 32)) <> 0 Then
        With Cells(i, 31)
            .Resize(, 3).Copy
            .Offset(, 3).Insert Shift:=xlDown
        End With
    End If
Next i
Application.ScreenUpdating = True
 
 
 
End Sub
 
Upvote 0
Is this what you're referring to?
Code:
Cells(r-1, "AH").Value = Cells(r-1, "V").Value
 
Upvote 0

Forum statistics

Threads
1,214,832
Messages
6,121,841
Members
449,051
Latest member
excelquestion515

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