VBA Code help - change certain Rows...

Matty

Well-known Member
Joined
Feb 17, 2007
Messages
3,717
Hi,

Given the following data set:

<table style="font-family:Arial,Arial; font-size:10pt; background-color:#ffffff; padding-left:2pt; padding-right:2pt; " border="1" cellpadding="0" cellspacing="0"><tbody><tr style="background-color:#cacaca; text-align:center; font-weight:bold; font-size:8pt; "><td> </td><td>A</td><td>B</td><td>C</td><td>D</td><td>E</td><td>F</td><td>G</td><td>H</td></tr><tr style="height:17px ;"><td style="font-size:8pt; background-color:#cacaca; text-align:center; ">1</td><td>Data1</td><td>Day1</td><td>Day2</td><td>Day3</td><td>Day4</td><td>Day5</td><td>Day6</td><td>Day7</td></tr><tr style="height:17px ;"><td style="font-size:8pt; background-color:#cacaca; text-align:center; ">2</td><td>ABC</td><td style="text-align:right; ">2</td><td style="text-align:right; ">76</td><td style="text-align:right; ">53</td><td style="text-align:right; ">58</td><td style="text-align:right; ">80</td><td style="text-align:right; ">96</td><td style="text-align:right; ">30</td></tr><tr style="height:17px ;"><td style="font-size:8pt; background-color:#cacaca; text-align:center; ">3</td><td>DEF</td><td style="text-align:right; ">0
</td><td style="text-align:right; ">0
</td><td style="text-align:right; ">0
</td><td style="text-align:right; ">0
</td><td style="text-align:right; ">0
</td><td style="text-align:right; ">0</td><td style="text-align:right; ">0</td></tr><tr style="height:17px ;"><td style="font-size:8pt; background-color:#cacaca; text-align:center; ">4</td><td>GHI</td><td style="text-align:right; ">52</td><td style="text-align:right; ">39</td><td style="text-align:right; ">20</td><td style="text-align:right; ">72</td><td style="text-align:right; ">53</td><td style="text-align:right; ">33</td><td style="text-align:right; ">1</td></tr><tr style="height:17px ;"><td style="font-size:8pt; background-color:#cacaca; text-align:center; ">5</td><td>ABC</td><td style="text-align:right; ">33</td><td style="text-align:right; ">97</td><td style="text-align:right; ">22</td><td style="text-align:right; ">74</td><td style="text-align:right; ">38</td><td style="text-align:right; ">91</td><td style="text-align:right; ">82</td></tr><tr style="height:17px ;"><td style="font-size:8pt; background-color:#cacaca; text-align:center; ">6</td><td>DEF</td><td style="text-align:right; ">12</td><td style="text-align:right; ">13</td><td style="text-align:right; ">16</td><td style="text-align:right; ">20</td><td style="text-align:right; ">33</td><td style="text-align:right; ">59</td><td style="text-align:right; ">42</td></tr><tr style="height:17px ;"><td style="font-size:8pt; background-color:#cacaca; text-align:center; ">7</td><td>GHI</td><td style="text-align:right; ">79</td><td style="text-align:right; ">50</td><td style="text-align:right; ">76</td><td style="text-align:right; ">86</td><td style="text-align:right; ">88</td><td style="text-align:right; ">12</td><td style="text-align:right; ">63</td></tr><tr style="height:17px ;"><td style="font-size:8pt; background-color:#cacaca; text-align:center; ">8</td><td>ABC</td><td style="text-align:right; ">2</td><td style="text-align:right; ">7</td><td style="text-align:right; ">10</td><td style="text-align:right; ">39</td><td style="text-align:right; ">100</td><td style="text-align:right; ">52</td><td style="text-align:right; ">97</td></tr><tr style="height:17px ;"><td style="font-size:8pt; background-color:#cacaca; text-align:center; ">9</td><td>DEF</td><td style="text-align:right; ">75</td><td style="text-align:right; ">98</td><td style="text-align:right; ">11</td><td style="text-align:right; ">9</td><td style="text-align:right; ">95</td><td style="text-align:right; ">21</td><td style="text-align:right; ">68</td></tr><tr style="height:17px ;"><td style="font-size:8pt; background-color:#cacaca; text-align:center; ">10</td><td>GHI</td><td style="text-align:right; ">41</td><td style="text-align:right; ">93</td><td style="text-align:right; ">72</td><td style="text-align:right; ">7</td><td style="text-align:right; ">80</td><td style="text-align:right; ">14</td><td style="text-align:right; ">49</td></tr></tbody></table>
I'm after some code which will convert any Cells in the "DEF" and "GHI" Rows from whatever is in them to 0. To illustrate, I have manually done this in the first "DEF" Row.

My list will grow and shrink, so this needs to be considered too (the Column count will remain constant).

Thanks in advance.

Matty
 
Matty,

