vba code to correct

JCKTDS123456

New Member
Joined
Mar 14, 2016
Messages
4
Dear sir,

I have a vba code that is working nice but problem is that while importing data, font size, conditional formatting etc.....is also importing along with............so please I want value to be imported to the next worksheet.............and for your king refference.........i am sending you model file...............please correct that vba code



Code:
Option Explicit


Sub copyDatabyCondition()
    'copy data if column C isn't empty
    Dim wsSource As Worksheet
    Dim wsTarget As Worksheet
    
    Dim LastCell, i As Long
    
    'intialization
    Set wsSource = ThisWorkbook.Sheets("Sheet2")
    Set wsTarget = ThisWorkbook.Sheets("Sheet3")
    
    'find the last row in both tables
    LastCell = wsSource.Cells(wsSource.Rows.Count, "A").End(xlUp).Row
    
    With wsSource
        'for each row in the source table
        For i = 4 To LastCell
            'column C isn't empty?
            If .Cells(i, "A") > 0 And .Cells(i, "C") = 0 Then
                wsSource.Range("A" & i & ":C" & i).Copy wsTarget.Cells(wsTarget.Rows.Count, "A").End(xlUp).Offset(1, 0)
                wsSource.Range("F" & i).Copy wsTarget.Cells(wsTarget.Rows.Count, "A").End(xlUp).Offset(, 5)
                wsSource.Range("N" & i).Copy wsTarget.Cells(wsTarget.Rows.Count, "A").End(xlUp).Offset(, 13)
            End If
        Next
    End With
End Sub
 
Last edited by a moderator:

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
This copies everything
Code:
wsSource.Range("A" & i & ":C" & i).Copy wsTarget.Cells(wsTarget.Rows.Count, "A").End(xlUp).Offset(1, 0)

This method pastes values only
Code:
wsSource.Range("A" & i & ":C" & i).Copy
wsTarget.Cells(wsTarget.Rows.Count, "A").End(xlUp).Offset(1, 0).PasteSpecial (xlPasteValues)
 
Last edited:
Upvote 0
Hi,
untested but see if this update to your code does what you want

Code:
Option Explicit
Sub CopyDataByCondition()
    'copy data if column C isn't empty
    Dim wsSource As Worksheet, wsTarget As Worksheet
    Dim LastCell, i As Long
    
    Set wsSource = ThisWorkbook.Sheets("Sheet2")
    Set wsTarget = ThisWorkbook.Sheets("Sheet3")
    
'for each row in the source table
        For i = 4 To wsSource.Cells(wsSource.Rows.Count, "A").End(xlUp).Row
            'column C isn't empty?
            If wsSource.Cells(i, "A") > 0 And wsSource.Cells(i, "C") = 0 Then
                With wsTarget
                LastCell = .Cells(.Rows.Count, "A").End(xlUp).Row + 1
                    With .Cells(LastCell, 1)
                        .Resize(, 3).Value = wsSource.Range("A" & i).Resize(, 3).Value
                        .Offset(, 5).Value = wsSource.Range("F" & i).Text
                        .Offset(, 13).Value = wsSource.Range("N" & i).Text
                    End With
                End With
            End If
        Next
End Sub

Dave


Always make a backup before testing new code.
 
Upvote 0
Here's also another way...

Code:
With wsSource.Range("A" & i & ":C" & i)
    wsTarget.Cells(wsTarget.Rows.Count, "A").End(xlUp).Offset(1, 0).Resize(.Rows.Count, .Columns.Count).Value = .Value
End With
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,909
Messages
6,175,315
Members
452,634
Latest member
cpostell

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top