logo elektroda
logo elektroda
X
logo elektroda

Creating Excel VBA Macro to Compare Columns with Same Headers in Different Order

luka_brazi 4818 6
ADVERTISEMENT
Treść została przetłumaczona polish » english Zobacz oryginalną wersję tematu
  • #1 16481359
    luka_brazi
    Level 7  
    Hi,
    request for help. Is it possible and how to do a macro in Excel VBA so that:
    - the assumption of the whole file is to compare the appropriate columns with each other, but ...
    - I have prepared an Excel list with 5 columns with different names, the statements are prepared by different people and the names of the columns are always the same but in different order .....
    ... and here is my problem: I want to always compare the values of the columns with the same names / headers that are sometimes in a different place in the statement

    The IF function is used below for the "T" column named PRODUCT. Unfortunately, if the set is prepared in such a way that the PRODUCT column will not be the 'T' column, but for example the "L" column, then unfortunately it does not fulfill its function.

    = IF (OR (T10 = T11, T11 = T12), "TRUE")
  • ADVERTISEMENT
  • #2 16481384
    Prot
    Level 38  
    Hello!
    Proposes to take advantage of range names for all columns in all worksheets (assigned to those worksheets, not the workbook :!: ) based on fixed column headings :idea:
  • ADVERTISEMENT
  • #3 16481446
    luka_brazi
    Level 7  
    I tried this too and either I don't know how to use it to the end or it won't work that way
    the ideal solution would be in the function used (as below) in the cell address in the "T" position the letter of the column in which excel header found the name "PRODUCT" - I have no idea how to do it :(
    = IF (OR (T10 = T11, T11 = T12), "TRUE")
  • ADVERTISEMENT
  • #4 16481669
    Prot
    Level 38  
    luka_brazi wrote:
    I don't know how to use it to the end


    Come on! :D

    Use "sheet names" will allow you to manipulate variables from the range marked with the same name in several sheets (as it is in several workbooks, there is probably no problem :D ) as in the screenshot:
    Creating Excel VBA Macro to Compare Columns with Same Headers in Different Ordernazwy.JPG Download (172.22 kB)

    or in the attached file.

    Nazwy.xlsx Download (15.07 kB)Points: 1.5 for user
  • #5 16482895
    luka_brazi
    Level 7  
    ok, something's starting to happen :)
    and to do something like the picture, i.e. arrange the columns so that they are in the expected order - only that the columns from the source table are always in different places, but with the same headings Creating Excel VBA Macro to Compare Columns with Same Headers in Different Order
  • ADVERTISEMENT
  • #6 16485950
    lanzul
    Level 30  
    luka_brazi wrote:
    ... and to do something like the picture, i.e. arrange the columns so that they are in the expected order - only that the columns from the source table are always in different places, but with the same headings ...

    ... manually?, functions?, macros? ...

    manually - provide appropriate headings with an entry " 1_ "for the first column of interest to us," 2_ "for the second, etc., and then - Sorting-> Sorting options -> Sort from left to right -> OK -> Sort by: row_num -> ascending

    functions - for a given table, for example: ' A1: N5 'enter in any range of the sheet, e.g. in' L20 ' functions:
    ' = CHOOSE (COLUMN (A1); "No."; "Company name"; "Contract date"; "Contract number"; "Employee"; "Product") '-> w' L21 ' functions: ' = HLOOKUP (L $ 20, $ A $ 1: $ N $ 5, ROW (A2), 0) '-> and drag it to the appropriate data range - if it is in another sheet, include references to the sheet with the table

    with macros - just like manually, only with the vba code, you can record it with a macro recorder and put it in a loop that will spin the procedure, you can use, for example: search or text comparison function, for example:
    Code: VBScript
    Log in, to see the code
  • #7 16486440
    luka_brazi
    Level 7  
    I meant the macro. Thanks a lot. I have succeeded with the more "complicated and confusing" method in VBA, but it works :) :)

Topic summary

The discussion revolves around creating an Excel VBA macro to compare columns with identical headers that may appear in different orders across various worksheets. The user seeks a solution to dynamically reference columns based on their headers rather than fixed column letters. Suggestions include using named ranges for columns, leveraging sheet names for variable manipulation, and employing functions like CHOOSE and HLOOKUP to rearrange data. The user ultimately finds success with a more complex VBA method, indicating progress in their task.
Summary generated by the language model.
ADVERTISEMENT