New to Excel Macros. Paste row from Master to existing worksheet in same row based upon # value.

Timfalcone

New Member
Joined
Jul 2, 2015
Messages
15
Hello,

First off let me apologize for posting this. I searched the site and could not find a working solution for my problem. I have tried to modify a few with no luck.

What I am trying to do.

I want to copy a row from a Worksheet (Master) to a worksheet (Base) based upon a number value in column. (B) of that row. # value = 1

Row must be copied over to same row on Base worksheet.

I know this is easy.. but can't figure it out ..

Thanks
 
Sub Base1()
Dim sh1 As Worksheet, sh2 As Worksheet, c As Range
Set sh1 = Sheets("Master")
Set sh2 = Sheets("Base")
For Each c In sh1.Range("B2", sh1.Cells(Rows.Count, 2).End(xlUp))
If c.Value = 1 Then
With sh1
sh1.Range(c, .Cells(c.Row, Columns.Count).End(xlToLeft)).Copy sh2.Range("B" & c.Row)
End With
Else
sh2.c.EntireRow.ClearContents
End If
Next
End Sub

Getting an error... that's the code I ran.. c.Entirerow.Clearcontents gets highlighted.

Code:
Sub CopyStuff()
Dim sh1 As Worksheet, sh2 As Worksheet, c As Range
Set sh1 = Sheets("Master")
Set sh2 = Sheets("Base")
For Each c In sh1.Range("B2", sh1.Cells(Rows.Count, 2).End(xlUp))
    If c.Value = 1 Then
        With sh1
            .Range(c, .Cells(c.Row, Columns.Count).End(xlToLeft)).Copy sh2.Range("B" & c.Row)
        End With
    Else
        sh2.c.EntireRow.ClearContents
    End If
Next
End Sub
 
Upvote 0

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
Code:
sh2.c.EntireRow.ClearContents
Should be
Code:
sh2.Rows(c.Row).ClearContents
My senility is showing.
 
Upvote 0
It works...Thanks!!

So quick question. (Rows.Count, 2) What does the 2 stand for... I am just figuring out your code. The rest makes complete sense.
 
Upvote 0
It works...Thanks!!

So quick question. (Rows.Count, 2) What does the 2 stand for... I am just figuring out your code. The rest makes complete sense.

The two is the column number. Rows.Count takes it to the bottom row of the worksheet, the End(xlUp) then takes it upward to the first occupied cell in the specified column, in this case column 2.

Regards, JLG
 
Upvote 0

Forum statistics

Threads
1,216,077
Messages
6,128,679
Members
449,463
Latest member
Jojomen56

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