+ Reply to Thread
Results 1 to 14 of 14

Shifting data based on values

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    08-14-2013
    Location
    LA Baby!!
    MS-Off Ver
    Excel 2007
    Posts
    1,598

    Shifting data based on values

    Hello. Whenever there is 0 in B3 AND there is a value greater than 0 in C3, I would like to adjust the following data one column to the left. C3:K3, N3:V3, Y3:AG3, AI3:AR3. All of the other data will not adjust.

    Spreadsheet attached BUT it is just one row. In the real data I have 16,000 rows. Thank you!!
    Attached Files Attached Files

  2. #2
    Forum Expert
    Join Date
    10-09-2012
    Location
    Dallas, Texas
    MS-Off Ver
    MO 2010 & 2013
    Posts
    3,049

    Re: Shifting data based on values

    Are you wanting EACH row to be evaluated individually? or if B3 = and C3 < 0 then shift everything, the ENTIRE dataset to the right?


    This evaluate and will shift each row individually:
    macro help With Code.xlsm

    Code with comments so you can understand what I am doing and why:
    Option Explicit
    
    Sub ProcessMyData()
    
    '===================================================================
    'Declare Variables
    '===================================================================
        Dim DataSheet As String 'Used to define DataSource sheet
        Dim LastRow As Long 'Used to determine last row with data
        Dim StartRow As Long ' Used to detemine first row to start at
        Dim I As Long 'Used to loop through rows
    
    '===================================================================
    'Define Variables
    '===================================================================
        StartRow = 5 ' this is the row where you want to BEGIN your work
        DataSheet = ThisWorkbook.ActiveSheet.Name 'Defines the Data Sheet Name
        LastRow = Worksheets(DataSheet).Cells(Rows.Count, 1).End(xlUp).Row 'This will find the last used row in column A of Data sheet
    
    '===================================================================
    'Setup for speed
    '===================================================================
        Application.ScreenUpdating = False 'Sets ScreenUpdating Off
        Application.Calculation = xlCalculationManual 'Sets calculations to manual so it wont calc during procedute
    
    '===================================================================
    'Process Data
    '===================================================================
        
     For I = StartRow To LastRow 'This loops through each of the rows
     Application.StatusBar = "Processing Row: " & I & " of " & LastRow 'this just has a neat little indicator on the statusbar
        
        If Range("B" & I).Value = 0 And Range("C" & I).Value > 0 Then 'This checks for Bi and Ci like you asked for
            
            Range("B" & I & ":J" & I).Value = Range("C" & I & ":K" & I).Value
                Range("K" & I).ClearContents
            Range("M" & I & ":U" & I).Value = Range("N" & I & ":V" & I).Value
                Range("V" & I).ClearContents
            Range("X" & I & ":AF" & I).Value = Range("Y" & I & ":AG" & I).Value
                Range("AG" & I).ClearContents
            Range("AI" & I & ":AQ" & I).Value = Range("AJ" & I & ":AR" & I).Value
                Range("AR" & I).ClearContents
                
        End If
    Next I
        
        ThisWorkbook.Worksheets(DataSheet).Range("A1").Select 'Just selects a random cell to tidy things up at the end
        
    '===================================================================
    'End Macro Procedures
    '===================================================================
        Application.CutCopyMode = False
        Application.ScreenUpdating = True 'Turns screen updating back on
        Application.Calculation = xlCalculationAutomatic 'Turns calculations back on
        Application.StatusBar = False
        MsgBox "Process Complete" 'Gives the user a message
    
    End Sub
    Last edited by mikeTRON; 09-02-2014 at 08:43 PM.
    Please ensure you mark your thread as Solved once it is. Click here to see how.
    If a post helps, please don't forget to add to our reputation by clicking the star icon in the bottom left-hand corner of a post.

  3. #3
    Forum Contributor
    Join Date
    08-14-2013
    Location
    LA Baby!!
    MS-Off Ver
    Excel 2007
    Posts
    1,598

    Re: Shifting data based on values

    @MikeTron: Yes, each row should be evaluated individually. Thanks everyone, I'll try these out.
    Last edited by ammartino44; 09-03-2014 at 12:55 PM.

  4. #4
    Forum Contributor
    Join Date
    08-14-2013
    Location
    LA Baby!!
    MS-Off Ver
    Excel 2007
    Posts
    1,598

    Re: Shifting data based on values

    @ JBeaucaire. Could you add notes for your code? There were some things I didn't quite understand. Thanks for your help.

  5. #5
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Shifting data based on values

    This does that:
    Option Explicit
    
    Sub ShiftLeft()
    Dim LR As Long, Rw As Long, COL As Long
    
    LR = Range("A" & Rows.Count).End(xlUp).Row
    
    Application.ScreenUpdating = False
    For Rw = 3 To LR
        If Range("B" & Rw).Value = 0 And Range("C" & Rw).Value > 0 Then
            For COL = 3 To 36 Step 11
                With Cells(Rw, COL).Resize(, 9)
                    .Offset(, -1).Value = .Value
                End With
                Cells(Rw, COL + 8).ClearContents
            Next COL
        End If
    Next Rw
    Application.ScreenUpdating = True
    
    End Sub
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  6. #6
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,527

    Re: Shifting data based on values

    Here's my attempt:

    Option Explicit
    Sub Macro1()
    
        Const lngStartRow As Long = 3 'Starting row number for the data. Change to suit.
        
        Dim lngLastRow As Long, _
            lngMyRow As Long
        
        lngLastRow = Range("B:C").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
        
        Application.ScreenUpdating = False
        
        For lngMyRow = lngStartRow To lngLastRow
            If Val(Range("B" & lngMyRow)) = 0 And Val(Range("C" & lngMyRow)) > 0 Then
                Range("B" & lngMyRow & ":J" & lngMyRow).Value = Range("C" & lngMyRow & ":K" & lngMyRow).Value
                Range("K" & lngMyRow).ClearContents
                Range("M" & lngMyRow & ":U" & lngMyRow).Value = Range("N" & lngMyRow & ":V" & lngMyRow).Value
                Range("V" & lngMyRow).ClearContents
                Range("X" & lngMyRow & ":AF" & lngMyRow).Value = Range("Y" & lngMyRow & ":AG" & lngMyRow).Value
                Range("AG" & lngMyRow).ClearContents
                Range("AI" & lngMyRow & ":AQ" & lngMyRow).Value = Range("AJ" & lngMyRow & ":AR" & lngMyRow).Value
                Range("AR" & lngMyRow).ClearContents
            End If
        Next lngMyRow
        
        Application.ScreenUpdating = True
    
    End Sub
    Regards,

    Robert
    ____________________________________________
    Please ensure you mark your thread as Solved once it is. Click here to see how
    If this post helps, please don't forget to say thanks by clicking the star icon in the bottom left-hand corner of my post

  7. #7
    Forum Contributor
    Join Date
    08-14-2013
    Location
    LA Baby!!
    MS-Off Ver
    Excel 2007
    Posts
    1,598

    Re: Shifting data based on values

    @ MikeTron. Your setup is a little different than mine. For example, in columns M:V, there would be 0's in columns M and data in columns N and O. The code works fine though. Thank you. Another question: When I run your data, it eliminates data from K, how could I make this all 0's? The point is to push the data back one year (given the criteria) but then this would make year 10, "0", because there is no data in that year. Does that make sense?

    Also, why do you clear contents in column K?
    What is cutcopymode?

    Thanks for your help!!

  8. #8
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Shifting data based on values

    Correction to my code for putting 0 in year 10.

    Option Explicit
    
    Sub ShiftLeft()
    Dim LR As Long, Rw As Long, COL As Long
    
    LR = Range("A" & Rows.Count).End(xlUp).Row
    
    Application.ScreenUpdating = False
    For Rw = 3 To LR
        If Range("B" & Rw).Value = 0 And Range("C" & Rw).Value > 0 Then
            For COL = 3 To 36 Step 11
                With Cells(Rw, COL).Resize(, 9)
                    .Offset(, -1).Value = .Value
                End With
                Cells(Rw, COL + 8).Value = 0
            Next COL
        End If
    Next Rw
    Application.ScreenUpdating = True
    
    End Sub

  9. #9
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Shifting data based on values

    Option Explicit
    
    Sub ShiftLeft()
    Dim LR As Long, Rw As Long, COL As Long
    
    LR = Range("A" & Rows.Count).End(xlUp).Row      'find the last row of data
    
    Application.ScreenUpdating = False              'speed up macro
    
    For Rw = 3 To LR                                'loop through one row at a time
        If Range("B" & Rw).Value = 0 And Range("C" & Rw).Value > 0 Then
            For COL = 3 To 36 Step 11               'process column groups of 11 starting at C
                With Cells(Rw, COL).Resize(, 9)     'copy 9 cells...
                    .Offset(, -1).Value = .Value    '...to the left one cell
                End With
                Cells(Rw, COL + 8).Value = 0        'set the 10th cell to 0
            Next COL                                'next column group of 11
        End If
    Next Rw
    Application.ScreenUpdating = True               'update the screen, back to normal
    
    End Sub

  10. #10
    Forum Contributor
    Join Date
    08-14-2013
    Location
    LA Baby!!
    MS-Off Ver
    Excel 2007
    Posts
    1,598

    Re: Shifting data based on values

    @jbeaucaire. Beautiful, thanks. Just one more question: When you enter the "with" code, why do you set everything to ".value"?

  11. #11
    Forum Contributor
    Join Date
    08-14-2013
    Location
    LA Baby!!
    MS-Off Ver
    Excel 2007
    Posts
    1,598

    Re: Shifting data based on values

    @trebor76. Thanks for your help. Would you mind writing notes with your code?

    A couple of question: 1) Why do you use a "const", is that necessary? 2) Why do you look for the last row in a different manner than jbeaucaire? Which way is better?

  12. #12
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Shifting data based on values

    Trebor's LR construct is fundamentally better as it looks for the "last used row" in several columns.

    Me, I prefer to select one key most important column, invariably column A, and just look UP that one column.

  13. #13
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Shifting data based on values

    The "." connects the commands to follow to the original WITH parent. Instead of having to identify parents over and over, then issue commands:

    Sheets("Sheet1").Range("A:A").ClearContents
    Sheets("Sheet1").Rows(1).Copy Sheets("Sheet1").Range("A7")
    ...you can set a parent object once... makes editing easier:
    With Sheets("Sheet1")
        .Range("A:A").ClearContents
        .Rows(1).Copy .Range("A7")
    End With

  14. #14
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,527

    Re: Shifting data based on values

    Hi ammartino44,

    Here's my code commented:

    Option Explicit 'Forces variable declaration at run time. Without it, any undeclared variables will be assigned as variants - the most expensive (in terms of memory) type of variable.
    Sub Macro1()
    
        'Declare constant variable.  As these types of variables are usually static they provide a good starting reference
        Const lngStartRow As Long = 3 'Starting row number for the data. Change to suit.
        
        'Declare dynamic variables
        Dim lngLastRow As Long, _
            lngMyRow As Long
        
        'Finds the last row from columns B and C
        lngLastRow = Range("B:C").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
        
        'Turn screen updating off
        Application.ScreenUpdating = False
        
        'Loop through each row in the defined number of rows
        For lngMyRow = lngStartRow To lngLastRow
            'If Col. B of the 'lngMyRow' row equals zero and Col. C is greater than zero, then...
            If Val(Range("B" & lngMyRow)) = 0 And Val(Range("C" & lngMyRow)) > 0 Then
                '...make the values across Col's B to J the same as Col's C to K, and...
                Range("B" & lngMyRow & ":J" & lngMyRow).Value = Range("C" & lngMyRow & ":K" & lngMyRow).Value
                '...clear the contents in Col. K, and...
                Range("K" & lngMyRow).ClearContents
                '...make the values across Col's M to U the same as Col's N to V, and...
                Range("M" & lngMyRow & ":U" & lngMyRow).Value = Range("N" & lngMyRow & ":V" & lngMyRow).Value
                '...clear the contents in Col. V, and...
                Range("V" & lngMyRow).ClearContents
                '...make the values across Col's X to AF the same as Col's Y to AG, and...
                Range("X" & lngMyRow & ":AF" & lngMyRow).Value = Range("Y" & lngMyRow & ":AG" & lngMyRow).Value
                '...clear the contents in Col. AG, and...
                Range("AG" & lngMyRow).ClearContents
                '...make the values across Col's AI to AQ the same as Col's AJ to AR, and...
                Range("AI" & lngMyRow & ":AQ" & lngMyRow).Value = Range("AJ" & lngMyRow & ":AR" & lngMyRow).Value
                '...clear the contents in Col. AR
                Range("AR" & lngMyRow).ClearContents
            End If
        Next lngMyRow
        
        'Turn screen updating back on
        Application.ScreenUpdating = True
    
    End Sub
    HTH

    Robert

+ 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. Replies: 0
    Last Post: 10-25-2011, 04:06 AM
  2. Replies: 3
    Last Post: 01-28-2010, 08:35 AM
  3. Replies: 8
    Last Post: 11-07-2009, 09:48 PM
  4. Shifting x-values in chart
    By Abed Alnaif in forum Excel Charting & Pivots
    Replies: 1
    Last Post: 08-29-2008, 04:40 AM
  5. shifting values
    By ChinaBoat in forum Excel General
    Replies: 2
    Last Post: 06-23-2005, 09:54 PM

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