Solved

Copy the cells from sheet1 to sheet2

Posted on 2014-01-23
5
675 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: 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

738 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