VBA - Copying Non-Contiguous Ranges in/out of (an) Array(s)

beartooth91

Board Regular
Joined
Dec 15, 2024
Messages
63
Office Version
  1. 365
  2. 2019
  3. 2016
Platform
  1. Windows
I have lots of worksheets which routinely get copied into a master sheet. The VBA for one example is below. Everything works but it does get a bit slow as each sheet gets copied from. I'm wondering if using an array - or arrays - would speed it up? The main problem is the copy from / paste to data is in non-contiguous ranges.

VBA Code:
Sub Import_109C_IOList_New()
'
' NOTE: This macro is for 109 column instrument templates!
' It copies data from a 109-Column IO/Instrument List in the Non-Standard IO Lists folder to a new worksheet in the Master IO List workbook.
'
'------------------------------Copy all the below into each non-std IO List import sub
'Declare Variables-------------------------------------------------------------------------------
Dim sheetExists As Boolean
Dim StartRow As Long, LastRow As Long
Dim wsname As String
Dim nme() As String
Dim wbMaster As Workbook, wbOpened As Workbook  '<-----use this instead of wbname?
Dim wS As Worksheet

Set wbMaster = Workbooks("NIC Master IO List.xlsm")
Set wbOpened = ActiveWorkbook
 
  With wbOpened.Sheets(1)
    nme = Split(.Cells(11, "M").Value, "-", -1)  '<-----These two lines need moved into the subs for non-standard IO import
    wsname = nme(1)  '<-------------------------------
  End With
  'wbMaster.Activate  '<----Is this needed?
  With wbMaster
    For Each wS In .Worksheets
      If wS.Name = wsname Then
         sheetExists = True
         '------------------------------------r1
         'GoTo ThisLine        'Or use Exit For (next line)
         Exit For
      Else                    '<---------------Is this needed?
         sheetExists = False
      End If
    Next
    '------------------------------------------r1

    ' Adjust the below for current import sub Copy/Destination code
    If Not sheetExists Then
           wbMaster.Sheets(1).Copy After:=wbMaster.Sheets(wbMaster.Worksheets.Count)
           ActiveSheet.Name = wsname
           
           '.Sheets("NewSheet").Name = wsname
    End If
    '-------------------------------------------
    StartRow = .Sheets(wsname).Cells(Rows.Count, "B").End(xlUp).Row + 1
  End With
   
'ThisLine:
  'wbOpened.Activate--------------------??
  With wbOpened.Sheets(1)
    LastRow = .Cells(Rows.Count, "B").End(xlUp).Row
    '<-------------------------------------------r1
    'Deleted the If Then Else, kept the copy/paste
'
'Copy Component Number
    wbOpened.Worksheets(1).Range("M11:M" & LastRow).Copy
    Workbooks("NIC Master IO List.xlsm").Worksheets(wsname).Range("B" & StartRow).PasteSpecial Paste:=xlPasteValues
'Copy Unit Number
    wbOpened.Worksheets(1).Range("B11:B" & LastRow).Copy
    Workbooks("NIC Master IO List.xlsm").Worksheets(wsname).Range("C" & StartRow).PasteSpecial Paste:=xlPasteValues
'Copy System Desig, CC, Seq Number, and Parallel Desig
    wbOpened.Worksheets(1).Range("D11:G" & LastRow).Copy
    Workbooks("NIC Master IO List.xlsm").Worksheets(wsname).Range("D" & StartRow).PasteSpecial Paste:=xlPasteValues
'Copy Channel/Div
    wbOpened.Worksheets(1).Range("K11:K" & LastRow).Copy
    Workbooks("NIC Master IO List.xlsm").Worksheets(wsname).Range("H" & StartRow).PasteSpecial Paste:=xlPasteValues
'Copy Desc, Noun Name, Design Phase, Bldg, Location, Elevation, Doc/Dwg, Datasheet, Parent, Safety Class, Safety Function
    wbOpened.Worksheets(1).Range("N11:Y" & LastRow).Copy
    Workbooks("NIC Master IO List.xlsm").Worksheets(wsname).Range("I" & StartRow).PasteSpecial Paste:=xlPasteValues

