+ Reply to Thread
Results 1 to 8 of 8

Copy cells from a table to another table with conditions.

Hybrid View

  1. #1
    Registered User
    Join Date
    01-22-2023
    Location
    Monterrey
    MS-Off Ver
    Office 365
    Posts
    5

    Lightbulb Copy cells from a table to another table with conditions.

    Hello everyone.

    I am trying to build an Excel script that basically follows the next steps:

    1. Look for the last row added to the table.
    2. Search for questions that have been answered as "Yes".
    3. If it founds a "Yes" value, it must copy the cell right next to it to another table, which is the answer given by the user. And also the name, nationality and age.

    Original Table (picture 1):

    Table 1.jpg

    Expected Table Result (picture 2):

    Table 2.png

    Here is the Excel file.
    Survey Trial (1).xlsx

    I will be very attentive to your answers. Thank you in advance.
    Last edited by AlexisUANL; 01-22-2023 at 11:55 PM. Reason: URGENT!

  2. #2
    Forum Expert BadlySpelledBuoy's Avatar
    Join Date
    06-14-2013
    Location
    East Sussex, UK
    MS-Off Ver
    365
    Posts
    7,911

    Re: Copy cells from a table to another table with conditions.

    Here is one way that will hopefully do what you need and be easy enough to understand so you can adapt it to your workbook.
    Sub MoveData()
        Dim LOSource As ListObject: Set LOSource = Sheets("Form1").ListObjects(1)
        Dim LOTarget As ListObject: Set LOTarget = Sheets("Sheet2").ListObjects(1)
        Dim LRSource As ListRow: Set LRSource = LOSource.ListRows(LOSource.ListRows.Count)
        Dim LRTarget As ListRow
        Dim i As Integer
        
        With LOSource
            For i = 8 To 23 Step 2
                If .Range(.ListRows.Count + 1, i) = "Yes" Then
                    Set LRTarget = LOTarget.ListRows.Add
                    LRTarget.Range(1) = LRSource.Range(5)
                    LRTarget.Range(2) = LRSource.Range(i).Offset(, 1)
                    LRTarget.Range(3) = LRSource.Range(27)
                    LRTarget.Range(4) = LRSource.Range(6)
                    LRTarget.Range(5) = LRSource.Range(7)
                End If
            Next i
        End With
    End Sub
    BSB

  3. #3
    Registered User
    Join Date
    01-22-2023
    Location
    Monterrey
    MS-Off Ver
    Office 365
    Posts
    5

    Re: Copy cells from a table to another table with conditions.

    Hello, thanks for the answer. I am using Excel Online and it does not support Macros. What I am trying to do is to create an Script and then activates it automatically with Power Automate every time a new response is received.

  4. #4
    Forum Expert BadlySpelledBuoy's Avatar
    Join Date
    06-14-2013
    Location
    East Sussex, UK
    MS-Off Ver
    365
    Posts
    7,911

    Re: Copy cells from a table to another table with conditions.

    Quote Originally Posted by AlexisUANL View Post
    What I am trying to do is to create an Script and then activates it automatically with Power Automate every time a new response is received.
    Quite an important piece of information to omit from the original request. A) because if you'd included it you could have attracted someone that could help and B) because it would have saved those that have looked and provided solutions so far from spending time doing that for no reason.

    I'm afraid I cannot help with the scripting for Power Automate as I don't have access to it.

    Good luck with the project.

    BSB

  5. #5
    Registered User
    Join Date
    01-22-2023
    Location
    Monterrey
    MS-Off Ver
    Office 365
    Posts
    5

    Re: Copy cells from a table to another table with conditions.

    Thanks for the answer and the time. I am just struggling in making the Script, your code in VBA really help me a lot so far, but I am just looking how to do the same thing but with TypeScript language.

    This all because it is an Excel file located in One drive and I can only use Scripts, not macros

  6. #6
    Valued Forum Contributor
    Join Date
    12-01-2011
    Location
    Philippines
    MS-Off Ver
    Excel 2021
    Posts
    979

    Re: Copy cells from a table to another table with conditions.

    try

    Option Explicit
    
    Sub check_yes()
    Dim lRow As Long, lRow2 As Long, i As Long
    Dim strFind As String
    Dim oRng As Range, fRng As Range
    
    lRow = Sheets("Form1").Cells(Rows.Count, 5).End(xlUp).Row
    strFind = "Yes" ' string to find
    Set oRng = Sheets("Form1").Range("E" & lRow & ":" & "X" & lRow) ' Range to search
    
        Set fRng = oRng.Cells(oRng.Cells.Count)
        For i = 1 To Application.CountIf(oRng, strFind)
            Set fRng = oRng.Cells.Find(What:=strFind, _
                                       LookIn:=xlValues, _
                                       LookAt:=xlWhole, _
                                       After:=fRng, _
                                       MatchCase:=False)
            If Not fRng Is Nothing Then
                lRow2 = Sheets("Sheet2").Cells(Rows.Count, 3).End(xlUp).Row + 1
                Sheets("Sheet2").Range("C" & lRow2) = Sheets("Form1").Range("E" & lRow)
                Sheets("Sheet2").Range("D" & lRow2) = fRng.Offset(0, 1).Value
                Sheets("Sheet2").Range("E" & lRow2) = Sheets("Form1").Range("AA" & lRow)
                Sheets("Sheet2").Range("F" & lRow2) = Sheets("Form1").Range("F" & lRow)
                Sheets("Sheet2").Range("G" & lRow2) = Sheets("Form1").Range("G" & lRow)
            End If
        Next i
    End Sub

  7. #7
    Registered User
    Join Date
    01-22-2023
    Location
    Monterrey
    MS-Off Ver
    Office 365
    Posts
    5

    Re: Copy cells from a table to another table with conditions.

    Hello, would it be possible to transfer this to TypeScript language?, i am using Excel Online and does not support Macros.

  8. #8
    Registered User
    Join Date
    01-24-2023
    Location
    Los Angeles
    MS-Off Ver
    Office 365
    Posts
    1

    Re: Copy cells from a table to another table with conditions.

    You can try the script below. I show that it is working with the file you provided:

    function main(workbook: ExcelScript.Workbook) {
    let inputTbl = workbook.getTable("Table1")
    let outputTbl = workbook.getTable("Table3")

    let nameCol = inputTbl.getColumnByName("Name").getRangeBetweenHeaderAndTotal().getValues() as string[][]
    let nationCol = inputTbl.getColumnByName("Write your nacionality.").getRangeBetweenHeaderAndTotal().getValues() as string[][]
    let cityCol = inputTbl.getColumnByName("Choose you city.").getRangeBetweenHeaderAndTotal().getValues() as string[][]
    let ageCol = inputTbl.getColumnByName("Write your age.").getRangeBetweenHeaderAndTotal().getValues() as string[][]

    let inputVals = inputTbl.getRangeBetweenHeaderAndTotal().getValues() as string[][]

    let inputRowCount = inputTbl.getRangeBetweenHeaderAndTotal().getRowCount()
    let inputColCount = inputTbl.getRangeBetweenHeaderAndTotal().getColumnCount()
    let tempArr: string[][] = []

    for (let i = 0; i < inputRowCount; i++) {
    for (let j = 0; j < inputColCount; j++) {
    if (inputVals[i][j] === "Yes") {
    let finding = inputVals[i][j + 1]
    let temp: string[] = [nameCol[i][0], finding, nationCol[i][0], cityCol[i][0], ageCol[i][0]]
    tempArr.push(temp)
    }
    }
    }

    outputTbl.addRows(-1,tempArr)

    }
    Last edited by b-gonzalez; 01-25-2023 at 10:54 PM.

+ 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. [SOLVED] Need formula to count table range depending on conditions in selected table columns
    By jj2105 in forum Excel Formulas & Functions
    Replies: 29
    Last Post: 05-27-2022, 04:49 AM
  2. Macro to fill 1st table with info from 2nd table. 2nd table search with 2 conditions
    By Ribeiro.JD in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 11-17-2016, 02:03 PM
  3. [SOLVED] copy dynamic data from one table to another one with 2 conditions
    By sa4a in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-08-2014, 04:41 PM
  4. Macro to copy a table and paste in another table (based on certain conditions)
    By acsishere in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 05-22-2013, 01:14 PM
  5. Merging Table Cells with Text under Certain Conditions
    By verdecove in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 08-31-2009, 08:08 PM
  6. Moving data from a pivot table to another table based on conditions
    By MarinaDBrown in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 04-22-2009, 12:11 PM
  7. [SOLVED] sum cells in table using column and row conditions
    By NEIL@NEIL1975.PLUS.COM in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 06-06-2006, 03:10 AM

Tags for this Thread

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