+ Reply to Thread
Results 1 to 10 of 10

Compare multiple cells at the same time

Hybrid View

  1. #1
    Registered User
    Join Date
    04-28-2013
    Location
    Portugal
    MS-Off Ver
    Excel 2013
    Posts
    19

    Compare multiple cells at the same time

    If you could help me with my problem anaylising multiple cells at the same time, i would be appreciated.

    Here's what i mean from the code i have.

    Option Base 1
    Sub userDef()
        Dim cNum As Long
        Dim stColArray(), lstColArray() As Variant
        Dim cRowArray(), cColArray() As Variant
        Dim cSel, rSel As Range
        Dim rw, col, j, mvSt1, mvEnd1, mvSt2, mvEnd2 As Integer
    
        j = 1
        
        cNum = Application.InputBox("Number of columns:")
        ReDim rStArray(cNum)
        ReDim stColArray(cNum)
        ReDim lstColArray(cNum)
        ReDim cRowArray(cNum)
        ReDim cColArray(cNum)
        For c = 1 To cNum
            Set rSel = Application.InputBox("Select " & c & " table(s)", Type:=8)
            Set cSel = Application.InputBox("Select " & c & " cell(s) of " & c & " table", Type:=8)
            If rSel Is Nothing Then
                MsgBox "No cell selected"
                Exit Sub
            Else
                stColArray(j) = rSel.Column
                lstColArray(j) = rSel.Columns(rSel.Columns.Count).Column
                cRowArray(j) = cSel.Row
                cColArray(j) = cSel.Column
                j = j + 1
            End If
        Next c
        
        
        j = 1
        x = 1
        y = 2
        rw = cRowArray(j)
        
    nxtChk:
      
      a = Math.Round(Cells(rw, cColArray(x)).Value, 2)
      d = Math.Round(Cells(rw + 1, cColArray(x)).Value, 2)
      mvSt1 = stColArray(x)
      mvEnd1 = lstColArray(x)
    
      b = Math.Round(Cells(rw, cColArray(y)).Value, 2)
      c = Math.Round(Cells(rw + 1, cColArray(y)).Value, 2)
      mvSt2 = stColArray(y)
      mvEnd2 = lstColArray(y)
      
      stDevAB = Math.Sqr((((b - ((b + a) / 2)) ^ 2) + ((a - ((b + a) / 2)) ^ 2)) / 2)
      stDevAC = Math.Sqr((((c - ((c + a) / 2)) ^ 2) + ((a - ((c + a) / 2)) ^ 2)) / 2)
      stDevBA = Math.Sqr((((a - ((a + b) / 2)) ^ 2) + ((b - ((a + b) / 2)) ^ 2)) / 2)
      stDevBD = Math.Sqr((((d - ((d + b) / 2)) ^ 2) + ((d - ((d + b) / 2)) ^ 2)) / 2)
      stErrAB = stDevAB / Math.Sqr(2)
      stErrAC = stDevAC / Math.Sqr(2)
      stErrBA = stDevBA / Math.Sqr(2)
      stErrBD = stDevBD / Math.Sqr(2)
      
      If a > 0 And b > 0 Then
        chisqrAB = ((b - a) - 0.05) ^ 2 / a
        p_val_AB = WorksheetFunction.ChiDist(chisqrAB, 1)
        chisqrAC = ((c - a) - 0.05) ^ 2 / a
        p_val_AC = WorksheetFunction.ChiDist(chisqrAC, 1)
        chisqrBA = ((a - b) - 0.05) ^ 2 / b
        p_val_BA = WorksheetFunction.ChiDist(chisqrBA, 1)
        chisqrBD = ((d - b) - 0.05) ^ 2 / b
        p_val_BD = WorksheetFunction.ChiDist(chisqrBD, 1)
      End If
     
      If a > 0 And stDevAB > stDevAC And stErrAB > stErrAC And p_val_AB < p_val_AC Then
            For col = mvSt1 To mvEnd1
            Cells(rw, col).Insert shift:=xlDown
            Next col
      ElseIf b > 0 And stDevBA > stDevBD And stErrBA > stErrBD And p_val_BA < p_val_BD Then
            For col = mvSt2 To mvEnd2
            Cells(rw, col).Insert shift:=xlDown
            Next col
      End If
    
    
        If rw > 5 And b = 0 Then
        y = y + 1
        rw = cRowArray(j) - 1
        End If
        If rw > 5 And b = 0 And y > cNum Then Exit Sub
        rw = rw + 1
    
    GoTo nxtChk
        
    End Sub
    I've attached the sample workbook too.

    13081715551.xlsm

    Variable a and d will be the fixed from the first selected cell.
    Variable b and c will have values from the other selected cells which i want to compare against a and d.
    If you have 3 selected cells the comparision i want is cell 1 vs cell 2 and cell 1 vs cell 3 all at the same time so that the line that will be inserted will be equally distribuited.

    Thanks for your patience.
    Last edited by fgq; 08-19-2013 at 01:19 PM.

  2. #2
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Compare multiple cells at the same time

    Hi,

    I for one find it impossible to visualise what your code is doing to your data, largely because I can't see the data.

    You would be better advised to upload your workbook, manually add whatever results you expect to see after a macro (not necessarily the one you give) has run and describe in a narrative form how you arrive at the results.
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  3. #3
    Registered User
    Join Date
    04-28-2013
    Location
    Portugal
    MS-Off Ver
    Excel 2013
    Posts
    19

    Re: Compare multiple cells at the same time

    Hi,

    I'm sorry for the confusion i'll try to explain the best i can.

    In the sample workbook i've uploaded you have 3 tables and in each table the last column (with the numbers in red) is the time column. The first time column is the reference time for the other columns.
    What i'm trying to do to the data is sort all the columns (shift cells down) so that the time columns matches in all 3 columns.
    The problem is the workbook can contain more than 3 tables so the user has to input the number of tables to analyse, select the range of each table (i used it to know how many times the cell has to shift) and the cell containing the time. Based on this proceed to the sorting part.

    Let me know if i wasn't clear enough.

  4. #4
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Compare multiple cells at the same time

    Hi,

    I don't understand what you mean by sort so that the time columns match. The numbers in the red columns don't seem the same in the three tables so what do you mean by 'match'

    I presume you're doing something more than a trivial sort of each table but can't see what. Are the tables you present after your macro has run or before.

  5. #5
    Registered User
    Join Date
    04-28-2013
    Location
    Portugal
    MS-Off Ver
    Excel 2013
    Posts
    19

    Re: Compare multiple cells at the same time

    I'm comparing the values based on the standard deviation and the p values for chisquare.

    Those tables are before the macro has run.

    Here's an example of 2 tables after the script have run. This one has no user input action.

    I hope you can get a clearer understanding.

    1308151745.xlsm

  6. #6
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Compare multiple cells at the same time

    What determines whether the cells in any of the tables are moved down?
    It originally looked like the 2nd table values were always less than the same row in the first (base) table but this isn't consistent so presumably that's not the rule.

  7. #7
    Registered User
    Join Date
    04-28-2013
    Location
    Portugal
    MS-Off Ver
    Excel 2013
    Posts
    19

    Re: Compare multiple cells at the same time

    I'll try to exemplify the rule. Considering the first two cells from the first and second table:
    - Calculate the standard deviation and p-value of the first cell of the 1st table and the first cell of the 2nd table (A).
    - Calculate the standard deviation and p-value of the first cell of the 1st table and the second cell of the 2nd table (B).
    - If A > B then the cells are moved down in the first table.

    The opposite is also done to move the cells in the second table.
    - Calculate the standard deviation and p-value of the first cell of the 2nd table and the first cell of the 1st table (A).
    - Calculate the standard deviation and p-value of the first cell of the 2nd table and the second cell of the 1st table (B).
    - If A > B then the cells are moved down in the second table.

    This is the rule for "sorting".

  8. #8
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Compare multiple cells at the same time

    More Qs I'm afraid.

    1. Is each table , i.e. Table 2, Table 3 etc. being compared with Table 1 as a discrete process and independent of the state of any earlier Tables that have been processed.

    2. If the answer to 1 is Yes, is the Table to be compared compared with the original state of Table 1 or Table 1 after it may have had additional blank rows added as a result of earlier table processes?

    3. If the answer to 1 is No, what are the rules for using the previous tables in the processing of the latest table?

    4. When calculating the STDEVp for the four cells in the two comparison tables, are the cells to be compared always the cell position before any previous process has added a blank row to either of the two tables being compared, or with a cell after a blank row has been added. What I mean is this:

    Assuming : where T1=Table 1 & T2 = Table 2
    T1 K10 vs T2 Z10 & T1 K10 vs T2 Z11 are compared.

    If as a result of this comparison T1 is shifted down introducing a new blank Row 11 is the next comparison
    T1 K12 & T2 Z11 vs T1 K12 & T2 Z12 (i.e. 12 being the original row 11) or
    T1 K11 & T2 Z11 vs T1 K11 vs T2 Z12

    Similarly if T2 Cells are shifted down introducing a new blank Row 11 is the next comparison
    T1 K11 & T2 Z1211 vs T1 K11 vs T2 K13 (i.e. 13 being the original row 12) or
    T1 K11 & T2 Z11 vs T1 K11 vs T2 Z12

  9. #9
    Registered User
    Join Date
    04-28-2013
    Location
    Portugal
    MS-Off Ver
    Excel 2013
    Posts
    19

    Re: Compare multiple cells at the same time

    I'm happy to answer to your questions.

    So, regarding your questions:

    1. To have each Table compared to Table1 as an independent process is what i want to achieve.

    2. Those Tables will be compared to the original state of Table1. All the blank rows need to be inserted at the same across the multiple Tables.

    3. I tried to compare each Table one at a time against Table 1 but after the blank rows have been inserted in Table1 that created a problem after the first compare causing Table3 to have completely wrong rows inserted.

    4. The cells position to be compared are always before any blank row is inserted. Using your examples:

    T1 K12 & T2 Z11 vs T1 K12 & T2 Z12 (i.e. 12 being the original row 11) - For T1
    T1 K11 & T2 Z1211 vs T1 K11 vs T2 K13 (i.e. 13 being the original row 12) - For T2

    If i haven't made myself very clear please let me know, it's not very easy trying to describe what i want to accomplish.

  10. #10
    Registered User
    Join Date
    04-28-2013
    Location
    Portugal
    MS-Off Ver
    Excel 2013
    Posts
    19

    Re: Compare multiple cells at the same time

    Sorry to bump this thread, just wondering if anyone can provide some help.

+ 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. Compare multiple cells to see if they match exactly
    By redwine in forum Excel General
    Replies: 12
    Last Post: 08-24-2016, 11:28 PM
  2. [SOLVED] How to compare time values in different cells - type mismatch
    By TC1980 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-05-2013, 10:13 AM
  3. macro to compare time between two cells if time matches output in third cell
    By kshitij_dch in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-31-2012, 07:09 AM
  4. to compare data in multiple cells
    By magendiran.thiru in forum Excel - New Users/Basics
    Replies: 1
    Last Post: 11-21-2010, 04:12 AM
  5. how to compare if multiple cells are equal
    By evanhughes in forum Excel General
    Replies: 13
    Last Post: 02-02-2010, 02:30 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