Macro to copy portion of row if column 'X' is a certain value

aolsen3

New Member
Joined
Sep 25, 2013
Messages
1
Hi there,
I am looking for some help to tweak my existing code a bit as I'm not very familiar with Macros. Here's my code (which is working great) but I'd like to make a small change, I just don't know where to insert it! I'd like to only copy a portion of the row (A-J) to another worksheet instead of the entire row

Sub SearchForProjectTypeCommercial()
Dim LSearchRow As Integer
Dim LCopyToRow As Integer

On Error GoTo Err_Execute

'Start search in row 8
LSearchRow = 8

'Start copying data to row 8 in Commercial (row counter variable)
LCopyToRow = 8

While Len(Range("A" & CStr(LSearchRow)).Value) > 0

'If value in column F = "Commercial", copy entire row to Commercial
If Range("F" & CStr(LSearchRow)).Value = "Commercial" Then

'Select row in Project Tracker to copy
Rows(CStr(LSearchRow) & ":" & CStr(LSearchRow)).Select
Selection.Copy

'Paste row into Commercial in next row
Sheets("Commercial").Select
Rows(CStr(LCopyToRow) & ":" & CStr(LCopyToRow)).Select
ActiveSheet.Paste

'Move counter to next row
LCopyToRow = LCopyToRow + 1

'Go back to Project Tracker to continue searching
Sheets("Project Tracker").Select

End If

LSearchRow = LSearchRow + 1

Wend

'Position on cell A3
Application.CutCopyMode = False
Range("A3").Select

MsgBox "All matching data has been copied."

Exit Sub

Err_Execute:
MsgBox "An error occurred."

End Sub

Any input would be greatly appreciated :)
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Change this portion of the code:
Code:
'Select row in Project Tracker to copy
Rows(CStr(LSearchRow) & ":" & CStr(LSearchRow)).Select
Selection.Copy
'Paste row into Commercial in next row
Sheets("Commercial").Select
Rows(CStr(LCopyToRow) & ":" & CStr(LCopyToRow)).Select
ActiveSheet.Paste
To this:
Code:
Sheets("Project Tracker").Range("A" & LSearchRow & ":J" & LSearchRow).Copy _
Sheets("Commercial").Range("A" & LCopyRow)
 
Upvote 0

Forum statistics

Threads
1,214,919
Messages
6,122,260
Members
449,075
Latest member
staticfluids

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