Solved

Copy the cells from sheet1 to sheet2

Posted on 2014-01-23
5
625 Views
Last Modified: 2014-01-23
Hi I want to copy  from sheet1 to sheet2

If column z doesn't contain "Default" then
 
Loop from row 2 till end of row in sheet 1

     Copy row2,column1 to row2, column8

     Paste to row2,column1

          Then

            Copy row3, column1 to row 3 column 8

              Paste to row15, column1
 
     If the column in sheet 1 contains "default" goto the next row

I'm attaching the file with 2 sheets and also the code

1st sheet contains the content and the second sheet shows the result it should happen


Sub Macro1()
    
    Dim sNm    As String
    Dim ws     As String
    Dim wsNew  As Worksheet
    Dim WrkSheetCount As Integer
    Dim WorkSheetNameNew(15) As String
    Dim LastColumn As Long
    Dim LastRow As Long
    Dim ws_Count As Integer
    Dim PSA_SubCount As Integer
    Dim headerColumnCount As Integer
    
    Dim TR As Integer
    TR = 2
    
    
    
    'Check how many worksheets are there
    ws_Count = ActiveWorkbook.Worksheets.Count
    WrkSheetCount = CountVisible
    'MsgBox "Number of Sheets are " & WrkSheetCount
    
    'Loops through each worksheet
     'For SheetCount = WrkSheetCount
    ' For SheetCount = ws_Count To 1 Step -1
       ' If Worksheets(SheetCount).Visible = True Then
            MsgBox "Worksheet Name " & ActiveWorkbook.Worksheets(1).Name
            MsgBox "Worksheet Name " & ActiveWorkbook.Worksheets(2).Name
            
            'Sheets(ActiveWorkbook.Worksheets(2).Name).Range("A " & I & ":H" & I).Select
       
            ActiveWorkbook.Worksheets(1).Activate
             
                    With ActiveWorkbook.Worksheets(1).UsedRange
                        LastColumn = .Columns(.Columns.Count).Column
                    End With
                    
                     
                    With ActiveWorkbook.Worksheets(1).UsedRange
                        LastRow = .Rows(.Rows.Count).Row
                    End With
                    
                            MsgBox "LastRow " & LastRow
                            MsgBox "LastColumn is" & LastColumn
                    
                   
                    
                    For I = 2 To LastRow 'Row Count
                      If Cells(I, 26) = "Default" Then
                      
                      Next I
                      Else
                      
                       ActiveSheet.Range("A " & I & ":H" & I).Select
                       Selection.Copy
                       Sheets(2).Select.Range("A" & TR).Select
                       ActiveSheet.Paste
                       TR = TR + 13
                     End If
                    Next I
                   
 End Sub
 
 

'Last Row:
Sub xlCellTypeLastCell_Example_Row()
    Dim LastRow As Long
    With ActiveSheet
        LastRow = .Range("A1").SpecialCells(xlCellTypeLastCell).Row
    End With

End Sub


Function UsedRange_Example_Row() As Integer

    Dim LastRow As Long
    With ActiveSheet.UsedRange
        LastRow = .Rows(.Rows.Count).Row
    End With

End Function

'Last Column:

Sub xlCellTypeLastCell_Example_Column()
    Dim LastColumn As Long
    With ActiveSheet
        LastColumn = .Range("A1").SpecialCells(xlCellTypeLastCell).Column
    End With
    MsgBox LastColumn
End Sub


Function UsedRange_Example_Column() As Integer

    Dim LastColumn As Long
    With ActiveSheet.UsedRange
        LastColumn = .Columns(.Columns.Count).Column
    End With
  
End Function

 
Function WksExists(wksName As String) As Boolean
    On Error Resume Next
    WksExists = CBool(Len(Worksheets(wksName).Name) > 0)
End Function

Open in new window

0
Comment
Question by:paindoookhan
  • 3
5 Comments
 
LVL 11

Expert Comment

by:Angelp1ay
Comment Utility
Think you missed the attachment?
0
 

Author Comment

by:paindoookhan
Comment Utility
0
 

Author Comment

by:paindoookhan
Comment Utility
sample file is there
0
 

Author Comment

by:paindoookhan
Comment Utility
I need this urgently please
0
 
LVL 32

Accepted Solution

by:
Robberbaron (robr) earned 500 total points
Comment Utility
here is my update.

note that in your example, source row 1 should not be copied based upon Z=default.

Sub Test1()
    CopyDataFromSheet Worksheets(1), Worksheets(2)
    
End Sub
Sub CopyDataFromSheet(src As Worksheet, dest As Worksheet)
    'assume Source sheet is well formatted
    Dim rngSrc As Range, rngDest As Range
    
    Set rngSrc = src.Range("A2")
    Set rngDest = dest.Range("a2")
    
    Do While Not IsEmpty(rngSrc)
        'more reliable than UsedRange
        If rngSrc.Offset(0, 25).Value = "Default" Then
            'do nothing
         Else
            'copy the data
            rngSrc.Resize(, 8).Copy rngDest
            
            
            'move to the next destination
            Set rngDest = rngDest.Offset(13, 0)
        End If
        'move to next source
        Set rngSrc = rngSrc.Offset(1, 0)
    Loop

End Sub

Open in new window

Multiple-position-request-Jan201.xlsm
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now