+ Reply to Thread
Results 1 to 11 of 11

To shift 5 columns to the upper row, if the upper row is empty

  1. #1
    Registered User
    Join Date
    06-12-2014
    Posts
    21

    To shift 5 columns to the upper row, if the upper row is empty

    I am new to VBA and need a code for the following. I am trying to search a column for empty cells. if it is empty then i want the row below it to move up and occupy the empty space but not the entire row to move up. more like 5 colums around the empty cell to move Is it possible?

    I have attached the file as an example. kindly help me.
    Attached Files Attached Files

  2. #2
    Forum Expert
    Join Date
    07-31-2010
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    4,070

    Re: To shift 5 columns to the upper row, if the upper row is empty

    Please Login or Register  to view this content.

  3. #3
    Registered User
    Join Date
    06-12-2014
    Posts
    21

    Re: To shift 5 columns to the upper row, if the upper row is empty

    Hey thx for the post. I'll try it soon and let you know how it worked.

  4. #4
    Registered User
    Join Date
    06-12-2014
    Posts
    21

    Re: To shift 5 columns to the upper row, if the upper row is empty

    Hi. This code works but i think it deletes the empty lines? I don't want it to delete but just move up the values in the respective columns.

    eg: if a row is empty and the rows below it have some data. I just want the data to be shifted up in the 4 columns and not everywhere. This is because I have a lot of formatting going on in my sheet along with other data.. It would be grat if you can help

  5. #5
    Registered User
    Join Date
    06-12-2014
    Posts
    21

    Re: To shift 5 columns to the upper row, if the upper row is empty

    Hi. This code works now for mw after some editing.

    I still have one small problem. Its about the formatting. As I mentioned previously i have a lot of formatting going on. When the code runs i lose the formatting in last few rows. Is it possible to change the last row value to stop before these values?(i.e set last row to row number 52 so that only my table gets altered and not my formatting). I am attached a picture for your understanding. Looking forward to your help eample.jpg
    Last edited by sneha1889; 08-26-2014 at 04:39 AM.

  6. #6
    Forum Expert
    Join Date
    07-31-2010
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    4,070

    Re: To shift 5 columns to the upper row, if the upper row is empty

    Did you try changing the last row to 52?

    Please Login or Register  to view this content.

  7. #7
    Registered User
    Join Date
    06-12-2014
    Posts
    21

    Re: To shift 5 columns to the upper row, if the upper row is empty

    I tried doing it but again affects my formatting. example.jpg In this figure you'll see that line 52 has formatting on the other side and changing last line to 52 cause all that to be unmerged. I need to check the criteria and move if empty only in columns C,D,E,F,G,H from rows 23 to 52.

  8. #8
    Forum Expert
    Join Date
    07-31-2010
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    4,070

    Re: To shift 5 columns to the upper row, if the upper row is empty

    Submit a copy of your workbook. When you start merging cells and having small ranges that need to be affected individually the code start to become tedious. Will see what I can do.

  9. #9
    Registered User
    Join Date
    06-12-2014
    Posts
    21

    Re: To shift 5 columns to the upper row, if the upper row is empty

    Hi stnkynts,

    Thank for your help. I need move the columns only from B23 to H52. Sorry for all XXX. its a company doc so cant leave in it. Once again thx for the help. I am quite new and
    still learning
    Last edited by sneha1889; 08-28-2014 at 04:15 AM.

  10. #10
    Forum Expert
    Join Date
    07-31-2010
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    4,070

    Re: To shift 5 columns to the upper row, if the upper row is empty

    Try this:

    Please Login or Register  to view this content.

  11. #11
    Registered User
    Join Date
    06-12-2014
    Posts
    21

    Re: To shift 5 columns to the upper row, if the upper row is empty

    Dear stnkynts,

    The code works perfectly. But I have a doubt. when i place the worksheet given above in another workbook with 5 other sheets and try and run the code it gives me Run-time error 1004("Application-defined or object defined error"). Why is that?

    Note: I was able to solve this problem by placing this macro in a separate module but I still would like to know what caused the error.
    Last edited by sneha1889; 08-28-2014 at 04:00 AM.

  12. #12
    Forum Expert
    Join Date
    07-31-2010
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    4,070

    Re: To shift 5 columns to the upper row, if the upper row is empty

    Could be any number of things. It is sheet specific so it should work on that specific sheet name within any workbook. What line was highlighted when the error occured? Don't put it under "ThisWorkbook", instead create a module for it.

  13. #13
    Registered User
    Join Date
    06-12-2014
    Posts
    21

    Re: To shift 5 columns to the upper row, if the upper row is empty

    Ya. Intially i put in on the sheet belonging to the code. then when i changed it and added a separate module it worked. Infact I also integrated it with another module i needed and it worked. Just that since i am only starting now my code doesn't seem to be very optimalistic. Takes some time to run. I will share my code here. If possible you to tell me where i can improve?


    Sub Copysheets()

    Dim DestinationBook As Workbook
    Dim LastRow As Integer
    Dim strSaveName As String
    Dim ws As Worksheet
    Dim collValues As Collection
    Dim rCell As Range
    Dim i As Integer, icolumn As Integer

    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    Application.EnableEvents = False
    'get the name to save
    Windows(GeneratorFile).Activate
    strSaveName = Worksheets("Configuration").Range("I56").Value

    'Creating the new workbook
    Set DestinationBook = Workbooks.Add
    'ActiveWorkbook.SaveAs strSaveName
    Application.Dialogs(xlDialogSaveAs).Show (strSaveName)

    'Copying data from cells of Sheet1
    Windows(GeneratorFile).Activate
    Sheets("SMOE-FRONT").Activate
    Cells.Select
    Range("A11").Activate
    Selection.Copy

    'Pasting the data of Sheet1 into the new workbook
    DestinationBook.Sheets(1).Activate
    ActiveSheet.Name = "SMOE-FRONT"
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False
    Selection.PasteSpecial Paste:=xlPasteFormats
    ActiveWindow.DisplayGridlines = False
    Application.CutCopyMode = False
    DestinationBook.Save


    'Copying data from cells of Sheet2
    Windows(GeneratorFile).Activate
    Sheets("SMOE-BACK").Activate
    Cells.Select
    Range("A11").Activate
    Selection.Copy

    'Pasting the data of Sheet2 into the new workbook
    DestinationBook.Worksheets.Add(After:=Worksheets(1)).Name = "Sheet2"
    DestinationBook.Sheets("Sheet2").Activate
    ActiveSheet.Name = "SMOE-BACK"
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False
    Selection.PasteSpecial Paste:=xlPasteFormats
    ActiveWindow.DisplayGridlines = False
    Application.CutCopyMode = False

    'copy
    Windows(GeneratorFile).Activate
    Sheets("SMOE-FRONT").Activate
    ActiveSheet.Shapes.Range(Array("Picture 160")).Select
    ActiveWindow.SmallScroll Down:=30
    ActiveSheet.Shapes.Range(Array("Picture 160", "Rectangle 168")).Select
    ActiveSheet.Shapes.Range(Array("Picture 160", "Rectangle 168", "Text 74")). _
    Select
    ActiveSheet.Shapes.Range(Array("Picture 160", "Rectangle 168", "Text 74", _
    "Rectangle 170")).Select
    ActiveSheet.Shapes.Range(Array("Picture 160", "Rectangle 168", "Text 74", _
    "Rectangle 170", "Text 74")).Select
    ActiveSheet.Shapes.Range(Array("Picture 160", "Rectangle 168", "Text 74", _
    "Rectangle 170", "Text 74", "Rectangle 169")).Select
    ActiveSheet.Shapes.Range(Array("Picture 160", "Rectangle 168", "Text 74", _
    "Rectangle 170", "Text 74", "Rectangle 169", "Text 74")).Select
    ActiveSheet.Shapes.Range(Array("Picture 160", "Rectangle 168", "Text 74", _
    "Rectangle 170", "Text 74", "Rectangle 169", "Text 74", "Text 74")).Select
    ActiveSheet.Shapes.Range(Array("Picture 160", "Rectangle 168", "Text 74", _
    "Rectangle 170", "Text 74", "Rectangle 169", "Text 74", "Text 74", "Text 74") _
    ).Select
    ActiveSheet.Shapes.Range(Array("Picture 160", "Rectangle 168", "Text 74", _
    "Rectangle 170", "Text 74", "Rectangle 169", "Text 74", "Text 74", "Text 74", _
    "Text 124")).Select
    Selection.Copy
    DestinationBook.Sheets("SMOE-FRONT").Activate
    ActiveWindow.SmallScroll Down:=-48
    Range("B2:C4").Select
    ActiveSheet.Paste
    ActiveWindow.SmallScroll Down:=-3
    Selection.ShapeRange.IncrementLeft 7.5
    Selection.ShapeRange.IncrementTop 10.5
    ActiveWindow.SmallScroll Down:=27
    Selection.ShapeRange.IncrementLeft -0.75
    Selection.ShapeRange.IncrementTop 3
    'This block reactivates the string counting in the label text
    Range("T24:T25").Select
    ActiveCell.FormulaR1C1 = "=LEN(RC[-10])"
    Range("T24:T25").Select
    Selection.AutoFill Destination:=Range("T24:T41"), Type:=xlFillDefault
    Range("T24:T41").Select
    Range("T24:T41").Select
    Range("T26").Activate
    ActiveWorkbook.Save

    'To remove the blanks between lines
    Set ws = Sheets("SMOE-FRONT")
    Application.ScreenUpdating = False

    For icolumn = 3 To 8
    If Application.WorksheetFunction.CountA(ws.Range(ws.Cells(23, icolumn), ws.Cells(52, icolumn))) > 1 Then
    Set collValues = New Collection
    For Each rCell In ws.Range(ws.Cells(23, icolumn), ws.Cells(52, icolumn)).SpecialCells(xlCellTypeConstants)
    collValues.Add rCell.Value
    Next rCell
    ws.Range(ws.Cells(23, icolumn), ws.Cells(52, icolumn)).ClearContents
    For i = 1 To collValues.Count
    ws.Cells(53, icolumn).End(xlUp).Offset(1, 0).Value = collValues.Item(i)
    Next i
    Set collValues = Nothing
    End If
    Next icolumn

    Application.ScreenUpdating = True
    'end of code to remove blanks
    ActiveWorkbook.Save
    ActiveWindow.Close



    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
    Application.EnableEvents = True

    Windows(GeneratorFile).Activate
    Sheets("Selector Homepage").Select
    End Sub


    If you have sometime you can tell what I can avoid here and what to improve on. It would be helpful. But till now thanks for all the help. It helped me a lot.
    Last edited by sneha1889; 08-28-2014 at 11:01 AM.

+ 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. [SOLVED] VBA code to select multiple columns with upper bound (j) & lower bound(i)
    By Faridwahidi in forum Excel Programming / VBA / Macros
    Replies: 20
    Last Post: 07-15-2014, 10:56 AM
  2. Replies: 2
    Last Post: 08-20-2010, 03:42 AM
  3. Replies: 14
    Last Post: 08-25-2005, 10:05 PM
  4. Replies: 1
    Last Post: 03-09-2005, 05:06 PM
  5. [SOLVED] How to change location A1 cell from upper right to upper left?
    By Doug@Peacock in forum Excel General
    Replies: 1
    Last Post: 02-08-2005, 11:06 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