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
Solved

vba help - replace data

Posted on 2014-02-26
3
270 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
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

840 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