-1

Top Table

What Top Table should looks like afterward

Bottom Table

What Bottom Table should looks like afterwards

I have the following code that basically copy the last two rows from the first table and then insert the rows below. Then I will delete certain part of the cells from the inserted rows. As you can see there is a pattern, 4,5,6....7,8,9....,11,12,13.... etc

Then, it will go to the bottom table and copy the row above "DOS" and insert it below. Afterwards, it'll copy the cell from column "OUT" from the row above and paste it to the row below.

The first problem that I am having is that the Union only take up to 30 arguments, but I have more than 30 rng.

I'd like to make my code more efficient using for loop or something

Sub BajaFresh_Update()
        Range("A1").Select
        Selection.End(xlDown).Select
        Selection.End(xlDown).Select
        Union(ActiveCell.EntireRow, ActiveCell.Resize(1).Offset(-1).EntireRow).Copy
        ActiveCell.Resize(1).Offset(1).EntireRow.Insert Shift:=xlDown
        Application.CutCopyMode = False


        Set rng1 = ActiveCell.Offset(1, 4)
        Set rng2 = ActiveCell.Offset(2, 4)
        Set rng3 = ActiveCell.Offset(1, 5)
        Set rng4 = ActiveCell.Offset(2, 5)
        Set rng5 = ActiveCell.Offset(1, 6)
        Set rng6 = ActiveCell.Offset(2, 6)
        Set rng7 = ActiveCell.Offset(1, 8)
        Set rng8 = ActiveCell.Offset(2, 8)
        Set rng9 = ActiveCell.Offset(1, 9)
        Set rng10 = ActiveCell.Offset(2, 9)
        Set rng11 = ActiveCell.Offset(1, 10)
        Set rng12 = ActiveCell.Offset(2, 10)
        Set rng13 = ActiveCell.Offset(1, 12)
        Set rng14 = ActiveCell.Offset(2, 12)
        Set rng15 = ActiveCell.Offset(1, 13)
        Set rng16 = ActiveCell.Offset(2, 13)
        Set rng17 = ActiveCell.Offset(1, 14)
        Set rng18 = ActiveCell.Offset(2, 14)
        Set rng19 = ActiveCell.Offset(1, 16)
        Set rng20 = ActiveCell.Offset(2, 16)
        Set rng21 = ActiveCell.Offset(1, 17)
        Set rng22 = ActiveCell.Offset(2, 17)
        Set rng23 = ActiveCell.Offset(1, 18)
        Set rng24 = ActiveCell.Offset(2, 18)
        Set rng25 = ActiveCell.Offset(1, 20)
        Set rng26 = ActiveCell.Offset(2, 20)
        Set rng27 = ActiveCell.Offset(1, 21)
        Set rng28 = ActiveCell.Offset(2, 21)
        Set rng29 = ActiveCell.Offset(1, 22)
        Set rng30 = ActiveCell.Offset(2, 22)
        Set rng31 = ActiveCell.Offset(1, 24)
        Set rng32 = ActiveCell.Offset(2, 24)
        Set rng33 = ActiveCell.Offset(2, 25)
        Set rng34 = ActiveCell.Offset(2, 25)
        Set rng35 = ActiveCell.Offset(2, 26)
        Set rng36 = ActiveCell.Offset(2, 26)


        Union(rng1, rng2, rng3, rng4, rng5, rng6, rng7, rng8, rng9, rng10, rng11, rng12, rng13, rng14, rng15, rng16, rng17, rng18, rng19, rng20, rng21, rng22, rng23, rng24, rng25, rng26, rng27, rng28, rng29, rng30).ClearContents
        Union(rng32, rng33, rng34, rng35, rng36).ClearContents
        ActiveCell.End(xlDown).Select
        ActiveCell.End(xlDown).Select
        ActiveCell.Offset(-6).EntireRow.Copy
        ActiveCell.Offset(-5).Select
        ActiveCell.EntireRow.Insert Shift:=xlDown
        Application.CutCopyMode = False

        Set copy1 = ActiveCell.Offset(-2, 5)
        Set copy2 = ActiveCell.Offset(-2, 9)
        Set copy3 = ActiveCell.Offset(-2, 13)
        Set copy4 = ActiveCell.Offset(-2, 17)
        Set copy5 = ActiveCell.Offset(-2, 21)
        Set copy6 = ActiveCell.Offset(-2, 25)
        Set paste1 = ActiveCell.Offset(-1, 5)
        Set paste2 = ActiveCell.Offset(-1, 9)
        Set paste3 = ActiveCell.Offset(-1, 13)
        Set paste4 = ActiveCell.Offset(-1, 17)
        Set paste5 = ActiveCell.Offset(-1, 21)
        Set paste6 = ActiveCell.Offset(-1, 25)
        copy1.Copy
        ActiveSheet.Paste paste1
        copy2.Copy
        ActiveSheet.Paste paste2
        copy3.Copy
        ActiveSheet.Paste paste3
        copy4.Copy
        ActiveSheet.Paste paste4
        copy5.Copy
        ActiveSheet.Paste paste5
        copy6.Copy
        ActiveSheet.Paste paste6




        End Sub
