Solved

vba help - replace data

Posted on 2014-02-26
3
266 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
3 Comments
 
LVL 39

Expert Comment

by:Pratima Pharande
Comment Utility
try this in macro

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

Expert Comment

by:Steve
Comment Utility
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
Comment Utility
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Script is being strange 8 48
Excel formula 6 21
Excel - Increse columns in range by 5 6 20
onOpen 14 38
Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now