'Copy Class 1E
    wbOpened.Worksheets(1).Range("AC11:AC" & LastRow).Copy
    Workbooks("NIC Master IO List.xlsm").Worksheets(wsname).Range("X" & StartRow).PasteSpecial Paste:=xlPasteValues
'Copy Voltage, SDG, and Off-Site Power Feed
    wbOpened.Worksheets(1).Range("AF11:AH" & LastRow).Copy
    Workbooks("NIC Master IO List.xlsm").Worksheets(wsname).Range("Y" & StartRow).PasteSpecial Paste:=xlPasteValues
'Copy Weight, MR/PR, Vendor Supplied, Fab Cat, Dest, Vendor Ins, Manufacturer, Model, Equip No.
    wbOpened.Worksheets(1).Range("AL11:AT" & LastRow).Copy
    Workbooks("NIC Master IO List.xlsm").Worksheets(wsname).Range("AC" & StartRow).PasteSpecial Paste:=xlPasteValues
'Copy Resp Design Org and Remarks
    wbOpened.Worksheets(1).Range("BA11:BB" & LastRow).Copy
    Workbooks("NIC Master IO List.xlsm").Worksheets(wsname).Range("AM" & StartRow).PasteSpecial Paste:=xlPasteValues
'Copy Instrument Data Fields
    wbOpened.Worksheets(1).Range("BF11:CF" & LastRow).Copy
    Workbooks("NIC Master IO List.xlsm").Worksheets(wsname).Range("AO" & StartRow).PasteSpecial Paste:=xlPasteValues

  wbOpened.Close
End With

    'Formatting
    Workbooks("NIC Master IO List.xlsm").Activate '<----
    fr = Cells(1, 1).SpecialCells(xlLastCell).Row
    Workbooks("NIC Master IO List.xlsm").Worksheets("Instrument List Template").Range("B11:BO11").Copy
    Workbooks("NIC Master IO List.xlsm").Worksheets(wsname).Range("B11:BO" & fr).PasteSpecial Paste:=xlPasteFormats
    '
    Application.ScreenUpdating = True
    
    Application.CutCopyMode = False
    Workbooks("NIC Master IO List.xlsm").Save
 '
End Sub
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Try. Replace this
VBA Code:
    wbOpened.Worksheets(1).Range("M11:M" & lastrow).Copy
    Workbooks("NIC Master IO List.xlsm").Worksheets(wsname).Range("B" & startrow).PasteSpecial Paste:=xlPasteValues
'Copy Unit Number
    wbOpened.Worksheets(1).Range("B11:B" & lastrow).Copy
    Workbooks("NIC Master IO List.xlsm").Worksheets(wsname).Range("C" & startrow).PasteSpecial Paste:=xlPasteValues
'Copy System Desig, CC, Seq Number, and Parallel Desig
    wbOpened.Worksheets(1).Range("D11:G" & lastrow).Copy
    Workbooks("NIC Master IO List.xlsm").Worksheets(wsname).Range("D" & startrow).PasteSpecial Paste:=xlPasteValues
'Copy Channel/Div
    wbOpened.Worksheets(1).Range("K11:K" & lastrow).Copy
    Workbooks("NIC Master IO List.xlsm").Worksheets(wsname).Range("H" & startrow).PasteSpecial Paste:=xlPasteValues
'Copy Desc, Noun Name, Design Phase, Bldg, Location, Elevation, Doc/Dwg, Datasheet, Parent, Safety Class, Safety Function
    wbOpened.Worksheets(1).Range("N11:Y" & lastrow).Copy
    Workbooks("NIC Master IO List.xlsm").Worksheets(wsname).Range("I" & startrow).PasteSpecial Paste:=xlPasteValues

'Copy Class 1E
    wbOpened.Worksheets(1).Range("AC11:AC" & lastrow).Copy
    Workbooks("NIC Master IO List.xlsm").Worksheets(wsname).Range("X" & startrow).PasteSpecial Paste:=xlPasteValues
