Consulting

Results 1 to 7 of 7

Thread: Replace Cell values (vba)

  1. #1

    Replace Cell values (vba)

    Hi all

    I have a sheet which downloads data onto it. This data is split into logical columns eg 'Price'

    However this column will not always be the same and some rows dont contain a value for price.

    Can anyone help me to create a procedure that identifies all values for price and replaces them with a generic value eg 100 goes to ?100

    thanks in advance

  2. #2
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    Well, you could use a custom format such as:

    [VBA][$€-42D]#,##0[/VBA]

    But you don't tell us how we might identify the price columns......Do they say price in the header?
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  3. #3
    Sorry - thats where the problem is, because this data is downloaded it is on the sheet in a fairly haphazard manner. eg cell b12 may have a value of 'price' and then b13:b17 may have values that i wish to change.

    Then further down in the sheet c52 may have value of 'price' and c52:c87 may need to be changed( please note that the number of rows being changes is variable and my examples are not necessarily accurate)

    The main point is that the code must first identify any row with 'price' in it and look in the immediate rows below it until it hits a blank value. it will then start looking for 'price' again

    sorry if im confusing you!

  4. #4
    [vba] If Worksheet = vbNullString Then Worksheet = ActiveSheet.Name

    Dim cellNum As Integer, endRow As Integer

    With Worksheets(Worksheet)
    On Error Resume Next
    endRow = .Cells.Find("*", .Cells(1), xlFormulas, _
    xlWhole, xlByRows, xlPrevious).Row
    If Err <> 0 Then xlLastRow = 0
    End With

    Dim bookCol As Range, Cell As Object

    'loop through each row
    For Row = 1 To endRow

    Set bookCol = Range(Row)
    Set bookCol = Range(bookCol, Cells(Rows.Count, bookCol.Column).End(xlUp))
    cellNum = 1
    For Each Cell In bookCol.Cells
    If Cell.Value Like "Price" Then 'move down the rows until u hit a blank row

    Cell.Value = "YAY"
    'TODO: check all rows below to see if they contain a value

    End If
    cellNum = cellNum + 1
    Next
    Next Row[/vba]
    Thats what i have at the moment but it doesn't seem to be working correctly. Im also not sure of how to do the inner loop once 'price has been found

  5. #5
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    so the word "price" is physically located at various places in the sheet and you want to find that word and format the cells beneath it until you come to a blank cell?
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  6. #6
    [vba]If Worksheet = vbNullString Then Worksheet = ActiveSheet.Name

    Dim endRow As Integer

    With Worksheets(Worksheet)
    On Error Resume Next
    endRow = .Cells.Find("*", .Cells(1), xlFormulas, _
    xlWhole, xlByRows, xlPrevious).Row
    If Err <> 0 Then xlLastRow = 0
    End With

    Set UsedRng = Worksheets(Worksheet).UsedRange
    LastCol = UsedRng(UsedRng.Cells.Count).Column

    'loop through each row
    For Row = 1 To endRow
    For cellNum = 1 To LastCol
    If Worksheets(Worksheet).Cells(Row, cellNum).Value = "Price" Then 'move down the rows until u hit a blank row
    For i = Row + 1 To endRow
    If Worksheets(Worksheet).Cells(i, cellNum).Value = "" Then
    Exit For
    End If
    Worksheets(Worksheet).Cells(i, cellNum).Value = "NEW VALUE"

    Next i
    Row = i
    Exit For
    End If
    Next cellNum
    Next Row[/vba]

    Right so for anybody else - this seems to work, no doubt there are errors but ill find them when necessary

  7. #7
    Quote Originally Posted by lucas
    so the word "price" is physically located at various places in the sheet and you want to find that word and format the cells beneath it until you come to a blank cell?
    yup exactly

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •