Excel Question - Page 1
Forum section: electronics / general

author: bostonman
author_rank: Super Contributor
timestamp: September 19, 2019, 08:23:49 pm
content: I'm creating a spreadsheet and would like to color every other number of rows (more than one).Unfortunately it gets a bit more complicated. A particular item may have six rows of data associated with it while another item may have seven.Can I check whether B3 contains text, and, if so, color that row plus the next X rows until Excel sees text in the same column (say B8)?Here is an example:<A1> <part number> <data><data><data><data><A5> <part number> <data><data><data>So I'd want the four rows for A1 to stay white, and the three rows for A5 to be a different color. Then the next time something shows in column B, it makes it white.

author: ledtester
author_rank: Super Contributor
timestamp: September 19, 2019, 11:36:46 pm
content: Create a new hidden column which will contain either a 0 or a 1.The formula for that column is:If the part number field is populated, value = 1 - the value in the previous row.Otherwise the value = the same value in the previous row.You'll have to add a new row at the top to specify an initial value for this column (either 0 or 1).This will group together rows which belong together and alternate the value among the groups.Now you can color the rows based on this column.

author: bostonman
author_rank: Super Contributor
timestamp: September 20, 2019, 03:28:55 am
content: How would it automatically color an entire row?Below is an example of the locations data would be in. I'm using the actual Excel locations to represent data in those cells. The actual data is in many more rows, so data would be from A1 to H1. So in this example, I'd need to keep A1 to A4 white but across to H1 to H4, A5 to A6 blue but also across to H5 to H6, next set of data (which may be less or more rows than previous) white, etc...This will be over 100s of rows and across to around column H.<A1> <B1> <C1><B2> <C2><B3> <C3><B4> <C4><A5> <B5> <C5><B6> <C6>

author: ledtester
author_rank: Super Contributor
timestamp: September 20, 2019, 03:39:41 am
content: Once you have the new column set up just follow the instructions in this answer:https://superuser.com/questions/625597/changing-a-rows-color-depending-on-the-value-text-of-a-cellAnother article on conditional formatting:https://www.howtogeek.com/howto/45670/how-to-highlight-a-row-in-excel-using-conditional-formatting/

author: bostonman
author_rank: Super Contributor
timestamp: September 20, 2019, 05:55:26 pm
content: Adding a column to use as 0s and 1s is a great idea.I'm having trouble trying to create a formula that would flip the 0s and 1s though.Currently I have data in B3 and I need from B3 until B8 to remain white. At B9 is a new piece of data, and I need from B9 to B14 to be shaded (which I'll use conditional formatting for), and then at B15 to etc... to be white, back to shaded, etc...So I need a statement that will keep the column at a '1' from a cell that has data, the following empty cells, and then flip to '0' when it sees a cell with another piece of data.

author: ledtester
author_rank: Super Contributor
timestamp: September 20, 2019, 06:27:54 pm
content: Let's say your new column is Z.Z1 = 0Z2 = IF A2 is populated THEN 1-Z1 ELSE Z1Z3 = IF A3 is populated THEN 1-Z2 ELSE Z2Z4 = IF A4 is populated THEN 1-Z3 ELSE Z3...

author: bostonman
author_rank: Super Contributor
timestamp: September 20, 2019, 07:24:03 pm
content: That worked, thanks.Now I have a nice column of 0s and 1s. I applied conditional formatting to the entire row based on whether Z1 = 1. Basically, if Z1 > 0, then format the cell blue.When I try to apply conditional formatting to all other rows, it bases the rule on Z1 only and doesn't increment the rule Z2, Z3, etc....

author: bostonman
author_rank: Super Contributor
timestamp: September 20, 2019, 07:31:40 pm
content: Never mind, I figured it out.Thanks anyway and thanks for the help.

