Results 1 to 7 of 7

"Read" the column header "Element" and perform calculation

Threaded View

dwsf "Read" the column header... 09-14-2015, 12:25 AM
Leith Ross Re: "Read" the column header... 09-14-2015, 01:14 AM
dwsf Re: "Read" the column header... 09-14-2015, 01:24 AM
Leith Ross Re: "Read" the column header... 09-14-2015, 01:26 AM
Leith Ross Re: "Read" the column header... 09-14-2015, 09:19 PM
dwsf Re: "Read" the column header... 09-14-2015, 10:21 PM
Leith Ross Re: "Read" the column header... 09-15-2015, 01:41 AM
  1. #1
    Registered User
    Join Date
    11-06-2012
    Location
    Brisbane, Austalia
    MS-Off Ver
    Excel 2010
    Posts
    21

    "Read" the column header "Element" and perform calculation

    Hello VBA Gurus,

    I get outputs from a program that gives me values in mol but I want mg.
    The column headers could be any order and any length and contain red-herrings (things that are not elements).
    I am trying to build a macro that searches the column header, identifies which element (if any) it refers to, then performs the appropriate conversion to all the values in that column (which may be of any length), then moves on to the next column.

    I have a collection of the elements and their atomic masses but I am stuck in the weeds manipulating collections and arrays.

    Attached is example input file.

    I want to multiply each value in the column by the corresponding atomic mass and then by 1000.

    Here is my code (I haven't got very far!):
    
    Sub elementLookup()
    
    Dim rngElementHeader As Range
    Dim rngHeaders As Range
    Dim Element As String
    Set rngHeaders = Range("1:1")
    Set rngElementHeader = rngHeaders.Find(Element)
    Dim x As Long, y As Long
    Elements = Array("H", "He", "Li", "Be", "B", "C", "N", "O", "F", "Ne", "Na", "Mg", "Al", "Si", "P", "S", "Cl", "Ar", "K", "Ca", "Sc", "Ti", "V", "Cr", "Mn", "Fe", "Co", "Ni", "Cu", "Zn", "Ga", "Ge", "As", "Se", "Br", "Kr", "Rb", "Sr", "Y", "Zr", "Nb", "Mo", "Ru", "Rh", "Pd", "Ag", "Cd", "In", "Sn", "Sb", "Te", "I", "Xe", "Cs", "Ba", "La", "Ce", "Pr", "Nd", "Sm", "Eu", "Gd", "Tb", "Dy", "Ho", "Er", "Tm", "Yb", "Lu", "Hf", "Ta", "W", "Re", "Os", "Ir", "Pt", "Au", "Hg", "Tl", "Pb", "Bi")
    
    
    For Each Element In Elements
    
    x = Rows(1).Find(Element, LookIn:=xlValues, lookat:=xlWhole).Column
    currentelementmass = c.Item(Element)
    
    Dim c As Collection
    
    Set c = New Collection
    
    c.Add "1.00794", "H"
    c.Add "4.002602", "He"
    c.Add "6.941", "Li"
    c.Add "9.012182", "Be"
    c.Add "10.811", "B"
    c.Add "12.011", "C"
    c.Add "14.00674", "N"
    c.Add "15.9994", "O"
    c.Add "18.9984032", "F"
    c.Add "20.1797", "Ne"
    c.Add "22.989768", "Na"
    c.Add "24.305", "Mg"
    c.Add "26.981539", "Al"
    c.Add "28.0855", "Si"
    c.Add "30.973762", "P"
    c.Add "32.066", "S"
    c.Add "35.4527", "Cl"
    c.Add "39.948", "Ar"
    c.Add "39.0983", "K"
    c.Add "40.078", "Ca"
    c.Add "44.95591", "Sc"
    c.Add "47.88", "Ti"
    c.Add "50.9415", "V"
    c.Add "51.9961", "Cr"
    c.Add "54.93805", "Mn"
    c.Add "55.847", "Fe"
    c.Add "58.9332", "Co"
    c.Add "58.6934", "Ni"
    c.Add "63.546", "Cu"
    c.Add "65.39", "Zn"
    c.Add "69.723", "Ga"
    c.Add "72.61", "Ge"
    c.Add "74.92159", "As"
    c.Add "78.96", "Se"
    c.Add "79.904", "Br"
    c.Add "83.8", "Kr"
    c.Add "85.4678", "Rb"
    c.Add "87.62", "Sr"
    c.Add "88.90585", "Y"
    c.Add "91.224", "Zr"
    c.Add "92.90638", "Nb"
    c.Add "95.94", "Mo"
    c.Add "101.07", "Ru"
    c.Add "102.9055", "Rh"
    c.Add "106.42", "Pd"
    c.Add "107.8682", "Ag"
    c.Add "112.411", "Cd"
    c.Add "114.818", "In"
    c.Add "118.71", "Sn"
    c.Add "121.757", "Sb"
    c.Add "127.6", "Te"
    c.Add "126.90447", "I"
    c.Add "131.29", "Xe"
    c.Add "132.90543", "Cs"
    c.Add "137.327", "Ba"
    c.Add "138.9055", "La"
    c.Add "140.115", "Ce"
    c.Add "140.90765", "Pr"
    c.Add "144.24", "Nd"
    c.Add "150.36", "Sm"
    c.Add "151.965", "Eu"
    c.Add "157.25", "Gd"
    c.Add "158.92534", "Tb"
    c.Add "162.5", "Dy"
    c.Add "164.93032", "Ho"
    c.Add "167.26", "Er"
    c.Add "168.93421", "Tm"
    c.Add "173.04", "Yb"
    c.Add "174.967", "Lu"
    c.Add "178.49", "Hf"
    c.Add "180.9479", "Ta"
    c.Add "183.84", "W"
    c.Add "186.207", "Re"
    c.Add "190.23", "Os"
    c.Add "192.22", "Ir"
    c.Add "195.08", "Pt"
    c.Add "196.96654", "Au"
    c.Add "200.59", "Hg"
    c.Add "204.3833", "Tl"
    c.Add "207.2", "Pb"
    c.Add "208.98037", "Bi"
    
    
    
    
    End Sub
    Thanks in advance, I have been using this site for a long time and it's a great thing!
    Attached Files Attached Files

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Replace all BLANK cells in column with header title "Balance" to "0"
    By ks100 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-21-2014, 09:25 AM
  2. Replies: 4
    Last Post: 11-17-2013, 12:05 PM
  3. [SOLVED] How to USE """"" cells count """"" change font color
    By austin123456 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 10-09-2013, 06:14 AM
  4. [SOLVED] If there is any text in column "A$" on "sheet1" then move cell to column "A$" on "sheet2"
    By ckgeary in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 06-27-2013, 08:28 PM
  5. Replies: 2
    Last Post: 06-06-2013, 12:45 PM
  6. [SOLVED] Formula needed to display "Pass" or "Fail" if a column contains any values other than "yes
    By andreindy in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 03-26-2013, 05:49 PM
  7. Replies: 1
    Last Post: 07-16-2010, 02:44 AM
  8. Replies: 0
    Last Post: 07-09-2009, 04:07 PM

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