Consulting

Results 1 to 5 of 5

Thread: simple copy-past

  1. #1
    VBAX Regular
    Joined
    Dec 2015
    Posts
    89
    Location

    simple copy-past

    Hi All

    I need a simple excel macro to do the below:

    - check for a cell on column B and if has text ignore it
    - Then hide the entire row (which has text on cell of column B)
    - moving down to the next cell on column B and check it's empty go left on column A and copy the content of A cell then back to B column and past it
    - Don't hide this row (that we just copied text inside)
    - move to the next cell on column B

    Thanks in advance

  2. #2
    VBAX Expert Leith Ross's Avatar
    Joined
    Oct 2012
    Location
    San Francisco, California
    Posts
    552
    Location
    Hello baset,

    Test this macro with your data.

    Sub CopyPaste()
    
    
        Dim Cell    As Range
        Dim RngBeg  As Range
        Dim RngEnd  As Range
        Dim Wks     As Worksheet
        
            ' // Change the worksheet name in double quotes to match your worksheet's name.
            Set Wks = Worksheets("Sheet1")
            
            Set RngBeg = Wks.Range("B1")
            
            ' // Find the last row with data in it.
            Set RngEnd = Wks.Cells(Rows.Count, RngBeg.Column).End(xlUp)
            
                For Each Cell In Wks.Range(RngBeg, RngEnd)
                    If Cell <> Empty Then
                        Cell.EntireRow.Hidden = True
                    Else
                        Cell.Value = Cell.Offset(0, -1).Value
                    End If
                Next Cell
                
    End Sub
    Sincerely,
    Leith Ross

    "1N73LL1G3NC3 15 7H3 4B1L17Y 70 4D4P7 70 CH4NG3 - 573PH3N H4WK1NG"

  3. #3
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    The last Row may have "B" purposely empty, so I would use
    ' // Find the last row with data in it.
    Dim LR As long
    LR = Application.WorksheetFunction.Max(Wks.Cells(Rows.Count, "B").End(xlUp).Row, _
                 Wks.Cells(Rows.Count, "A").End(xlUp).Row
    Set RngEnd = Wks.Cells(LR, RngBeg.Column)
    If it is possible that both "A" and "B" are empty in the actual last Row
    Option Explicit
    
    Function RealLastRow(WsName As String) As Long
    Dim LastFormula As Range
    Dim LastValue As Range
    
    With Worksheets(WsName)
       On Error Resume Next
       Set LastFormula = .Cells.Find(What:="*", After:=Range("A1"), LookIn:=xlFormulas, _
                         LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlPrevious)
       Set LastValue = .Cells.Find(What:="*", After:=Range("A1"), LookIn:=xlValues, _
                       LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlPrevious)
       On Error GoTo 0
    End With
       
       If LastFormula Is Nothing And LastValue Is Nothing Then
          RealLastRow = 1
          Exit Function
       End If
       
       RealLastRow = Application.WorksheetFunction.Max(LastFormula.Row, LastValue.Row)
    End Function
    Sub Test_RealLastRow()
    Dim Ws As Worksheet
    
    For Each Ws In Worksheets
       MsgBox RealLastRow(Ws.Name)
    Next
    End Sub
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  4. #4
    VBAX Regular
    Joined
    Dec 2015
    Posts
    89
    Location
    Lot of thanks Mr. Leith Ross your Macro works fine except it's ignore only the last row.

    Thanks a lot sir.

  5. #5
    VBAX Regular
    Joined
    Dec 2015
    Posts
    89
    Location
    Thanks a lot Mr. SamT for your additions to enhance the macro.

    Thanks a lot sir.

Posting Permissions

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