{"id":9312,"date":"2021-12-01T11:01:34","date_gmt":"2021-12-01T11:01:34","guid":{"rendered":"https:\/\/lgildv5i97.onrocket.site\/answers\/?post_type=question&#038;p=9312"},"modified":"2021-12-01T11:01:57","modified_gmt":"2021-12-01T11:01:57","slug":"solved-vba-macro-to-copy-multiple-columns-in-user-defined-order","status":"publish","type":"question","link":"https:\/\/computing.net\/answers\/office\/vba-macro-to-copy-multiple-columns-in-user-defined-order\/20772.html","title":{"rendered":"Solved VBA Macro To Copy Multiple Columns In User Defined Order"},"content":{"rendered":"<p>Hi everyone,<\/p>\n<p>I am asking for help since I have got stuck with my code and barely can find solution on my own even after I tried to make a Google search on my topic.<\/p>\n<p>I have a huge data set which starts from A:AW and many columns down. In my work, I have to create several new tables (in new worksheets) based on user-defined-criterias. This working task I perform on a quite offen basis as soon as I receive the update on the sorting data set (the number of columns and criteria-column are always the same).<\/p>\n<p>VBA task: I made a code which take a given data set into input-array and depending on user-defined-criteria finds the match and copy all rows to the right.<\/p>\n<p>My personal issue:<br \/>\n1. I would like to pik up the concrete columns in a specific sequences: 19, 20, 18, 31, 28,41.<br \/>\n2. I would like my output-table to be re-sized once more so that in my new worksheet the table will start from A1:.<br \/>\nHow can I modify my VBA-macro to reach the desired result?<\/p>\n<p>Sub ComplianceTabel()<br \/>\nDim lRow As Long<br \/>\nDim lCol As Long<br \/>\nDim lCount As Long<br \/>\nDim rInputTable As Range<br \/>\nDim rTarget As Range<br \/>\nDim arInput()<br \/>\nDim arOutput()<br \/>\nDim vPattern As Variant<\/p>\n<p>On Error GoTo ErrorHandle<\/p>\n<p>vPattern = InputBox(&#8220;Angiv complience gruppe&#8221;, &#8220;Identifikator&#8221;)<br \/>\nIf Len(vPattern) = 0 Then Exit Sub<\/p>\n<p>Set rInputTable = Range(&#8220;A1&#8221;).CurrentRegion<br \/>\narInput = rInputTable.Value<br \/>\nSet rInputTable = Nothing<br \/>\nReDim arOutput(1 To UBound(arInput), 1 To UBound(arInput, 2))<\/p>\n<p>For lRow = 1 To UBound(arInput)<br \/>\nIf arInput(lRow, 22) Like vPattern Then<br \/>\nlCount = lCount + 1<br \/>\nFor lCol = 23 To UBound(arInput, 2)<br \/>\n&#8216;the output table should have colon-numbers in the next sequence: 19, 20, 18, 31, 28,41<br \/>\narOutput(lCount, lCol) = arInput(lRow, lCol)<br \/>\nNext<br \/>\nEnd If<br \/>\nNext<\/p>\n<p>If lCount = 0 Then<br \/>\nMsgBox &#8220;Ingen r\u00e6kker opfyldte s\u00f8gekriteriet.&#8221;<br \/>\nGoTo BeforeExit<br \/>\nEnd If<\/p>\n<p>Worksheets.Add<br \/>\nSet rTarget = Range(&#8220;A1&#8243;).Resize(UBound(arOutput), UBound(arOutput, 2))<br \/>\nrTarget.Value = arOutput<\/p>\n<p>BeforeExit:<br \/>\nOn Error Resume Next<br \/>\nSet rTarget = Nothing<br \/>\nErase arInput<br \/>\nErase arOutput<\/p>\n<p>Exit Sub<br \/>\nErrorHandle:<br \/>\nMsgBox Err.Description &amp; &#8221; Procedure CopyRows&#8221;<br \/>\nResume BeforeExit<br \/>\nEnd Sub<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"inline_featured_image":false,"iawp_total_views":9},"question-category":[53],"question_tags":[],"class_list":["post-9312","question","type-question","status-publish","hentry","question-category-office"],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/computing.net\/answers\/wp-json\/wp\/v2\/question\/9312","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/computing.net\/answers\/wp-json\/wp\/v2\/question"}],"about":[{"href":"https:\/\/computing.net\/answers\/wp-json\/wp\/v2\/types\/question"}],"author":[{"embeddable":true,"href":"https:\/\/computing.net\/answers\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/computing.net\/answers\/wp-json\/wp\/v2\/comments?post=9312"}],"wp:attachment":[{"href":"https:\/\/computing.net\/answers\/wp-json\/wp\/v2\/media?parent=9312"}],"wp:term":[{"taxonomy":"question-category","embeddable":true,"href":"https:\/\/computing.net\/answers\/wp-json\/wp\/v2\/question-category?post=9312"},{"taxonomy":"question_tags","embeddable":true,"href":"https:\/\/computing.net\/answers\/wp-json\/wp\/v2\/question_tags?post=9312"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}