condensing lines of code

stephicohu

New Member
Joined
Jan 27, 2023
Messages
26
Office Version
  1. 365
Platform
  1. MacOS
This is Stephanie, again, I was informed that I didn't need to use select option in coding as much. I am wondering if I could condense this code:

Sheets("5 month").Activate
Range(Cells(3, 33), Cells(7, 33)).Copy
Sheets("Sheet2").Activate
Range(Cells(3, 2), Cells(7, 2)).Select
ActiveSheet.Paste Link:=True

Can I have combine the sheets("5 month").Range(Cells(3,33), Cells(7,33)).Copy?

Thanks
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
If you don't want to use Activate often then you must be very specific with your references.
VBA Code:
Sub test()
Dim wb As Workbook, sht2 As Worksheet, mon As Worksheet
Dim cpyRng As Range, pstRng As Range

Set wb = ThisWorkbook: Set sht2 = wb.Worksheets("Sheet2"): Set mon = wb.Worksheets("5 month")
Set cpyRng = Range(mon.Cells(33, 3), mon.Cells(33, 7))
Set pstRng = Range(sht2.Cells(3, 2), sht2.Cells(7, 2))

cpyRng.Copy
pstRng.PasteSpecial xlPasteAll

End Sub
 
Upvote 0
Did you intentionaly Paste Link:=True ?
You can't do that without activating the sheet and selecting the destination.

PS: not using Activate & Select is not so much about condensing the code but more about speeding up the code since they make is slower and making the code easier to understand since it is often hard to keep track of what is the Active or selected worksheet or range.
 
Upvote 0
If you did meant to link and wanted to condense the code, this should work:
VBA Code:
Sub NoCopyLinkVersion()

    With Sheets("Sheet2")
        .Range(.Cells(3, 2), .Cells(7, 2)).Formula = "=" & Sheets("5 month").Cells(3, 33).Address(0, 0, xlA1, 1)
    End With

End Sub

The address parameters used:
expression.Address (RowAbsolute, ColumnAbsolute, ReferenceStyle, External)
 
Upvote 0

Forum statistics

Threads
1,214,908
Messages
6,122,187
Members
449,072
Latest member
DW Draft

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