Solved

vba help - replace data

Posted on 2014-02-26
3
271 Views
Last Modified: 2014-02-27
Hello All:

I am looking for a macro VBA to replace all data cells in sheet 1 with "1" - but should not replace the first row.

thanks
0
Comment
Question by:CalmSoul
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 39

Expert Comment

by:Pratima Pharande
ID: 39891045
try this in macro

  Rows("2:10000").Select
Selection.Value = "1"
Range("A2").Select
0
 
LVL 24

Expert Comment

by:Steve
ID: 39891131
Be careful what you ask for, you may do more than you want...

if you were to do whole rows using Rows as below:
This would put the whole row to "1", not just any Data area you may have.
thats a lot of 1s. It will be slow in Excel 2007 or newer and should NOT be done (imo).
Rows("2:10000").Value = "1"

Open in new window


You can name the data range as myData (select the range then in the name bar type myData and hit enter.
then use the following to put 1 in that range
range("myData").Value = 1

Open in new window


Or you could set up a range to change using union:
Dim newRange As range
Set newRange = Union(range("A20:B30"), range("D20:E30"))
newRange.Value = 1

Open in new window


Note: "1" is not the same as 1  this may not cause issue here, but may cause problems in other situations.
Example.xlsm
0
 
LVL 12

Accepted Solution

by:
Harry Lee earned 500 total points
ID: 39892438
CalmSoul,

You can use this vba.

Sub ReplaceData()
Dim WkRng As Range, Rng As Range, Srh As String, ReplaceBy As String
Set WkRng = Range(Cells(2, 1), Cells(Cells(Rows.Count, 1).End(xlUp).Row, Cells(1, Columns.Count).End(xlToLeft).Column))

'Set your Search Value here
Srh = "1"

'Set your Replace By Value here
ReplaceBy = "I'm being replaced."

For Each Rng In WkRng
    If Rng = Srh Then
        Rng = ReplaceBy
    End If
Next
End Sub

Open in new window


The benefit of using this is it's easier to configure if you do this replace action regularly.
0

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question