ایجاد سوال
dark_mode
0 دوستدار 0 امتیاز منفی
8 visibility
موضوع: آفیس توسط:

روبی در تلاش است تا راهی آسان برای تعیین تعداد اتم ها در فرمول های مولکولی برخی از ساختارهای شیمیایی بیابد. به عنوان مثال، یک سلول ممکن است حاوی فرمولی مانند C12H10N6F2 باشد. در این مورد تعداد اتم ها 12 + 10 + 6 + 2 = 30 است. روبی حدود 300 مورد از این فرمول ها را برای انجام دارد و فکر می کرد آیا یک فرمول اکسل وجود دارد که بتوان برای این کار استفاده کرد.

اول، خبر بد: هیچ راه آسانی برای انجام این کار وجود ندارد.

آنجا؛ در صورتی که این راه حل نباشد، می توانیم به دنبال راه حل باشیم. مثال فرمول شیمیایی ارائه شده توسط روبی ممکن است برخی را به این فکر بیندازد که شمارش اتم ها فرآیندی ساده برای جایگزینی کاراکترهای الفبایی با چیز دیگری است تا فقط کاراکترهای عددی را بتوان ارزیابی کرد. به عنوان مثال، در اینجا مثال فرمول شیمیایی Rubys است:

C12H10N6F2

اگر کاراکترهای الفبایی را با علائم مثبت جایگزین کنید، این را دریافت خواهید کرد:

+12+10+6+2

اکنون فرمول ساده ای به نظر می رسد، درست است؟ این فریبنده است، زیرا در حالی که در این نمونه کار می کند، ممکن است برای سایر فرمول های شیمیایی Rubys اصلا کارایی نداشته باشد. فرمول شیمیایی زیر را که بسیاری از مردم با آن آشنا هستند در نظر بگیرید:

H2O

انجام همین جانشینی به این صورت است:

+2+

مشکل این است که هرگاه یک عنصر وجود داشته باشد - برای مثال، عنصر اکسیژن، یک تعداد ضمنی 1 وجود دارد. بنابراین، H20 در واقع 3 اتم است.

بنابراین اکنون می توانیم راهی برای حساب کردن 1 ضمنی پیدا کنیم، درست است؟ مطمئن؛ این را می توان انجام داد. این کار را می توان به آسانی و تمیزی با یک ماکرو انجام داد، مانند تابع تعریف شده توسط کاربر زیر:

Function CountAtoms(ChemForm As String)
    Dim sNewNum As String
    Dim sTemp As String
    Dim iNewAtoms As Integer
    Dim iTotalAtoms As Integer
    Dim J As Integer

    sNewNum = ""
    iTotalAtoms = 0

    For J = 2 To Len(ChemForm)
        sTemp = Mid(ChemForm, J, 1)
        If sTemp >= "0" And sTemp 

In order to use this function in your worksheet, you would simply reference the chemical formula:

=CountAtoms(A1)

If the chemical formula is in cell A1, this function returns the count you desire. It will even work with formulas such as the following:

NaCl
SbF6

Note that these rely on two-character element names, of which there are many. It does require, however, that the second character of a two-character element name not be capitalized.

So, will this approach work with all chemical formulas? Not really; it only works with the simple ones weve covered so far. You see, chemical formulas can get quite complex. Consider the following example:

2H2O

When an initial number appears like this, then the formula is to be multiplied by that value. Thus, instead of the normal 3 atoms in H2O, this formula would have 6 atoms.

It gets worse. Consider the following valid chemical formulas:

Ca3(PO4)2
Al2(SO4)3(H2O)18

Note the parentheses followed by a number. In this nomenclature, the value immediately following the closing parenthesis indicate how many of the molecules within the parentheses are in the larger molecule. Thus, in the second example there are 3 molecules of SO4 and 18 molecules of H2O in the overall molecule. This obviously affects the number of atoms in the entire formula. To compound complexity, parentheses can even be nested:

CH3(C3H4(NH2)2)18CH3

Fun, huh?

This can still be addressed with a more complex macro. Rather than reinvent the wheel here, though, if you are working with complex chemical formulas such as these, you might want to consider using the macros provided at this site:

http://www.vbaexpress.com/kb/getarticle.php?kb_id=670

Note that the macros arent implemented as user-defined functions. To use them you simply select the cells with the formulas, run the macro, and then the macros modify information in the columns to the right of the selected chemical formulas. Full instructions are included with the code at the above website.

Youll also need to make sure you enable, in the Visual Basic Editor, regular expressions. You do this by choosing Tools | References and then scrolling through the available references to locate the Microsoft VBScript Regular Expressions 5.5 option. Make sure the check box to the left of the reference is selected, then click OK.

اگر خواستی، با این لینک از ما حمایت کن

پاسخ شما

looks_5نام شما برای نمایش - اختیاری
حریم شخصی : آدرس ایمیل شما محفوظ میماند و برای استفاده های تجاری و تبلیغاتی به کار نمی رود
عدد چهار رقمی در تصویر را وارد کنید

برای جلوگیری از این تایید در آینده, لطفا وارد شده یا ثبت نام کنید.
اگر حساب گوگل دارید به راحتی وارید شوید

0 پاسخ وجود دارد

سوال مشابهی یافت نشد

برای دسترسی راحت به مطالب سایت ، اپلیکیشن سایت را نصب کنید
و لطفا بعد از نصب امتیاز دهید. با تشکر از حمایت شما

23.2k سوال

8.5k پاسخ

608 دیدگاه

9.7k کاربر

124 نفر آنلاین
0 عضو و 124 مهمان در سایت حاضرند
بازدید امروز: 7782
بازدید دیروز: 21973
بازدید کل: 15280647
...