'Copy Voltage, SDG, and Off-Site Power Feed
    wbOpened.Worksheets(1).Range("AF11:AH" & lastrow).Copy
    Workbooks("NIC Master IO List.xlsm").Worksheets(wsname).Range("Y" & startrow).PasteSpecial Paste:=xlPasteValues
'Copy Weight, MR/PR, Vendor Supplied, Fab Cat, Dest, Vendor Ins, Manufacturer, Model, Equip No.
    wbOpened.Worksheets(1).Range("AL11:AT" & lastrow).Copy
    Workbooks("NIC Master IO List.xlsm").Worksheets(wsname).Range("AC" & startrow).PasteSpecial Paste:=xlPasteValues
'Copy Resp Design Org and Remarks
    wbOpened.Worksheets(1).Range("BA11:BB" & lastrow).Copy
    Workbooks("NIC Master IO List.xlsm").Worksheets(wsname).Range("AM" & startrow).PasteSpecial Paste:=xlPasteValues
By
VBA Code:
Dim Ary
Ary = wbOpened.Worksheets(1).Range("A11:BB" & lastrow)
With Workbooks("NIC Master IO List.xlsm").Worksheets(wsname).Range("A" & startrow).Resize(lastrow - 10,1)
.Offset(0, Range("B1").Column - 1) = WorksheetFunction.Index(Ary, 0, Range("M1").Column)
.Offset(0, Range("C1").Column - 1) = WorksheetFunction.Index(Ary, 0, Range("B1").Column)
.Offset(0, Range("D1").Column - 1) = WorksheetFunction.Index(Ary, 0, Range("D1").Column)
.Offset(0, Range("H1").Column - 1) = WorksheetFunction.Index(Ary, 0, Range("K1").Column)
.Offset(0, Range("I1").Column - 1) = WorksheetFunction.Index(Ary, 0, Range("N1").Column)
.Offset(0, Range("X1").Column - 1) = WorksheetFunction.Index(Ary, 0, Range("AC1").Column)
.Offset(0, Range("Y1").Column - 1) = WorksheetFunction.Index(Ary, 0, Range("AF1").Column)
.Offset(0, Range("AC1").Column - 1) = WorksheetFunction.Index(Ary, 0, Range("AL1").Column)
.Offset(0, Range("AM1").Column - 1) = WorksheetFunction.Index(Ary, 0, Range("BA1").Column)
.Offset(0, Range("AN1").Column - 1) = WorksheetFunction.Index(Ary, 0, Range("BB1").Column)
End With
 
Upvote 0
Try. Replace this
VBA Code:
    wbOpened.Worksheets(1).Range("M11:M" & lastrow).Copy
    Workbooks("NIC Master IO List.xlsm").Worksheets(wsname).Range("B" & startrow).PasteSpecial Paste:=xlPasteValues
'Copy Unit Number
    wbOpened.Worksheets(1).Range("B11:B" & lastrow).Copy
    Workbooks("NIC Master IO List.xlsm").Worksheets(wsname).Range("C" & startrow).PasteSpecial Paste:=xlPasteValues
'Copy System Desig, CC, Seq Number, and Parallel Desig
    wbOpened.Worksheets(1).Range("D11:G" & lastrow).Copy
    Workbooks("NIC Master IO List.xlsm").Worksheets(wsname).Range("D" & startrow).PasteSpecial Paste:=xlPasteValues
'Copy Channel/Div
    wbOpened.Worksheets(1).Range("K11:K" & lastrow).Copy
    Workbooks("NIC Master IO List.xlsm").Worksheets(wsname).Range("H" & startrow).PasteSpecial Paste:=xlPasteValues
'Copy Desc, Noun Name, Design Phase, Bldg, Location, Elevation, Doc/Dwg, Datasheet, Parent, Safety Class, Safety Function
    wbOpened.Worksheets(1).Range("N11:Y" & lastrow).Copy
    Workbooks("NIC Master IO List.xlsm").Worksheets(wsname).Range("I" & startrow).PasteSpecial Paste:=xlPasteValues