Sorry for all the questions - merely trying to broaden my understanding.


Training / Books / Sites

MrExcel's Products: Books, CDs, Podcasts Discuss topics related to Holy Macro! Products: Learn Excel from MrExcel, VBA and Macros for Microsoft Excel,Holy Macro! It's 2500 VBA Examples CD, Guerilla Data Analysis Using Microsoft Excel and Excel Knowledge Base CD and the MrExcel Podcasts.
http://www.mrexcel.com/forum/forumdisplay.php?f=19

How to Learn to Write Macros
http://articles.excelyogi.com/playin...ba/2008/10/27/

How to use the macro recorder
http://articles.excelyogi.com/

Click here and scroll down to Getting Started with VBA.
http://www.datapigtechnologies.com/ExcelMain.htm

If you are serious about learning VBA try
http://www.add-ins.com/vbhelp.htm

Excel Tutorials and Tips - VBA - macros - training
http://www.mrexcel.com/articles.shtml

See David McRitchie's site if you just started with VBA
http://www.mvps.org/dmcritchie/excel/getstarted.htm

What is a Visual Basic Module?
http://www.emagenit.com/VBA Folder/what_is_a_vba_module.htm

Debra Dalgleish has some notes how to implement macros here:
http://www.contextures.com/xlvba01.html

David McRitchie has an intro to macros:
http://www.mvps.org/dmcritchie/excel/getstarted.htm

Ron de Bruin's intro to macros:
http://www.rondebruin.nl/code.htm

Creating custom functions
http://office.microsoft.com/en-us/excel/HA011117011033.aspx

Writing Your First VBA Function in Excel
http://www.exceltip.com/st/Writing_Your_First_VBA_Function_in_Excel/631.html

Where to paste code in VBE VBA
Introducing the Excel VBA Editor
http://www.ask.com/web?qsrc=2417&o=101881&l=dis&q=Where+to+paste+code+in+the+Excel+VBA+Editor

VBA for Excel (Macros)
http://www.excel-vba.com/excel-vba-contents.htm

VBA Lesson 11: VBA Code General Tips and General Vocabulary
http://www.excel-vba.com/vba-code-2-1-tips.htm

Excel VBA -- Adding Code to a Workbook
http://www.contextures.com/xlvba01.html

http://www.excel-vba.com/
http://www.mvps.org/dmcritchie/excel/getstarted.htm
http://www.exceltip.com/excel_links.html

(livelessons video)
Excel VBA and Macros with MrExcel
ISBN: 0-7897-3938-0
http://www.amazon.com/Excel-Macros-M...7936479&sr=1-1

Excel Tutorials / Video Tutorials - Functions
http://www.contextures.com/xlFunctions02.html

http://www.xl-central.com/index.html

http://www.datapigtechnologies.com/ExcelMain.htm

Cascading queries

http://www.tushar-mehta.com/excel/ne...ing_dropdowns/

Excel VLOOKUP Function and VLOOKUP Example
http://www.contextures.com/xlFunctions02.html

INDEX MATCH - Excel Index Function and Excel Match Function
http://www.contextures.com/xlFunctions03.html

http://www.contextures.com/xlDataVal02.html
http://www.contextures.com/xlDataVal05.html
http://www.contextures.com/xlDataVal08.html#Larger

Excel Data Validation - Add New Items
http://www.contextures.com/excel-data-validation-add.html

Programming The VBA Editor - Created by Chip Pearson at Pearson Software Consulting LLC
This page describes how to write code that modifies or reads other VBA code.
http://www.cpearson.com/Excel/vbe.aspx

Locating files containing VBA
Searching Files in Subfolders for VBA code string:
http://www.dailydoseofexcel.com/arch...a-code-string/

http://www.pcreview.co.uk/forums/thread-978054.php

Excel 2003 Power Programming with VBA (Excel Power Programming With Vba)
by John Walkenbach

VBA and Macros for Microsoft Excel, by Bill Jelen "Mr.Excel" and Tracy Syrstad

Excel Hacks 100 Industrial-Strength Tips & Tools, by David & Traina Hawley

VBA and Macros for Microsoft Excel 2007, by Bill Jelen "Mr.Excel" and Tracy Syrstad

Excel 2007 Book: you can try this...there is a try before you buy ebook available at this link…
http://www.mrexcel.com/learnexcel2.shtml

Professional Excel Development
by Stephen/ Bovey, Rob/ Green, John Bullen (Paperback - Feb 11, 2005)

Excel 2002 VBA: Programmers Reference
by Rob Bovey, Stephen Bullen, John Green, and Robert Rosenberg (Paperback - Sep 26, 2001)

