?
Solved

Copy the cells from sheet1 to sheet2

Posted on 2014-01-23
5
Medium Priority
?
688 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 11

Expert Comment

by:Angelp1ay
ID: 39803745
Think you missed the attachment?
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 2000 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

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…
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
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.

770 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