author: coolyota
author_rank: Contributor
timestamp: September 24, 2019, 08:43:23 am
content: Late to the party, hope this helps youAssuming the data set...Push the big button.Alternating rows are highlighted[ Specified attachment is not available ]VBA code:Code:Option Explicit'SettingsConst FIRST_COL As String = "A"Const prev_COL As String = "F"Sub main()'Define colour arrayDim colour(1) As VariantDim colour_flag As Byte'Define the colour in RGB here'255,255,255 = white. Experiment with different values.colour(0) = RGB(255, 255, 255)colour(1) = RGB(111, 222, 255)colour_flag = 1Dim current_col1_val, current_col2_val As StringDim prev_col1_val As StringDim row As LongDim highlight_first_row, highlight_prev_row As Long'Activate sheetSheets("Data").Activate'Start rowrow = 2highlight_first_row = 2highlight_prev_row = 2'Get valuescurrent_col1_val = Range("A" & row).Valuecurrent_col2_val = Range("B" & row).Valueprev_col1_val = current_col1_val'Loop until last rowWhile current_col2_val <> ""'If current data in column A is different from previous row, AND previous data is empty then...If (current_col1_val <> prev_col1_val And prev_col1_val = "") Then'Switch colourcolour_flag = colour_flag Xor 1Call Highlight(highlight_first_row, row - 1, colour(colour_flag))'Make current row the new start row for next set of highlightshighlight_first_row = rowEnd If'Save current data in column Aprev_col1_val = current_col1_val'Move onto next rowrow = row + 1current_col1_val = Range("A" & row).Valuecurrent_col2_val = Range("B" & row).ValueWendcolour_flag = colour_flag Xor 1Call Highlight(highlight_first_row, row - 1, colour(colour_flag))End Sub'Highlight cellsSub Highlight(ByVal first_row As Long, ByVal prev_row As Long, ByVal colour As Variant)Range(FIRST_COL & first_row & ":" & prev_COL & prev_row).Interior.color = colourEnd SubHow to add VBA to Excel:https://www.ablebits.com/office-addins-blog/2013/12/06/add-run-vba-macro-excel/

author: bostonman
author_rank: Super Contributor
timestamp: October 10, 2019, 08:04:14 pm
content: After spending the last few weeks on a project and incorporating the conditional formatting per the suggestions of others, I discovered an issue.I need to create a basic spreadsheet without formulas and remove the 0s and 1s that help the conditional formatting. If I copy/paste it into a blank sheet without the 0s and 1s, it removes the conditional formatting color. I've searched online and found two options:Paste the entire thing into Word, and then paste it back into a spreadsheet (seems to work well)Entire code (which I'm uncertain how to execute) into Excel - but it's all homemade code.Any ideas on how to make conditional formatting static?

author: Ian.M
author_rank: Super Contributor
timestamp: October 10, 2019, 09:02:00 pm
content: Is keeping the conditional formatting but 'flattening' the column of formulae that control it to literal 0 and 1 values acceptable?If so, simply copy the column and paste it back using 'Paste Special', with 'Values' selected.If not, it gets really gnarly. I am not aware of any way to do it cleanly*without a lot of VBA scripting. Others have done the heavy lifting for the scripting:https://www.mrexcel.com/forum/excel-questions/284013-convert-conditional-formatting-into-standard-one.htmlbut a fair bit of debugging is likely to be required to get it to 'flatten' the correct attributes from conditional to normal formatting.*The hacks of paste to Word and copy back or save as HTML and reopen are likely to screw up fonts, layout etc.

author: bostonman
author_rank: Super Contributor
timestamp: October 11, 2019, 12:57:03 pm
content: QuoteHaving a spreadsheet with only "values" is perfectly fine. The final spreadsheet needs to be presented and can't have formulas anyway, however, when I (initially) tried this prior to posting the message, I realized it still carried conditional formatting with it.The link you provided is a site I already read, however, I don't know how to implement the code into Excel. Any assistance will be appreciated.

