I want macro only select that records from Sno 1 to 10 which have code and ONLY Transfer values to anther sheet in excel ?
Below is a VBA macro designed to copy only the values (not formulas) from rows with a valid Code (non-empty in the "Code" column) for Sno 1 to 10 from the source sheet and paste them into the target sheet.
Sub CopyValidRecords()
Dim SourceSheet As Worksheet, TargetSheet As Worksheet
Dim LastRow As Long, TargetRow As Long
Dim i As Long
' Set worksheets
Set SourceSheet = ThisWorkbook.Sheets("Source") ' Replace "Source" with your source sheet name
Set TargetSheet = ThisWorkbook.Sheets("Target") ' Replace "Target" with your target sheet name
' Find the next empty row in the target sheet
TargetRow = TargetSheet.Cells(TargetSheet.Rows.Count, "A").End(xlUp).Row + 1
' Loop through Sno 1 to 10 in the source sheet
For i = 1 To 10
' Check if Code column is not empty (Column "C")
If SourceSheet.Cells(i, "C").Value <> "" Then
' Copy values of the entire row to the target sheet
TargetSheet.Cells(TargetRow, "A").Resize(1, 7).Value = SourceSheet.Cells(i, "A").Resize(1, 7).Value
' Increment the target row
TargetRow = TargetRow + 1
End If
Next i
MsgBox "Records copied successfully!", vbInformation
End Sub
How It Works
Source Sheet:
- The macro loops through rows 1 to 10.
- It checks if the "Code" column (column "C") is not empty.
Target Sheet:
- Copies only values of the rows that have a non-empty "Code" to the next available row in the target sheet.
Dynamic Column Handling:
- The macro assumes data spans from columns A to G (Invoice, Sno, Code, Description, Qty, Unit Price, Amount).
- Adjust
.Resize(1, 7)if more or fewer columns are needed.
No Formulas:
- Only the final values (not formulas) are pasted into the target sheet.
Steps to Implement
- Open your Excel workbook.
- Press Alt + F11 to open the VBA editor.
- Go to Insert > Module and paste the macro code.
- Replace
"Source"and"Target"with your actual sheet names. - Close the editor and press Alt + F8.
- Run the macro
CopyValidRecords.
Example Input
Source Sheet (Name: Source):
Example Output
Target Sheet (Name: Target):
Let me know if you have further questions! 😊
Comments
Post a Comment