gary0418
  • 5
  • 2
  • 1
    first get rid of all the `.Select` and stop using `ActiveCell` Use the actual range through, `Range()` or `Cells` See [HERE](https://stackoverflow.com/questions/10714251/how-to-avoid-using-select-in-excel-vba) for guidance. – Scott Craner Aug 24 '18 at 17:04

1 Answers1

1

Highly recomend taking @Craners advice left in comment. Remove all instances of .Select & .ActiveCell. I left them here, but you should work to remove all of them.


I believe you can use this to round up your 30 ranges into a single varible: MyUnion. Once loop is done, you can then just refer to MyUnion which contains all of the individual ranges (MyUnion.ClearContents, MyUnion.Copy, etc.)

You can apply similar logic to other loops. This will knock out your first, and larges, loop though!

Dim MyUnion As Range, iRow As Integer, iCol As Integer

For iCol = 4 To 26
    If iCol <> 7 Or iCol <> 15 Or iCol <> 19 Or iCol <> 23 Then 'Skip these columns
        For iRow = 1 To 2
            If MyUnion Is Nothing Then
                Set MyUnion = ActiveCell.Offset(iRow, iCol)
            Else
                Set MyUnion = Union(MyUnion, ActiveCell.Offset(iRow, iCol))
            End If
        Next iRow
    End If
Next iCol

Msgbox "REMOVE .SELECT OR ELSE CRANER WILL FIND YOU" vbCritical

GL :)

urdearboy
  • 14,439
  • 5
  • 28
  • 58
  • This is exactly what I was looking for. Thanks for the detailed explaination! I will remove .SELECT – gary0418 Aug 24 '18 at 17:41
  • Hi Sorry, I have deleted my previous comments. It was giving me an error before, but it's working perfectly fine now. Thanks again! Btw, do you know why I'm getting a -1 on my post? – gary0418 Aug 24 '18 at 18:07
  • I can't do myunion.paste. Are there other ways around it? – gary0418 Aug 24 '18 at 18:20
  • Idk. Maybe for posting links to photos rather the photo. This site has a learning curve so don’t worry about it :) – urdearboy Aug 24 '18 at 18:55
  • When pasting a Union, the columns or rows will not be preserved. I.e. if your union is column A and C and you paste it, it will paste in column A and B. It converts non-continuous ranges to continuous ranges – urdearboy Aug 24 '18 at 18:57
  • I’m never tried pasting onto a Union so I can’t help you there. I’ve only used them to copy ranges. You can try resizing your range to MyUnion.rows/columns.count and set the range values equal to each other – urdearboy Aug 24 '18 at 19:05
  • Thanks! I'll give it a try. Also, if I have 2 workbook and t I'd like to execute the same code in both workbook, how should I go about it? I have tried looking online, but they don't have a for loop within the code that they would like to duplicate onto another worksheet – gary0418 Aug 24 '18 at 20:01
  • I am not sure what I am doing it wrong, but for some reason the code is still deleting the cell where we specify the column not to be in. – gary0418 Aug 24 '18 at 21:22