'Copy Class 1E
    wbOpened.Worksheets(1).Range("AC11:AC" & lastrow).Copy
    Workbooks("NIC Master IO List.xlsm").Worksheets(wsname).Range("X" & startrow).PasteSpecial Paste:=xlPasteValues
'Copy Voltage, SDG, and Off-Site Power Feed
    wbOpened.Worksheets(1).Range("AF11:AH" & lastrow).Copy
    Workbooks("NIC Master IO List.xlsm").Worksheets(wsname).Range("Y" & startrow).PasteSpecial Paste:=xlPasteValues
'Copy Weight, MR/PR, Vendor Supplied, Fab Cat, Dest, Vendor Ins, Manufacturer, Model, Equip No.
    wbOpened.Worksheets(1).Range("AL11:AT" & lastrow).Copy
    Workbooks("NIC Master IO List.xlsm").Worksheets(wsname).Range("AC" & startrow).PasteSpecial Paste:=xlPasteValues
'Copy Resp Design Org and Remarks
    wbOpened.Worksheets(1).Range("BA11:BB" & lastrow).Copy
    Workbooks("NIC Master IO List.xlsm").Worksheets(wsname).Range("AM" & startrow).PasteSpecial Paste:=xlPasteValues
By
VBA Code:
Dim Ary
Ary = wbOpened.Worksheets(1).Range("A11:BB" & lastrow)
With Workbooks("NIC Master IO List.xlsm").Worksheets(wsname).Range("A" & startrow).Resize(lastrow - 10,1)
.Offset(0, Range("B1").Column - 1) = WorksheetFunction.Index(Ary, 0, Range("M1").Column)
.Offset(0, Range("C1").Column - 1) = WorksheetFunction.Index(Ary, 0, Range("B1").Column)
.Offset(0, Range("D1").Column - 1) = WorksheetFunction.Index(Ary, 0, Range("D1").Column)
.Offset(0, Range("H1").Column - 1) = WorksheetFunction.Index(Ary, 0, Range("K1").Column)
.Offset(0, Range("I1").Column - 1) = WorksheetFunction.Index(Ary, 0, Range("N1").Column)
.Offset(0, Range("X1").Column - 1) = WorksheetFunction.Index(Ary, 0, Range("AC1").Column)
.Offset(0, Range("Y1").Column - 1) = WorksheetFunction.Index(Ary, 0, Range("AF1").Column)
.Offset(0, Range("AC1").Column - 1) = WorksheetFunction.Index(Ary, 0, Range("AL1").Column)
.Offset(0, Range("AM1").Column - 1) = WorksheetFunction.Index(Ary, 0, Range("BA1").Column)
.Offset(0, Range("AN1").Column - 1) = WorksheetFunction.Index(Ary, 0, Range("BB1").Column)
End With
May I ask some questions - for understanding - before I try this?
1) The copy from worksheets *and* the paste to worksheets data start is cell B11.....nothing in Col A.
2) For the copy from worksheets; the last (furthest) copy range is BF11:CF11. I'm missing that in your code.
VBA Code:
'Copy Resp Design Org and Remarks
    wbOpened.Worksheets(1).Range("BA11:BB" & LastRow).Copy
    Workbooks("NIC Master IO List.xlsm").Worksheets(wsname).Range("AM" & StartRow).PasteSpecial Paste:=xlPasteValues
'Copy Instrument Data Fields
    wbOpened.Worksheets(1).Range("BF11:CF" & LastRow).Copy
    Workbooks("NIC Master IO List.xlsm").Worksheets(wsname).Range("AO" & StartRow).PasteSpecial Paste:=xlPasteValues
The ending range of data on the paste to worksheet is Col BO (starting at BO11)
Do the questions above require any changes to your VBA?
 
Upvote 0
I rewrote your macro to put the source data into an array.
And pass column by column to the destination sheet.

Question:
What do you have in the columns where data will not be pasted, for example columns "A", "V", "W", "AB", etc?
Do You have data or formulas or it is simply empty?
Because if they are empty, the macro can be improved and should be faster.

