Application.Union VBA in specified order

Mojo689

New Member
Joined
Feb 18, 2016
Messages
12
Office Version
  1. 2016
Platform
  1. Windows
I have this vba code that is taking specific columns from a worksheet, and copying and pasting them into another worksheet. It works for other worksheets I'm copying because the columns being copied are in alphabetical order; however, for this specific worksheet, I want it in the order specified (seen below). I know that in order to do this, I just need to create an array of the column order. My problem is, I'm not sure how to include that array into my code, or how that specific line of copying code would change. Any suggestions that would help would be appreciated :)

'episign table for COPY job

Dim esrtbl As ListObject, essize As Long, lastES As Long
Set estbl = Worksheets("EpiSign").ListObjects("TES")
essize = estbl.ListRows.Count
lastES = estbl.HeaderRowRange.Offset(essize + 1).End(xlUp).Row

'episign table for PASTE job

Dim EStbl2 As ListObject, tblGES As Long, EStbl2size, tblESsize As Integer

Set EStbl2 = Worksheets("Gantt").ListObjects("SES")
tblGES = EStbl2.ListRows.Count
EStbl2size = essize - tblGES
tblESsize = tblGES - essize

With EStbl2
If tblGES < essize Then
For counter = 1 To EStbl2size
EStbl2.ListRows.Add , alwaysInsert:=True
Next counter
End If

If essize < tblGES Then
EStbl2.ListRows(2).Range.Resize(tblESsize).Delete (xlShiftUp)
End If

End With

With Worksheets("EpiSign")

Application.Union(.Range("B2:B" & lastES), .Range("P2:P" & lastES), .Range("H2:H" & lastES), .Range("L2:L" & lastES), .Range("R2:R" & lastES)).Copy
EStbl2.HeaderRowRange.Offset(1).PasteSpecial xlPasteValues
Application.CutCopyMode = False

End With


Thanks!
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
I got tired of trying to make an array work, so I solved this by just moving the one column within the original table so that my code works (the "P" column moved up to follow the B column - making the new column ranges B, G, I, M, R, which works as it's alphabetical.
 
Upvote 0

Forum statistics

Threads
1,214,918
Messages
6,122,252
Members
449,075
Latest member
staticfluids

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