problem with selecting a range

kbishop94

Active Member
Joined
Dec 5, 2016
Messages
458
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
This is on a worksheet. I am using a command button to select (I'm trying to select!) a range on another worksheet and paste it onto the worksheet where the command button is.

THIS works:

Code:
ActiveWorkbook.Worksheets("Seatex Incident Log").Cells(18, 6).Select
Selection.Copy

THESE do NOT:

Code:
ActiveWorkbook.Worksheets("Seatex Incident Log").Cells(18, 18), Cells(707, 18).Select
Selection.Copy
Code:
ActiveWorkbook.Worksheets("Seatex Incident Log").Range(Cells(18, 18), Cells(rCol, 18)).Select
the code works for selecting and copying a single cell, but not for a range.
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
Try
VBA Code:
With Worksheets("Seatex Incident Log")
   .Range(.Cells(18, 18), .Cells(rCol, 18)).Copy
End With
 
Upvote 0
I guess rCol is a column number, then Try this

VBA Code:
Sub test()
  Dim rcol As Long, sh As Worksheet
  rcol = 20
  Set sh = Sheets("Seatex Incident Log")
  sh.Range(sh.Cells(18, 18), sh.Cells(18, rcol)).Copy
End Sub
 
Upvote 0
I guess rCol is a column number, then Try this

VBA Code:
Sub test()
  Dim rcol As Long, sh As Worksheet
  rcol = 20
  Set sh = Sheets("Seatex Incident Log")
  sh.Range(sh.Cells(18, 18), sh.Cells(18, rcol)).Copy
End Sub
My bad. I its actually the number of rows (I've been trying so many different things to try and get this, and I was just really getting frustrated over something this simple.) (its been a really crappy monday so far. )

As always, thanks for the help. (y)
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0
What would be the best way to do the same thing, but for multiple columns instead of just the "R" column?

I'm trying to get only columns: B, E, F, G, K and R all starting at row #18 and down to the last row copied over to the same worksheet that I did with the above code.

I can modify your code to start at column B (2) and go over to R (18), copy all of them over, and then continue with the code where it just deletes the columns in between that I dont need... but unfortunately most of those unwanted columns contain ALOT of data and I'm trying to avoid any unnecessary lagging of the code (especially since I dont need the data thats in those other columns.) Thanks for any suggestions and assistance.
 
Upvote 0
I'm glad to help you. Thanks for the feedback.
 
Upvote 0
On second thought, I just tried it with copying over ALL the data and I cant notice any lag at all. I think it will be fine with just copying it all over and then I'll just use the data that I need. Thanks!
 
Upvote 0
B, E, F, G, K and R all starting at row #18 and down to the last row copied

Try this

VBA Code:
Sub test()
  Dim lr As Long, sh As Worksheet
  Set sh = Sheets("Seatex Incident Log")
  lr = sh.Range("B" & Rows.Count).End(xlUp).Row
  sh.Range("B18:B" & lr & ",E18:G" & lr & ",K18:K" & lr & ",R18:R" & lr).Copy
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,327
Messages
6,124,280
Members
449,149
Latest member
mwdbActuary

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