For the macro:
On this line you must put the source column, the destination column and the number of continuous columns to copy.
VBA Code:
arr = Array("M", "B", 1, "B", "C", 1, "D", "D", 4, "K", "H", 1, "N", "I", 12, "AC", "X", 1, _
                "AF", "Y", 3, "AL", "AC", 9, "BA", "AM", 2, "BF", "AO", 2, "BF", "AO", 27)

For example:
Rich (BB code):
'Copy System Desig, CC, Seq Number, and Parallel Desig
    wbOpened.Worksheets(1).Range("D11:G" & LastRow).Copy
    Workbooks("NIC Master IO List.xlsm").Worksheets(wsname).Range("D" & StartRow).PasteSpecial Paste:=xlPasteValues
Source Column "D"
Destination Column "D"
Number of continuous columns 4

Then:
Rich (BB code):
arr = Array("M", "B", 1, "B", "C", 1, "D", "D", 4, "K", "H", 1, "N", "I", 12, "AC", "X", 1, _
                "AF", "Y", 3, "AL", "AC", 9, "BA", "AM", 2, "BF", "AO", 2, "BF", "AO", 27)

Try this:
VBA Code:
Sub Import_109C_IOList_New()
  '
  ' NOTE: This macro is for 109 column instrument templates!
  ' It copies data from a 109-Column IO/Instrument List in the Non-Standard IO Lists folder to a new worksheet in the Master IO List workbook.
  '
  '------------------------------Copy all the below into each non-std IO List import sub
  'Declare Variables-------------------------------------------------------------------------------
  Dim sheetExists As Boolean
  Dim StartRow As Long
  Dim wsname As String
  Dim nme() As String
  Dim wbMaster As Workbook, wbOpened As Workbook  '<-----use this instead of wbname?
  Dim wS As Worksheet
  Dim fr As Long
  Dim a As Variant, arr As Variant
  Dim i As Long, k As Long, j As Long, y As Long, m As Long
  
  Set wbMaster = Workbooks("NIC Master IO List.xlsm")
  Set wbOpened = ActiveWorkbook
   
  With wbOpened.Sheets(1)
    nme = Split(.Cells(11, "M").Value, "-", -1)   '<-----These two lines need moved into the subs for non-standard IO import
    wsname = nme(1)                               '<-------------------------------
    a = .Range("A11:CF" & .Range("B" & Rows.Count).End(xlUp).Row).Value
  End With
  
  With wbMaster
    sheetExists = False
    For Each wS In .Worksheets
      If UCase(wS.Name) = UCase(wsname) Then
         sheetExists = True
         Exit For
      End If
    Next
    
    If Not sheetExists Then
      .Sheets(1).Copy After:=.Sheets(.Worksheets.Count)
      .Sheets(.Worksheets.Count).Name = wsname
    End If
    '-------------------------------------------
    StartRow = .Sheets(wsname).Cells(Rows.Count, "B").End(xlUp).Row + 1
  
    arr = Array("M", "B", 1, "B", "C", 1, "D", "D", 4, "K", "H", 1, "N", "I", 12, "AC", "X", 1, _
                "AF", "Y", 3, "AL", "AC", 9, "BA", "AM", 2, "BF", "AO", 2, "BF", "AO", 27)
    For y = 0 To UBound(arr) Step 3
      ReDim b(1 To UBound(a), 1 To arr(y + 2))
      k = 0
      For i = 1 To UBound(a)
        k = k + 1
        m = 0
        For j = Columns(arr(y)).Column To Columns(arr(y)).Column + arr(y + 2) - 1
          m = m + 1
          b(k, m) = a(i, j)
        Next
      Next
      .Sheets(wsname).Range(arr(y + 1) & StartRow).Resize(UBound(b), UBound(b, 2)).Value = b
    Next
  
  End With
  
  wbOpened.Close

  'Formatting
  Workbooks("NIC Master IO List.xlsm").Activate '<----
  fr = Cells(1, 1).SpecialCells(xlLastCell).Row
  Workbooks("NIC Master IO List.xlsm").Worksheets("Instrument List Template").Range("B11:BO11").Copy
  Workbooks("NIC Master IO List.xlsm").Worksheets(wsname).Range("B11:BO" & fr).PasteSpecial Paste:=xlPasteFormats
  '
  Application.ScreenUpdating = True
  
  Application.CutCopyMode = False
  Workbooks("NIC Master IO List.xlsm").Save
 '
