+ Reply to Thread
Results 1 to 6 of 6

Copy data into specific columns using array

  1. #1
    Registered User
    Join Date
    09-18-2021
    Location
    Aarhus, Denmark
    MS-Off Ver
    O365
    Posts
    3

    Copy data into specific columns using array

    I'm trying to copy data from one sheet to another, but into columns arranged differently.

    I just an subscript error?

    Help is much appreciated

    Please Login or Register  to view this content.
    Last edited by KimK78; 09-19-2021 at 03:51 AM.

  2. #2
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,618

    Re: Copy data into specific columns using array

    First, try adding
    Please Login or Register  to view this content.
    and delete (or just comment out)
    Please Login or Register  to view this content.
    It could be wiser to:
    1) load whole range into array (in one operation, not in a loop)
    2) process it (porbably fill the second array)
    3) write whole result array in one operation into worksheet
    Last edited by Kaper; 09-19-2021 at 08:37 AM.
    Best Regards,

    Kaper

  3. #3
    Registered User
    Join Date
    09-18-2021
    Location
    Aarhus, Denmark
    MS-Off Ver
    O365
    Posts
    3

    Re: Copy data into specific columns using array

    Thanks Kasper, I'll test it out in the morning

  4. #4
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,618

    Re: Copy data into specific columns using array

    Looking for your observations after testing.

    If not everything is clear, see the yellow banner near the top of the page.

    Working with sample data makes life much easier

  5. #5
    Registered User
    Join Date
    09-18-2021
    Location
    Aarhus, Denmark
    MS-Off Ver
    O365
    Posts
    3

    Re: Copy data into specific columns using array

    Thanks Kaper, it works now:

    Sub filtercopyrange()

    Dim x As Long, cls
    Dim DataArr() As String
    Dim iCount As Integer
    Dim rng1 As Range
    Dim sh1 As Worksheet, sh2 As Worksheet
    Dim fcol As Integer
    Dim lcol As Integer
    Dim valuee1 As Integer
    Dim lRow2 As Long
    Dim lRow1 As Long
    Dim iCntr As Long
    Dim i As Integer
    Dim ct As Variant

    Set sh1 = Sheets("Dataset")
    Set sh2 = Sheets("Forside")

    Sheets("Forside").Activate

    Application.ScreenUpdating = False

    Range("A7:Y5000").Clear

    If IsNumeric(Range("D2").Value) = False Then
    MsgBox "Vælg kontraktperiode først"
    Exit Sub
    Else

    lRow2 = sh1.Cells(Rows.Count, "A").End(xlUp).Row

    Sheets("Dataset").Activate

    valuee1 = Sheets("Forside").Range("D2").Value

    iCount = 6
    ReDim DataArr(1 To lRow2, 0 To 9)
    For i = 2 To lRow2

    ct = Range("L" & i).Value

    If ct = valuee1 Then
    iCount = iCount + 1
    DataArr(i, 0) = Range("A" & i).Value2 'ID
    DataArr(i, 1) = Range("D" & i).Value2 'Address
    DataArr(i, 2) = Range("E" & i).Value2 'Number
    DataArr(i, 3) = Range("F" & i).Value2 'Country
    DataArr(i, 4) = Range("G" & i).Value2 'City
    DataArr(i, 5) = Range("H" & i).Value2 'Zip
    DataArr(i, 6) = Range("I" & i).Value2 'Product
    DataArr(i, 7) = Range("J" & i).Value2 'Bandwidth
    DataArr(i, 8) = Range("R" & i).Value2 'NRC
    DataArr(i, 9) = Range("S" & i).Value2 'MRC


    Sheets("Forside").Range("B" & iCount).Value2 = DataArr(i, 0) 'ID
    Sheets("Forside").Range("C" & iCount).Value2 = DataArr(i, 1) 'Address
    Sheets("Forside").Range("D" & iCount).Value2 = DataArr(i, 2) 'Number
    Sheets("Forside").Range("G" & iCount).Value2 = DataArr(i, 3) 'Country
    Sheets("Forside").Range("F" & iCount).Value2 = DataArr(i, 4) 'City
    Sheets("Forside").Range("E" & iCount).Value2 = DataArr(i, 5) 'Zip
    Sheets("Forside").Range("H" & iCount).Value2 = DataArr(i, 6) 'Product
    Sheets("Forside").Range("I" & iCount).Value2 = DataArr(i, 7) 'Bandwidth
    Sheets("Forside").Range("K" & iCount).Value2 = DataArr(i, 8) 'NRC
    Sheets("Forside").Range("L" & iCount).Value2 = DataArr(i, 9) 'MRC
    Else
    End If

    Next

    Sheets("Forside").Activate

    Application.ScreenUpdating = True


    End If
    End Sub

  6. #6
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,618

    Re: Copy data into specific columns using array

    If that takes care of your original question, please select Thread Tools from the menu link above and mark this thread as SOLVED.

    Also, you may not be aware that you can thank those who have helped you by clicking the small star icon located in the lower left corner of the post in which the help was given. By doing so you can add to the reputation(s) of those who helped.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Copy non blank data from columns into 2D array
    By kmmadhu in forum Excel Programming / VBA / Macros
    Replies: 14
    Last Post: 03-27-2021, 12:00 PM
  2. [SOLVED] Need help on copy data and paste to the specific columns in another worksheet
    By nelsonlauo in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 11-19-2019, 06:25 AM
  3. [SOLVED] Macro to copy data from three columns to a specific column
    By jackyung in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-10-2017, 05:16 AM
  4. [SOLVED] copy data in specific columns to another sheet
    By steve400243 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-28-2017, 10:05 AM
  5. Replies: 8
    Last Post: 04-04-2013, 08:02 PM
  6. [SOLVED] Adding Record to data table + preserve array formulas in specific columns
    By olivierpbeland in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-21-2013, 02:33 PM
  7. Macro to copy specific data to specific columns from a pivot table
    By Raju Radhakrishnan in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 04-09-2012, 07:24 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1