+ Reply to Thread
Results 1 to 2 of 2

VBA Code for altering a series of data

Hybrid View

  1. #1
    Registered User
    Join Date
    02-01-2016
    Location
    denver, colorado
    MS-Off Ver
    office 2013
    Posts
    1

    VBA Code for altering a series of data

    I have a workbook with a column of 1's and -1's. I want to go through with a loop and alter that data in two instances. If the column produces (1,-1,1) or (-1,1,-1) I then want it to change that third value to be equal to the second value and move on down the column. I have some VBA to accomplish this but it does nothing when I run it. It might be something simple or I might be way off base, either way any suggestions would be appreciated. Here is what I have.( Two options)

    Sub Alter_data()
        
        Dim cell As Range
        Dim Rng As Range
        Dim x As Integer
        
        Do While (x - 1) <> (x - 2) And (x - 2) = x
        x = x + 1
    
            If (x - 1) <> (x - 2) And (x - 2) = x Then
            x = (x - 1)
            End If
        Loop
        
    End Sub
    OR
    Sub Alter()
    
        Dim i As Integer
        Dim r As Range, cell As Range
        
        Set r = Range("B2:B4248")
        
        For Each cell In r
    
            If (i - 1) <> (i - 2) And (i - 2) = i Then
            i = (i - 1)
            Else: i = i
            
            End If
        Next
    
    End Sub
    Last edited by JBeaucaire; 02-01-2016 at 07:12 PM. Reason: Added missing CODE tags. Please read and follow the Forum Rules, link above in the menu bar. Thanks.

  2. #2
    Valued Forum Contributor bulina2k's Avatar
    Join Date
    11-20-2012
    Location
    Urziceni, Ialomita, Romania
    MS-Off Ver
    2019 and 365
    Posts
    867

    Re: VBA Code for altering a series of data

    Presuming that your data is in column A
    Sub alterdata()
    
    Dim i As Long
    Dim lr As Long
    
    lr = Cells(Rows.Count, 1).End(xlUp).Row
    For i = 3 To lr
        If Range("A" & i) <> Range("A" & i - 1) And Range("A" & i) = Range("A" & i - 2) Then
            Range("A" & i) = Range("A" & i - 1)
        End If
    Next
    End Sub
    .. and don't forget to have fun!
    Bogdan.

    mark SOLVED and Add Reputation if my answer pleases you

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Help Altering Code that Was Created
    By trevor2524 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 09-08-2014, 09:55 AM
  2. [SOLVED] Need Help Altering Code
    By basketball2524 in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 09-04-2014, 01:05 PM
  3. [SOLVED] Altering existing formulas using VBA code
    By TheRobsterUK in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 03-14-2014, 11:35 AM
  4. Altering existing macro code
    By jsmity in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 08-03-2010, 01:05 PM
  5. Altering Range in Code
    By Janc in forum Excel Charting & Pivots
    Replies: 4
    Last Post: 11-02-2009, 07:27 AM
  6. altering URLs with code
    By imimin in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-23-2006, 12:45 AM
  7. [SOLVED] Altering code to accomodate empty space
    By JOUIOUI in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-28-2006, 12:20 PM

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1