End Sub


😇
 
Upvote 0
Solution
Question:
What do you have in the columns where data will not be pasted, for example columns "A", "V", "W", "AB", etc?
Do You have data or formulas or it is simply empty?
Because if they are empty, the macro can be improved and should be faster.

They either blank or contain non-pertinent data.
 
Upvote 0
I rewrote your macro to put the source data into an array.
And pass column by column to the destination sheet.

Question:
What do you have in the columns where data will not be pasted, for example columns "A", "V", "W", "AB", etc?
Do You have data or formulas or it is simply empty?
Because if they are empty, the macro can be improved and should be faster.

For the macro:
On this line you must put the source column, the destination column and the number of continuous columns to copy.
VBA Code:
arr = Array("M", "B", 1, "B", "C", 1, "D", "D", 4, "K", "H", 1, "N", "I", 12, "AC", "X", 1, _
                "AF", "Y", 3, "AL", "AC", 9, "BA", "AM", 2, "BF", "AO", 2, "BF", "AO", 27)

For example:
Rich (BB code):
'Copy System Desig, CC, Seq Number, and Parallel Desig
    wbOpened.Worksheets(1).Range("D11:G" & LastRow).Copy
    Workbooks("NIC Master IO List.xlsm").Worksheets(wsname).Range("D" & StartRow).PasteSpecial Paste:=xlPasteValues
Source Column "D"
Destination Column "D"
Number of continuous columns 4

Then:
Rich (BB code):
arr = Array("M", "B", 1, "B", "C", 1, "D", "D", 4, "K", "H", 1, "N", "I", 12, "AC", "X", 1, _
                "AF", "Y", 3, "AL", "AC", 9, "BA", "AM", 2, "BF", "AO", 2, "BF", "AO", 27)

Try this:
VBA Code:
Sub Import_109C_IOList_New()
  '
  ' NOTE: This macro is for 109 column instrument templates!
  ' It copies data from a 109-Column IO/Instrument List in the Non-Standard IO Lists folder to a new worksheet in the Master IO List workbook.
  '
  '------------------------------Copy all the below into each non-std IO List import sub
  'Declare Variables-------------------------------------------------------------------------------
  Dim sheetExists As Boolean
  Dim StartRow As Long
  Dim wsname As String
  Dim nme() As String
  Dim wbMaster As Workbook, wbOpened As Workbook  '<-----use this instead of wbname?
  Dim wS As Worksheet
  Dim fr As Long
  Dim a As Variant, arr As Variant
  Dim i As Long, k As Long, j As Long, y As Long, m As Long
 
  Set wbMaster = Workbooks("NIC Master IO List.xlsm")
  Set wbOpened = ActiveWorkbook
 
  With wbOpened.Sheets(1)
    nme = Split(.Cells(11, "M").Value, "-", -1)   '<-----These two lines need moved into the subs for non-standard IO import
    wsname = nme(1)                               '<-------------------------------
    a = .Range("A11:CF" & .Range("B" & Rows.Count).End(xlUp).Row).Value
  End With
 
  With wbMaster
    sheetExists = False
    For Each wS In .Worksheets
      If UCase(wS.Name) = UCase(wsname) Then
         sheetExists = True
         Exit For
      End If
    Next
  
    If Not sheetExists Then
      .Sheets(1).Copy After:=.Sheets(.Worksheets.Count)
      .Sheets(.Worksheets.Count).Name = wsname
    End If
    '-------------------------------------------
    StartRow = .Sheets(wsname).Cells(Rows.Count, "B").End(xlUp).Row + 1
 
    arr = Array("M", "B", 1, "B", "C", 1, "D", "D", 4, "K", "H", 1, "N", "I", 12, "AC", "X", 1, _
                "AF", "Y", 3, "AL", "AC", 9, "BA", "AM", 2, "BF", "AO", 2, "BF", "AO", 27)
    For y = 0 To UBound(arr) Step 3
      ReDim b(1 To UBound(a), 1 To arr(y + 2))
      k = 0
      For i = 1 To UBound(a)
        k = k + 1
        m = 0
        For j = Columns(arr(y)).Column To Columns(arr(y)).Column + arr(y + 2) - 1
          m = m + 1
          b(k, m) = a(i, j)
        Next
      Next
      .Sheets(wsname).Range(arr(y + 1) & StartRow).Resize(UBound(b), UBound(b, 2)).Value = b
    Next
 
  End With
 
  wbOpened.Close

  'Formatting
  Workbooks("NIC Master IO List.xlsm").Activate '<----
  fr = Cells(1, 1).SpecialCells(xlLastCell).Row
  Workbooks("NIC Master IO List.xlsm").Worksheets("Instrument List Template").Range("B11:BO11").Copy
  Workbooks("NIC Master IO List.xlsm").Worksheets(wsname).Range("B11:BO" & fr).PasteSpecial Paste:=xlPasteFormats
  '
  Application.ScreenUpdating = True
 
  Application.CutCopyMode = False
  Workbooks("NIC Master IO List.xlsm").Save
 '
