[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

vba help - replace data

Posted on 2014-02-26
3
Medium Priority
?
274 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 2000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
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 will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

656 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