VB & VBA in a Nutshell: The Language
(http://www.amazon.co.uk/VB-VBA-Nutsh...4671189&sr=1-2)

Writing Excel Macros with VBA
(http://www.amazon.co.uk/Writing-Exce...4671189&sr=1-3)

User Form Creation
http://www.contextures.com/xlUserForm01.html

DonkeyOte: My Recommended Reading
Volatility
http://www.decisionmodels.com/calcsecretsi.htm

Sumproduct
http://www.xldynamic.com/source/xld.SUMPRODUCT.html
http://www.xldynamic.com/source/xld.SUMPRODUCT.html

Arrays
http://www.xtremevbtalk.com/showthread.php?t=296012

Pivot Intro
http://peltiertech.com/Excel/Pivots/pivotstart.htm

Email from XL - VBA
http://www.rondebruin.nl/sendmail.htm

Outlook VBA
http://www.outlookcode.com/article.aspx?ID=40

Function Dictionary
http://www.xlfdic.com/

Function Translations
http://www.piuha.fi/excel-function-name-translation/

Dynamic Named Ranges
http://www.contextures.com/xlNames01.html

How to create Excel Dashboards
http://www.contextures.com/excel-dashboards.html
http://chandoo.org/wp/excel-dashboards/
http://chandoo.org/wp/management-dashboards-excel/
http://www.exceldashboardwidgets.com/

Excel Dashboard / Scorecard Ebook
http://www.qimacros.com/excel-dashboard-scorecard.html

Mike Alexander from Data Pig Technologies
Excel 2007 Dashboards & Reports For Dummies
 
Upvote 0

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
Thanks for that hiker95. Makes sense now and good to know about the working in memory option.

I now have a lot of reading to do and may be quite some time...

Cheers,

Matty
 
Upvote 0
Hi again, hiker95,

I am coming up with some challenges with this code. The problem is that the none "DEF" or "GHI" Rows contain formulas, but the code overwrites these formulas with their resulting values.

Is there a way to stop this? I suspect it's to do with the working in memory factor where the whole range is "pasted" back in at the end.

Thanks in advance.

Matty
 
Upvote 0
I should also add that the formula Rows are also protected against accidental overwrite.

Thanks,

Matty
 
Upvote 0
Matty,


Sample raw data before the macro:


Excel Workbook
ABCDEFGH
1Data1Day1Day2Day3Day4Day5Day6Day7
2ABC2765358809630
3DEF2710391005297
4
5GHI5239207253331
6ABC33972274389182
7DEF12131620335942
8GHI79507686881263
9ABC2710391005297
10
11DEF7598119952168
12GHI4193727801449
13
Sheet1





After the latest macro:


Excel Workbook
ABCDEFGH
1Data1Day1Day2Day3Day4Day5Day6Day7
2ABC2765358809630
3DEF0000000
4
5GHI0000000
6ABC33972274389182
7DEF0000000
8GHI0000000
9ABC2710391005297
10
11DEF0000000
12GHI0000000
13
Sheet1





Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).


Code:
Option Explicit
Sub ChangeRowsV3()
' hiker95, 08/06/2011
' http://www.mrexcel.com/forum/showthread.php?t=566146
Dim a As Long, aa As Long, LR As Long
Application.ScreenUpdating = False
LR = Cells(Rows.Count, 1).End(xlUp).Row
For a = 2 To LR Step 1
  If Cells(a, 1) = "DEF" Or Cells(a, 1) = "GHI" Then Cells(a, 2).Resize(, 7) = 0
Next a
Application.ScreenUpdating = True
End Sub


Then run the ChangeRowsV3 macro.



If the latest macro does not work correctly, then:

See below in my Signature block: You can upload your workbook to Box Net, and provide us with a link to your workbook.
 
Upvote 0
Perfect. Thanks, hiker95.

With your first version (which is more complex), did you choose that method over this one purely because of the speed factor that comes from working in memory?

Cheers,

Matty
 
Upvote 0
Matty,

did you choose that method over this one purely because of the speed factor that comes from working in memory?

Yes.


You are very welcome.

Glad I could help.

Thanks for the feedback.

Come back anytime.
 
Upvote 0

Thought so. Thanks once again.

The annoying thing with VBA is that I can read it and understand what is going on with it, but I struggle to concoct anything from scratch myself. I shall keep percevering though!

Cheers,

Matty
 
Upvote 0
Thought so. Thanks once again.

The annoying thing with VBA is that I can read it and understand what is going on with it, but I struggle to concoct anything from scratch myself. I shall keep percevering though!

Cheers,

Matty

But you have 2,200 posts here? Haven't you acquired enough knowledge and experience to write code from scratch?
 
Upvote 0
But you have 2,200 posts here? Haven't you acquired enough knowledge and experience to write code from scratch?

Hi wigi,

I'm more of a formula man than a programmer if I'm honest. It's a rare breed who are very good at both!

Matty
 
Upvote 0

Forum statistics

Threads
1,224,598
Messages
6,179,814
Members
452,945
Latest member
Bib195

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