• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 275
  • Last Modified:

vba help - replace data

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
CalmSoul
Asked:
CalmSoul
1 Solution
 
Pratima PharandeCommented:
try this in macro

  Rows("2:10000").Select
Selection.Value = "1"
Range("A2").Select
0
 
SteveCommented:
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
 
Harry LeeCommented:
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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now