Consulting

Results 1 to 4 of 4

Thread: Import from a Number of Worksheets Listed in a Column

  1. #1
    VBAX Mentor
    Joined
    Feb 2016
    Location
    I have lived in many places, I love to Travel
    Posts
    413
    Location

    Import from a Number of Worksheets Listed in a Column

    Hi folks,

    Good Tuesday all

    I have a import data issue that Im trying to solve.


    Get Data.jpg


    I have put the code togther from other code I had.

    I'm afraid its gone a bit pear shaped.


     
        Dim i            As Long
        Dim ws           As Worksheet
        Dim oSourceWorksheet    As Worksheet
        Dim oImportToWorksheet   As Worksheet
        Dim strDir As String
       
       
        Set ws = Worksheets("ImportList")
     
        For i = 3 To ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
     
            Set oSourceWorksheet = Worksheets(ws.Cells(i, "A").Value)
     
            Set oImportToWorksheet = Application.ActiveWorkbook.Path & Worksheets(ws.Cells(i, "C").Value)
     
            oSourceWorksheet.Range(ws.Cells(i, "C")).Copy Destination:=oImportToWorksheet.Range(ws.Cells(i, "E").Value)
     
          Next i
     
    End Sub
    I saw Mr Bruins work, but that copied from 1 worksheet
    http://www.rondebruin.nl/win/s3/win001.htm

    The reason I need to do it this way is because It has formatting and other bits and bobs like hyperlinks, so if I don’t paste it in - I can import only the raw data and I will lose the good bits.

    Is my code on the right track?

    Thank you for your advice and time
    Cheers for your help

    dj

    'Extreme VBA Newbie in progress - one step at a time - like a tortoise's pace'


  2. #2
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    since destination workbook is not stated i assumed it's the current workbook which the following code will be copied.

    test with a copy of the original file.

    Sub vbax_57724_import_from_listed_worksheets()
    
        Dim ImpList
        Dim i As Long
        Dim CopyRng As Range
        
        With Application
            .DisplayAlerts = False
            .ScreenUpdating = False
            .AskToUpdateLinks = False
            .EnableEvents = False
            .Calculation = xlCalculationManual
        End With
        
        With ThisWorkbook.Worksheets("ImportList")
            With .Cells(1).CurrentRegion
                ImpList = .Offset(2).Resize(.Rows.Count - 2).Value
            End With
        End With
        
        For i = LBound(ImpList, 1) To UBound(ImpList, 1)
            Workbooks.Open ImpList(i, 1)
            With ActiveWorkbook
                Set CopyRng = .Worksheets(ImpList(i, 2)).Range(ImpList(i, 3))
                ThisWorkbook.Worksheets(ImpList(i, 4)).Range(ImpList(i, 5)).Resize(CopyRng.Rows.Count, CopyRng.Columns.Count).Value = CopyRng.Value
                .Close False
            End With
        Next i
    
        With Application
            .AskToUpdateLinks = True
            .EnableEvents = True
            .Calculation = xlCalculationAutomatic
        End With
    
    End Sub
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  3. #3
    VBAX Mentor
    Joined
    Feb 2016
    Location
    I have lived in many places, I love to Travel
    Posts
    413
    Location
    Thank you Mancubus

    This is an epic line, I will have to get my study glasses on for this

    ThisWorkbook.Worksheets(ImpList(i, 4)).Range(ImpList(i, 5)).Resize(CopyRng.Rows.Count, CopyRng.Columns.Count).Value = CopyRng.Value


    It did the job

    Thank you for helping

    Have a good day !

    Cheers for your help

    dj

    'Extreme VBA Newbie in progress - one step at a time - like a tortoise's pace'


  4. #4
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    you are welcome

    it's a good practice to prepare a guide especially when dealing with complex projects.

    i personally dont use merged cells and 2 header lines, btw.
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

Posting Permissions

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