Check if cell contains !

ThomasOES

Board Regular
Joined
Aug 29, 2017
Messages
174
I'm submitting a chemistry report but some of the elements are listed in % and some in ppm. I want to change all elements to %. The elements reported in ppm have an exclamation point in the cell containing the element symbol. I would like vba to copy each column and place the copied data two rows below. In the cases where an element is ppm, Multiply the data by 0.0001 and place data two rows below. I've pasted some sample data below. The Boron(B), Calcium(Ca), and Magnesium(Mg) are all reported by a spectrometer in ppm. I want to change the report to %. I would place the data beginning at cell A100. Top row would end at cell L100. Something like, Range("A100:L100").Select. If any cells have "!" Then cell End(xlDown)*0.0001.

Make This

SampleAlAsB!CCa!CoCrCuMg!*MnMo
A-10.00040.0052.960.36914.90.00750.1550.2442.050.760.0464
A-2/B-10.00080.00462.720.35817.310.00750.1540.243.410.770.0457
B-20.00080.00412.780.35817.510.00750.1540.2422.450.7680.0456
C-10.00090.00432.430.35716.530.00750.1550.2432.180.7560.0458
C-2/D-10.00060.00432.520.35818.460.00770.1550.2432.460.7590.0458
D-20.00060.00512.840.36715.030.00750.1550.2422.470.7730.0464
E-10.00060.00432.750.36115.760.00780.1550.2422.330.7650.0461
E-2/F-10.00060.00482.750.35517.350.00750.1540.2412.220.7550.0456
F-20.00060.00462.770.36615.980.00730.1540.2422.270.7710.0458

<colgroup><col width="64" style="width:48pt" span="12"> </colgroup><tbody>
</tbody>

Look Like This

SampleAlAsB!CCa!CoCrCuMg!*MnMo
A-10.00040.0050.000300.3690.001490.00750.1550.2440.002050.760.0464
A-2/B-10.00080.00460.000270.3580.001730.00750.1540.240.003410.770.0457
B-20.00080.00410.000280.3580.001750.00750.1540.2420.002450.7680.0456
C-10.00090.00430.000240.3570.001650.00750.1550.2430.002180.7560.0458
C-2/D-10.00060.00430.000250.3580.001850.00770.1550.2430.002460.7590.0458
D-20.00060.00510.000280.3670.001500.00750.1550.2420.002470.7730.0464
E-10.00060.00430.000280.3610.001580.00780.1550.2420.002330.7650.0461
E-2/F-10.00060.00480.000280.3550.001740.00750.1540.2410.002220.7550.0456
F-20.00060.00460.000280.3660.001600.00730.1540.2420.002270.7710.0458

<colgroup><col width="64" style="width:48pt" span="12"> </colgroup><tbody>
</tbody>

Sometimes the exclamation isn't the right-most character
Round to 5 digits

Any help is greatly appreciated
Tom
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
You will need to determine how you want to trigger the event (I used the double-click event), but this should work.

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Dim theLastRow As Integer
Dim dataValue As Variant

theCopiedData = Range("A1:L10").Copy
Range("A13").PasteSpecial

For Each dataValue In Range("J14:J22")
Range("J" & dataValue.Row) = dataValue * 0.0001
Next
Range("A1").Select
Application.CutCopyMode = False
End Sub
 
Upvote 0
Try:-
Code:
[COLOR="Navy"]Sub[/COLOR] MG11Apr09
[COLOR="Navy"]Dim[/COLOR] Rng [COLOR="Navy"]As[/COLOR] Range, Dn [COLOR="Navy"]As[/COLOR] Range, Rngac [COLOR="Navy"]As[/COLOR] Range, Ac [COLOR="Navy"]As[/COLOR] Range, Mpy [COLOR="Navy"]As[/COLOR] Range
[COLOR="Navy"]Set[/COLOR] Rng = Range(Range("A100"), Range("A" & Rows.Count).End(xlUp))
Rng.Resize(, 12).Copy Rng.Offset(Rng.Count + 2)
[COLOR="Navy"]Set[/COLOR] Rngac = Rng(1).Offset(Rng.Count + 2).Resize(, 12)
[COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] Ac [COLOR="Navy"]In[/COLOR] Rngac
 [COLOR="Navy"]If[/COLOR] InStr(Ac, "!") > 0 [COLOR="Navy"]Then[/COLOR]
    [COLOR="Navy"]Set[/COLOR] Mpy = Ac.Offset(1).Resize(Rng.Count - 1)
        Mpy.Value = Application.Round(Evaluate(Mpy.Address & "*0.0001"), 5)
 [COLOR="Navy"]End[/COLOR] If
 [COLOR="Navy"]Next[/COLOR] Ac
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0
Thanks Mick
I'm busy doing other analysis, but I need to mention the sample data I posted will vary. Sometimes different elements or different number of elements. Also variable number of samples. Can the code have flexibility to accommodate changing numbers of rows and columns?

Thanks
Tom
 
Upvote 0
Try this:-
This code should take care of varying numbers of columns and rows, and paste the copied version 2 rows after the end of the data starting "A100".
Code:
[COLOR="Navy"]Sub[/COLOR] MG12Apr32
[COLOR="Navy"]Dim[/COLOR] Rng [COLOR="Navy"]As[/COLOR] Range, Dn [COLOR="Navy"]As[/COLOR] Range, Rngac [COLOR="Navy"]As[/COLOR] Range
[COLOR="Navy"]Dim[/COLOR] Lst [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] Ac [COLOR="Navy"]As[/COLOR] Range, Mpy [COLOR="Navy"]As[/COLOR] Range
Lst = Cells("100", Columns.Count).End(xlToLeft).Column
[COLOR="Navy"]Set[/COLOR] Rng = Range(Range("A100"), Range("A" & Rows.Count).End(xlUp))
Rng.Resize(, Lst).Copy Rng.Offset(Rng.Count + 2)
[COLOR="Navy"]Set[/COLOR] Rngac = Rng(1).Offset(Rng.Count + 2).Resize(, 12)
[COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] Ac [COLOR="Navy"]In[/COLOR] Rngac
 [COLOR="Navy"]If[/COLOR] InStr(Ac, "!") > 0 [COLOR="Navy"]Then[/COLOR]
    [COLOR="Navy"]Set[/COLOR] Mpy = Ac.Offset(1).Resize(Rng.Count - 1)
        Mpy.Value = Application.Round(Evaluate(Mpy.Address & "*0.0001"), 5)
 [COLOR="Navy"]End[/COLOR] If
 [COLOR="Navy"]Next[/COLOR] Ac
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0
Thanks Mick
Works perfectly. Your code allows me to finish a huge project. Cant thank you enough.
Tom
 
Upvote 0
Hello Mick
I made an alteration to your code
Code:
[COLOR=#000080]Set[/COLOR] Rngac = Rng(1).Offset(Rng.Count + 2).Resize(, 12)
I believe the Resize property needs to match the number of columns
I changed your code to
Code:
[COLOR=#000080]Set[/COLOR] Rngac = Rng(1).Offset(Rng.Count + 2).Resize(, Lst)
After my edit, I stepped through the code and it would cycle the same number of columns. Before the edit it would cycle 12 times. I hope I'm correct that the edit affects the number of columns.
Thanks
Tom
 
Upvote 0
Yes !!, sorry about that I failed to update "12" with "lst" in all the places.
Hope its working for OK now !!
 
Upvote 0

Forum statistics

Threads
1,214,643
Messages
6,120,707
Members
448,981
Latest member
recon11bucks

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top