Conditionally format data in column

Carpenter

New Member
Joined
Nov 30, 2005
Messages
12
I have a column of data similar to the following:

A12312-01
A12445-02
W34323-01
A34298-01
W54732-01

and so on.

I am querying all of this from a database with MSQuery.

I need to take all instances that start with "W" and truncate the "01" from the end. If the value does not start with "W", the suffix "0x" must remain.


Thanks for helping a newb!
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
I am not sure how do that with Conditional Formatting (or even if it is possible).

Via formula, for a value in cell A1, you can use:

=IF(LEFT(A3,1)="W",LEFT(A3,LEN(A3)-3),A3)

Note: This also removes the "-" too. IF you want to keep it, change "-3" to "-2".
 
Upvote 0
To actually physically change the value in the cell it resides in, you can use a macro, i.e. (this example on the range A1:A100)
Code:
Sub FixValues()

    Dim cell As Range
    For Each cell In Range("A1:A100")
        If Left(cell, 1) = "W" Then cell = Left(cell, Len(cell) - 3)
    Next cell
    
End Sub
 
Upvote 0
Thanks for your response... I can use that method to fix the data for a single cell. Perhaps this is simple, but could you let me know how to apply this formula for a whole column? Since this is running on a query, the amount of data in the spreadsheet is dynamic, and can range from one row to a few thousand.
 
Upvote 0
You can make the code more dynamic with a slight modification.

For example, let's say that the data is always in column A, but you don't know how many rows there will be. Then try this:
Code:
Sub FixValues() 

    Dim cell As Range 
    For Each cell In Range("A1:A" & Range("A65536").End(xlUp).Row) 
        If Left(cell, 1) = "W" Then cell = Left(cell, Len(cell) - 3) 
    Next cell 
    
End Sub
 
Upvote 0
I have the macro in now, but I am unsure where to run it. As of now, I run it through tools->macro. Is there any way to have the macro run when I refresh the data?
 
Upvote 0
That depends on how you are refreshing your data. You may be able to put the code in an event procedure, which will automatically trigger the macro based on some event happening.

Here is a good write up on event procedures:
http://www.cpearson.com/excel/events.htm
 
Upvote 0
Thanks for all the help.

I wrote the query with MSQuery... after I place the correct parameter in a cell, I click "Refresh data" on the external data toolbar.

I will have to tinker with all of these events to find the most intuitive approach.
 
Upvote 0

Forum statistics

Threads
1,214,599
Messages
6,120,447
Members
448,966
Latest member
DannyC96

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