+ Reply to Thread
Results 1 to 4 of 4

Shifting cells up with VBA

Hybrid View

  1. #1
    Registered User
    Join Date
    11-19-2014
    Location
    Chennai
    MS-Off Ver
    Office 2010
    Posts
    2

    Question Shifting cells up with VBA

    Hello All,

    Please look at the sample excel worksheet and I want to shift the contents of Column B (Time) up to the same column B next to the empty row above. I guess the attached image can explain my situation clearer. Logs Sample Output.jpg

    Thanks.
    Attached Files Attached Files
    Last edited by syednizamudeen; 04-19-2024 at 01:31 PM. Reason: Issue solved

  2. #2
    Valued Forum Contributor
    Join Date
    02-02-2016
    Location
    Indonesia
    MS-Off Ver
    Office 365
    Posts
    1,016

    Re: Shifting cells up with VBA

    Try this:
    Sub syednizamudeen_1()
    Dim i As Long, j As Long, n As Long
    Dim va, vb
    
    va = Range("B2", Cells(Rows.Count, "B").End(xlUp))
    ReDim vb(1 To UBound(va, 1), 1 To 1)
    For i = 1 To UBound(va, 1)
        If n = 0 And va(i, 1) = "" Then
            n = i
        ElseIf va(i, 1) <> "" Then
            vb(n, 1) = va(i, 1)
            n = 0
        End If
    Next
    Range("B2").Resize(UBound(va, 1), 1) = vb
    End Sub

  3. #3
    Registered User
    Join Date
    11-19-2014
    Location
    Chennai
    MS-Off Ver
    Office 2010
    Posts
    2

    Re: Shifting cells up with VBA

    Quote Originally Posted by Akuini View Post
    Try this:
    Sub syednizamudeen_1()
    Dim i As Long, j As Long, n As Long
    Dim va, vb
    
    va = Range("B2", Cells(Rows.Count, "B").End(xlUp))
    ReDim vb(1 To UBound(va, 1), 1 To 1)
    For i = 1 To UBound(va, 1)
        If n = 0 And va(i, 1) = "" Then
            n = i
        ElseIf va(i, 1) <> "" Then
            vb(n, 1) = va(i, 1)
            n = 0
        End If
    Next
    Range("B2").Resize(UBound(va, 1), 1) = vb
    End Sub
    Thanks. So handy. Can you explain the code with added comments?? Sorry for asking too much.

  4. #4
    Valued Forum Contributor
    Join Date
    02-02-2016
    Location
    Indonesia
    MS-Off Ver
    Office 365
    Posts
    1,016

    Re: Shifting cells up with VBA

    Quote Originally Posted by syednizamudeen View Post
    Thanks. So handy. Can you explain the code with added comments?? Sorry for asking too much.
    Sub syednizamudeen_1()
    Dim i As Long, j As Long, n As Long
    Dim va, vb
    
    va = Range("B2", Cells(Rows.Count, "B").End(xlUp)) 'load data starting at B2 into array va
    ReDim vb(1 To UBound(va, 1), 1 To 1) 'create array vb with the same dimension as va
    For i = 1 To UBound(va, 1)
        If n = 0 And va(i, 1) = "" Then 'in col B: get the first blank cell row after non-blank cell & send it to n
            n = i
        ElseIf va(i, 1) <> "" Then 'in col B: if it's non-blank cell
            vb(n, 1) = va(i, 1) 'sent it's value to vb on row n
            n = 0
        End If
    Next
    Range("B2").Resize(UBound(va, 1), 1) = vb 'put vb values to col B starting at B2
    End Sub

+ 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: 3
    Last Post: 09-21-2015, 08:43 AM
  2. Shifting Cells
    By KristenL in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 12-05-2014, 04:42 PM
  3. Shifting....cells
    By brjohnsmith in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-07-2014, 02:46 PM
  4. Shifting Cells then Optimizing
    By rybuns in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-27-2010, 05:09 PM
  5. Shifting Cells to the Right
    By tomwest in forum Excel General
    Replies: 1
    Last Post: 04-09-2010, 06:48 AM
  6. Problems shifting through cells
    By jondadbin in forum Excel General
    Replies: 2
    Last Post: 10-18-2009, 08:35 PM
  7. shifting cells
    By ExcelNewby in forum Excel General
    Replies: 0
    Last Post: 01-22-2008, 09:52 AM

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