Copy Paste VBA

RMXByker

New Member
Joined
Apr 1, 2010
Messages
38
Good Morning Everyone,

I am working on my first VBA project here at work. I need the following.

Copy Data from Worksheet "WORKSHEET"
Range is ("B4:B33")

The data in this range are sum calculations. I only want to copy the value, not the calculation so a paste special seems to be needed.

Now onto where the values will be pasted to.

Worksheet "HRS BY WEEK"

Now comes the more difficult part for me...

I want the macro to look in cell range ("C5:C34"). If there is information there, go to ("D5:D34") and so forth. The first column within that range (5 to 34) that doesn't have information is going to be the paste location.


I thought this was going to be an easy VBA for me to be my first try but for some reason it just is not working for me. Any help is appreciated.

Thanks all,
RMXByker
 
Try this:

Code:
Sub copyvalue()
Worksheets("Sheet1").Range("B4:B33").Copy
Worksheets("Sheet2").Select
'assuming you mean the first empy cell in row 5, starting from cell C5
Range("C5").Select
If IsEmpty(Range("C5")) Then
Else
    If IsEmpty(Range("D5")) Then
        Range("D5").Select
    Else
        Selection.End(xlToRight).Select
        Selection.Offset(0, 1).Select
    End If
End If
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End Sub
 
Upvote 0

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Try this, you can keep running this macro to copy and paste to the first blank column to the right of C5:

Code:
Sub copyvalue()
Worksheets("WORKSHEET").Range("B4:B33").Copy
Worksheets("HRS BY WEEK").Select
'assuming you mean the first empy cell in row 5, starting from cell C5
Range("C5").Select
If IsEmpty(Range("C5")) Then
Else
    If IsEmpty(Range("D5")) Then
        Range("D5").Select
    Else
        Selection.End(xlToRight).Select
        Selection.Offset(0, 1).Select
    End If
End If
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End Sub
 
Upvote 0
That worked great. One last question. I have 2 sub routines. I want the Copy Paste rountine to run followed by a Sort routine. How do I do this by only pressing a button once?

Here is the code I have currently that isn't working....
Code:
Sub COPYPASTE()
Worksheets("WORKSHEET").Range("B2:B31").COPY
Worksheets("HRS BY WEEK").Select
'assuming you mean the first empy cell in row 5, starting from cell C5
Range("C5").Select
If IsEmpty(Range("C5")) Then
Else
    If IsEmpty(Range("D5")) Then
        Range("D5").Select
    Else
        Selection.End(xlToRight).Select
        Selection.Offset(0, 1).Select
    End If
End If
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
End Sub
Sub Sort1stPage()
Sheets("SUMMARY").Unprotect Password:="winston"
Range("C4:D36").Select
Selection.SORT Key1:=Range("C4"), Order1:=xlAscending, Header:=xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal
Sheets("SUMMARY").PROTECT Password:="winston"
End Sub

I want to take the data that is on page "SUMMARY" and sort it in ascending order. My code works great individually but now I want them to run back to back by one button.

Thanks again for your help,
Jon
 
Upvote 0
Assuming your sorting code works, then you can combine your code and mine as:

Code:
Sub COPYPASTE()
Worksheets("WORKSHEET").Range("B2:B31").COPY
Worksheets("HRS BY WEEK").Select
'assuming you mean the first empy cell in row 5, starting from cell C5
Range("C5").Select
If IsEmpty(Range("C5")) Then
Else
    If IsEmpty(Range("D5")) Then
        Range("D5").Select
    Else
        Selection.End(xlToRight).Select
        Selection.Offset(0, 1).Select
    End If
End If
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False

Sheets("SUMMARY").Unprotect Password:="winston"
Range("C4:D36").Select
Selection.SORT Key1:=Range("C4"), Order1:=xlAscending, Header:=xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal
Sheets("SUMMARY").PROTECT Password:="winston"
End Sub

Or if you want to keep the two sections seperate, in the code for the button use:

Code:
Call COPYPASTE
Call Sort1stPage

Between the SUB and END SUB lines
 
Upvote 0
Code:
Sub COPYPASTE()
Dim PastePoint As Range
Worksheets("WORKSHEET").Range("B2:B31").COPY
Set PastePoint = Worksheets("HRS BY WEEK").Range("C5")

If IsEmpty(Range(PastePoint)) Then
Else
   Set PastePoint = PastePoint.End(xlToRight).Offset(0, 1)
End If
PastePoint.PasteSpecial Paste:=xlPasteValues, _
                             Operation:=xlNone, _
                           SkipBlanks:=False, _
                            Transpose:=False

DoEvents

Sheets("SUMMARY").Unprotect Password:="winston"
Range("C4:D36").SORT Key1:=Range("C4"), _
                                Order1:=xlAscending, _
                                Header:=xlGuess, _
                       OrderCustom:=1, _
                           MatchCase:=False, _
                           Orientation:=xlTopToBottom, _
                         DataOption1:=xlSortNormal
Sheets("SUMMARY").PROTECT Password:="winston"
End Sub
 
Last edited:
Upvote 0
I will work on it for awhile. I have some other formatting issues going on right now. It seems like I pretty much got it now though.

Thank you all for your help!
 
Upvote 0
Hi SamTyler,

In your code:

Code:
If IsEmpty(Range(PastePoint)) Then
Else
   Set PastePoint = PastePoint.End(xlToRight).Offset(0, 1)

If C5 is empty, it selects C5 but then .end(xlToRight) will go to the furthest right column if D5 is empty won't it? And then fail as it tries to move across one column? I may be missing something as I can't see how you account for both C5 and D5 being empty cells when using xlToRight, do you mind explaining please as I'm still learning!
 
Upvote 0
Actually, I have never used the construct
Code:
If Something Then
Else
[/code

I always use
[code]
If IsEmpty(Range(PastePoint)) Then
Set Range(PastePoint = Range(PastePoint)
Else
   Set PastePoint = PastePoint.End(xlToRight).Offset(0, 1)
EndIf
But yo're right
Code:
  Set PastePoint = PastePoint.End(xlToRight).Offset(0, 1)[/code
 won't work reliably.

[code]
'No If statement
  Set PastePoint = Range("C3").End(xlToRight).Offset(0, 1)
Should work even without checking if C5 is empty. Or something similar.

Might have to use
Code:
Cells(3, Columns.Count).End(xlToLeft) etc
 
Upvote 0

Forum statistics

Threads
1,216,105
Messages
6,128,859
Members
449,472
Latest member
ebc9

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