+ Reply to Thread
Results 1 to 8 of 8

vba code to extract data from data table and update data sheet

Hybrid View

JEAN1972 vba code to extract data from... 05-24-2017, 02:26 PM
leelnich Re: vba code to extract data... 05-24-2017, 03:08 PM
JEAN1972 Re: vba code to extract data... 05-24-2017, 10:26 PM
leelnich Re: vba code to extract data... 05-24-2017, 05:29 PM
leelnich Re: vba code to extract data... 05-25-2017, 12:56 AM
JEAN1972 Re: vba code to extract data... 05-25-2017, 11:30 AM
leelnich Re: vba code to extract data... 05-25-2017, 12:55 PM
JEAN1972 Re: vba code to extract data... 05-25-2017, 01:23 PM
  1. #1
    Forum Contributor
    Join Date
    02-11-2014
    Location
    mauritius
    MS-Off Ver
    MS365
    Posts
    1,141

    vba code to extract data from data table and update data sheet

    Hi All

    I have a workbook with table 1 and data sheet .

    the data sheet requires an index and match from column I of data sheet to table 1 sheet column A.

    extract data from column C, D,E,F to paste to data sheet from column AS TO AW.

    however if data sheet column K has SADC IN ROW 2 then INSERT header SADC IN AX 1, then put Y if there are sadc reference and adjust column AT on data sheet to 0.

    otherwise if there is no sadc then put the rate as from table 1 sheet to 15 to datasheet from column D and put N IN COLUMN AT

    Note the datasheet goes to 5000 to 20000 rows doing this manually is a true pain
    Attached Files Attached Files
    Last edited by JEAN1972; 05-24-2017 at 02:29 PM.

  2. #2
    Forum Expert leelnich's Avatar
    Join Date
    03-20-2017
    Location
    Delaware, USA
    MS-Off Ver
    Office 2016
    Posts
    2,807

    Re: vba code to extract data from data table and update data sheet

    Hi Jean
    Quote Originally Posted by JEAN1972 View Post
    otherwise if there is no sadc then put the rate as from table 1 sheet to 15 to datasheet from column D and put N IN COLUMN AT
    Could you please clarify: is this data coming from 'Table 1'!column D, or from 'Table 1'!column N? Both have a value of 15.
    Last edited by leelnich; 05-24-2017 at 03:23 PM.

  3. #3
    Forum Contributor
    Join Date
    02-11-2014
    Location
    mauritius
    MS-Off Ver
    MS365
    Posts
    1,141

    Re: vba code to extract data from data table and update data sheet

    Quote Originally Posted by leelnich View Post
    Hi JeanCould you please clarify: is this data coming from 'Table 1'!column D, or from 'Table 1'!column N? Both have a value of 15.
    Hi Leelnich

    time difference , it was late night on my side.

    So to clarify the data come from table1 , so if there is sadc on data sheet column K2 then it stands y (yes) otherwise if k3 is blank then it puts 15 according to column tariff no column I of data sheet which is related to table 1 ff tariff column A.
    I reupload the file there was error on my side on data sheet
    Attached Files Attached Files

  4. #4
    Forum Expert leelnich's Avatar
    Join Date
    03-20-2017
    Location
    Delaware, USA
    MS-Off Ver
    Office 2016
    Posts
    2,807

    Re: vba code to extract data from data table and update data sheet

    Post #3 code and workbook updated to Final Versions 25 May 2017 12:50
    I didn't get an answer to my question, so I set Column AT = corresponding 'Table 1' values (Column D) in rows where no SADC was found. Hope this suits-Lee
    Sub DataSheetUpdate()
    
    Dim SourceRange As Range
    Dim DestinationRange As Range
    Dim found As Range
    Dim LookupRange As Range
    Dim sLastRow As Long
    Dim dLastRow As Long
    Dim rw As Long
    Dim WS1 As Worksheet
    Dim WS2 As Worksheet
    Dim SADCs As Boolean
    
    Set WS1 = Worksheets("table 1")
    Set WS2 = Worksheets("DATA SHEET")
    
    With WS1
        Set SourceRange = .Columns("C:F")
        sLastRow = .Cells(.Rows.Count, 1).End(xlUp).Row
        Set LookupRange = .Cells(2, "A").Resize(sLastRow - 1)
    End With
    
    With WS2
        Set DestinationRange = .Columns("AS:AV")
        dLastRow = .Cells(.Rows.Count, 1).End(xlUp).Row
    
        Set found = .Columns("K").Find("SADC*(", LookIn:=xlFormulas, Lookat:=xlPart)
        If Not found Is Nothing Then
            .Cells(1, "AW").Resize(dLastRow).Copy
            .Cells(1, "AX").PasteSpecial xlPasteFormats
            .Cells(1, "AX") = "SADC"
            SADCs = True
            Application.CutCopyMode = False
        End If
    
    For rw = 2 To dLastRow
        Set found = LookupRange.Find(.Cells(rw, "I"))
        If found Is Nothing Then
            DestinationRange.Rows(rw) = "XXX"
            Debug.Print "Tariff # not found on row " & rw
        Else
            DestinationRange.Rows(rw).Value = SourceRange.Rows(found.Row).Value
            .Cells(rw, "AW").Value = WS1.Cells(found.Row, "O")
            If SADCs Then
                On Error Resume Next
                If Left(.Cells(rw, "K"), 4) = "SADC" Then
                    .Cells(rw, "AT") = 0
                    .Cells(rw, "AX") = "Y"
                Else
                    .Cells(rw, "AX") = "N"
                End If
                On Error GoTo 0
            End If
        End If
    Next
    
    End With
    
    End Sub
    Please click the Add Reputation star below any helpful posts, and if you have your answer, mark your thread as SOLVED (Thread Tools up top). Thanks!-Lee
    Attached Files Attached Files
    Last edited by leelnich; 05-25-2017 at 02:29 PM.

  5. #5
    Forum Expert leelnich's Avatar
    Join Date
    03-20-2017
    Location
    Delaware, USA
    MS-Off Ver
    Office 2016
    Posts
    2,807

    Re: vba code to extract data from data table and update data sheet

    Yes, I corrected the difference between the 2 sheets. So, my code already does this:

    .If DATA cell K2 has a SADC number, add a SADC column (AX). Formats for new column are copied from column AW.
    .For each row in DATA, use the Tariff Number (cell I) to find the corresponding row in TABLE 1 (cell A).
    .Copy cells C,D,E,F,O from TABLE 1 row to DATA row cells AS,AT,AU,AV,AW.
    .If DATA row cell K has a SADC number, put "Y" in cell AX and CHANGE cell AT = 0...
    ....Else if cell K is blank (but SADCs are tracked), put "N" in cell AX. Cell AT RETAINS value from TABLE 1 row cell D.

    Do I need to change anything? For example:
    .Was I correct in assuming DATA column AW takes values from TABLE 1 column O? You mentioned AW, but not O.
    .If SADCs are present, will row 2 ALWAYS have one, or should I just check entire column before looping through rows?
    .What should happen if NO corresponding row is found in TABLE 1? (Current code puts "XXX" in DATA cells AS,AT,AU,AV)

    Please click the Add Reputation star below any helpful posts, and if you have your answer, mark your thread as SOLVED (Thread Tools up top). Thanks!-Lee
    Last edited by leelnich; 05-25-2017 at 01:38 AM.

  6. #6
    Forum Contributor
    Join Date
    02-11-2014
    Location
    mauritius
    MS-Off Ver
    MS365
    Posts
    1,141

    Re: vba code to extract data from data table and update data sheet

    to first question DATA column AW takes values from TABLE 1 column O , yes .

    2)no , at row 2 there may be no sadc, as there may be loops , that is row 2 no sadc to row 5 , then row 6 sadc , then row 7 no sadc , row 8 to row 50 sadc , row51 to 150 no sadc , then row 152 to row 350 sadc and so on .it's uneven and go up to 1000 rows or more

    So data sheet check column I with table 1 column AA, THEN check K2 (DATA SHEET)TO, IF there is sadc in row2 then data extract from table 1 column D , E , F ,I and O but inserting SADC header at a AX.

    So to my opinion it should check first column K sadc header of data sheet up to last data row , if there is sadc eg row 50 , then it should insert the sadc header at column X , then process

    So if there is no sadc in column K of datasheet to last data row then d ont insert header sadc at column AX but extract data from COLUMN C, D,E,F and O of table as it is .

    So if only there is sadc is column K datasheet that it should insert sadc header at column AX , put Y if sadc and N if the row is blank

  7. #7
    Forum Expert leelnich's Avatar
    Join Date
    03-20-2017
    Location
    Delaware, USA
    MS-Off Ver
    Office 2016
    Posts
    2,807

    Re: vba code to extract data from data table and update data sheet

    Hi Jean- Please see post #3 for revised code and workbook. Hope it works as desired.

  8. #8
    Forum Contributor
    Join Date
    02-11-2014
    Location
    mauritius
    MS-Off Ver
    MS365
    Posts
    1,141

    Re: vba code to extract data from data table and update data sheet

    Hi leelnich

    One thing bravo , the code rocks , works as a charm

    Saved me from a daily tedious task

    Thank you

+ 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. VBA code to extract and reorganize data from table in Excel
    By Atom in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-27-2016, 01:13 PM
  2. [SOLVED] Vba code to extract data from one sheet to another
    By xlhelp7 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-03-2016, 01:17 AM
  3. Vba code to Extract data from multiple sheet
    By Masa1989 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 08-05-2015, 01:51 AM
  4. Replies: 3
    Last Post: 12-18-2014, 10:27 AM
  5. Replies: 1
    Last Post: 12-13-2013, 03:03 AM
  6. Replies: 1
    Last Post: 11-28-2013, 02:10 AM
  7. Search for Excel file and the sheet within that from the given table data and extract data
    By adrian_slash in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 08-23-2013, 07:55 AM

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