End Sub


😇
Question:
What do you have in the columns where data will not be pasted, for example columns "A", "V", "W", "AB", etc?
Do You have data or formulas or it is simply empty?
Because if they are empty, the macro can be improved and should be faster.

They're either blank or contain non-pertinent data. That said, there's a bunch of different sheets that go into the master, so I can't change the master (delete columns)
 
Last edited:
Upvote 0
They're either blank or contain non-pertinent data. That said, there's a bunch of different sheets that go into the master, so I can't change the master (delete columns)
I'm not deleting columns.

Did you try the macro in post #4?
Check the execution time and comment.
 
Upvote 0
I'm not deleting columns.

Did you try the macro in post #4?
Check the execution time and comment.
I've been studying your code and will try it tomorrow.

That said, I'd really like to understand the section below. (Comments inserted in vba snip below)
It turns out I'll eventually accumulate 30-60 of these instrument lists that I'll have to import/reimport on a routine basis.
The master worksheet is in a 67 column format. About half of the import-from instrument lists are in the same format. For those, its a simple range copy/paste in one read/write. The other instrument lists are 65 column, 90 column, 95 column, or 109 column versions where the data has to be copied - and pasted - in non-contiguous column ranges. So, if I can understand how the below works; I can port this to the other versions.
I wondering if I step through it - and watch the locals window - maybe I might gain some more understanding.......

VBA Code:
arr = Array("M", "B", 1, "B", "C", 1, "D", "D", 4, "K", "H", 1, "N", "I", 12, "AC", "X", 1, _

"AF", "Y", 3, "AL", "AC", 9, "BA", "AM", 2, "BF", "AO", 2, "BF", "AO", 27)

For y = 0 To UBound(arr) Step 3
'--------------------------------------------------------------------- I believe I understand most of the above, but I lose it here at ReDim b and beyond......
ReDim b(1 To UBound(a), 1 To arr(y + 2))  '<----------------------Is it necessary to Declare b above? (I didn't see that.)

k = 0

For i = 1 To UBound(a)

k = k + 1

m = 0

For j = Columns(arr(y)).Column To Columns(arr(y)).Column + arr(y + 2) - 1

m = m + 1

b(k, m) = a(i, j)  '<-------------------???What does this do?

Next

Next

.Sheets(wsname).Range(arr(y + 1) & StartRow).Resize(UBound(b), UBound(b, 2)).Value = b

Next
 
Upvote 0
I'd really like to understand the section below
I will gladly explain the following code to you.

I wondering if I step through it
If you are really interested, you will have to read the entire explanation very carefully.