author: Ian.M
author_rank: Super Contributor
timestamp: October 11, 2019, 01:55:06 pm
content: Implementation depends on your Excel version. As I strenuously avoid using any MS Orifice applications with a ribbon, and use Excel 97 for my own document creation needs, I cant be much help for anything past Excel 2000. Make sure you have the VBA*help file installed and start digging into recording and running a macro. Do something simple like putting an ordinary format on a single cell then see if you can find and open the VBA source of the macro and edit it to do what you need.* VBA: Visual BASIC for Applications - a cut-down version of Microsoft's Visual Basic - an object-oriented dialect of BASIC - used for macro scripting in many Microsoft productivity applications.

author: sokoloff
author_rank: Super Contributor
timestamp: October 11, 2019, 04:08:43 pm
content: Try the attached.When you run it, select the region you want to flatten the values and formatting, then click the button.

author: sokoloff
author_rank: Super Contributor
timestamp: October 11, 2019, 05:05:24 pm
content: I just noticed your username. Chances are good we're just a few miles apart (West Cambridge here).

author: bostonman
author_rank: Super Contributor
timestamp: October 15, 2019, 02:14:21 pm
content: I'm a bit north of you, closer to Tewksbury.I need to use that file on my work PC, so I'm uncertain whether it will be allowed. What is the file?

author: sokoloff
author_rank: Super Contributor
timestamp: October 15, 2019, 03:24:23 pm
content: It's an excel file inside the zip file. (It has to be in a zip file because eevblog doesn't allow excel uploads.)I did it on Excel 16.29.1 on Mac, but it probably works on any of the last 5 years of Excel or thereabouts.If you need it in an earlier format of excel, I might be able to dig that up or can just post the code here for you to copy/paste into that earlier version.

author: bostonman
author_rank: Super Contributor
timestamp: October 15, 2019, 03:40:32 pm
content: QuoteBy any chance, are you looking for any test equipment? I'm selling three logic analyzers and a low voltage precision power supply for cheap. Since you're relatively local to me, thought I'd ask.

author: bostonman
author_rank: Super Contributor
timestamp: October 17, 2019, 07:30:55 pm
content: The file you sent works well.Two flaws:I need to copy that tab into my document because I can't carry over Conditional Formatting into your document. Not a big issue, and easily worked around. Basically copy the tab and reapply Conditional Formatting before running the macro.The main issue: it flattens the spreadsheet with a pre-selected color (kind of a tan color). Any way to change the color? Also, what if I have different colors in different cells for different Conditional Formatting rules? If so, it will change them to the same color.

author: sokoloff
author_rank: Super Contributor
timestamp: October 17, 2019, 07:44:14 pm
content: Quote from: bostonman on October 17, 2019, 07:30:55 pmCopy the Macro code and save it in either the original workbook, or in a personal workbook and run the code right from the original spreadsheet.https://support.office.com/en-us/article/create-and-save-all-your-macros-in-a-single-workbook-66c97ab3-11c2-44db-b021-ae005a9bc790Note that the column A formula will need to be applied into the original workbook (or another solution for the conditional color-coding) in order to get the color coding to work.Quote from: bostonman on October 17, 2019, 07:30:55 pmThat should not be the case. It should be flattening the spreadsheet with whatever color formatting the original cell contents had. I think this is a side-effect of you copying the data from your original sheet into the sheet I sent and if you follow the recommendation above (to run the code in the original workbook), that this issue will be automatically eliminated.If it is still an issue, let me know and I'll be happy to look into it.

author: bostonman
author_rank: Super Contributor
timestamp: October 23, 2019, 06:06:46 pm
content: I've tried various methods and all I get is black and white in a new spreadsheet.Here is an example of what my spreadsheet looks like. The 0s and 1s on the right are what I'm using to make the alternating white/green, and the left is a modified formula from your spreadsheet.

author: sokoloff
author_rank: Super Contributor
timestamp: October 23, 2019, 06:13:20 pm
content: Zip up what you have and post it, or mail me a copy and I'll have a look and almost surely figure it out.I'll PM you my email.