Solved

Copy the cells from sheet1 to sheet2

Posted on 2014-01-23
5
642 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
ID: 39803745
Think you missed the attachment?
0
 

Author Comment

by:paindoookhan
ID: 39804291
0
 

Author Comment

by:paindoookhan
ID: 39804294
sample file is there
0
 

Author Comment

by:paindoookhan
ID: 39805507
I need this urgently please
0
 
LVL 32

Accepted Solution

by:
Robberbaron (robr) earned 500 total points
ID: 39805526
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

895 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

12 Experts available now in Live!

Get 1:1 Help Now