-------------------------------------------------
Before entering that part of the code, you need to know that all the data from the source sheet has been loaded into the matrix 'a' in this line:
VBA Code:
a = .Range("A11:CF" & .Range("B" & Rows.Count).End(xlUp).Row).Value
The above means that it was loaded into the matrix 'a' from cell A11 to column CF and the last row with data from column B.
To read the rows of this matrix, row 11 of your cells is in row 1 of the matrix, row 12 in row 2, row 13 in row 3, and so on.
To read the columns of this matrix, column A is column 1, column B is column 2, column C is column 3 and so on.

Now we enter the code:
Rich (BB code):
'The following arrangement configures the columns that will be copied and where they will be pasted:
    arr = Array( _ 
"M", "B", 1, _
"B", "C", 1, _
"D", "D", 4, _
"K", "H", 1, _
"N", "I", 12, _
"AC", "X", 1, "AF", "Y", 3, "AL", "AC", 9, "BA", "AM", 2, "BF", "AO", 2, "BF", "AO", 27)

'The next loop will read the data from array 'arr' 3 at a time
    For y = 0 To UBound(arr) Step 3
'In the first cycle it reads the data: "M", "B", 1
'It means that you are going to copy column M and paste it into column B, with a resize of 1 a column, that is, only column "M"

'In the second cycle it reads the data: "B", "C", 1
'It means that you are going to copy column B and paste it into column C, with a resize of 1 a column, that is, only column "B"

'In the third cycle it reads the data: "D", "D", 4
'It means that you are going to copy column D and paste it into column D, with a resize of 4 a columns, that is, columns D, E, F and G

'In the fifth cycle it reads the data: "K", "H", 1
'It means that you are going to copy column K and paste it into column H, with a resize of 1 a columns, that is, columns only column "K"

'In the sixth cycle it reads the data: "N", "I", 12
'It means that you are going to copy column N and paste it into column I, with a resize of 12 a columns, that is, columns N, O, P, Q, R, S, T, U, V, W, X and Y

'etc....

'When you use Redim it is not necessary to declare the variable, but the correct thing to do is to do it, I forgot to do it, it should go here:
' Dim a As Variant, b as Variant, arr As Variant

      ReDim b(1 To UBound(a), 1 To arr(y + 2))
'It means that the matrix is 2-dimensional. 
Every time the loop goes through here, the matrix 'b' is resized, mainly to get the number of columns.

'Rows: From 1 to ubound(a), that is, it starts at row 1 and will contain the same number of rows as the matrix 'a'
'Columns: From 1 to arr(y +2), Remember that we are in the loop to read the data from the array 'arr' so when 'y' is 0, y + 2 = 2, 
'it means that it is taking parameter 3 of the array 'arr'
'In the first cycle it reads the data: arr(y) = "M", arr(y+1) = "B", arr(y+2) =1

'In the next loop y = 4, then:
arr(y) = "B", arr(y+1) = "C", arr(y+2) =1

'In the next loop y = 7, then:
arr(y) = "D", arr(y+1) = "D", arr(y+2) =4

      k = 0
      
      'Read all rows of matrix 'a' , starting at row 1 of the matrix 'a', 
      For i = 1 To UBound(a)
        k = k + 1
        m = 0

        'First cycle Read column "M" to "M" 
        'Second cycle Read column "B" to "B"
        'third cycle read column "D" to "G"
        'and so on...
        For j = Columns(arr(y)).Column To Columns(arr(y)).Column + arr(y + 2) - 1
          m = m + 1
          b(k, m) = a(i, j)  'What does this do?  'this is the MAIN PART of the code
          'Pass the data from array 'a' to array 'b'

        Next
      Next
       'When it finishes passing data from array a to array b, it passes array 'b' into the sheet.
      .Sheets(wsname).Range(arr(y + 1) & StartRow).Resize(UBound(b), UBound(b, 2)).Value = b
    Next

The best way to understand it is to execute the macro with F8 (step by step), so you can see how matrix b is filled with the data from matrix a.


😅
 
Upvote 0

Forum statistics

Threads
1,225,533
Messages
6,185,499
Members
453,298
Latest member
Adam1258

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