VBA to copy a dynamic range to clipboard

Purplemuffin

New Member
Joined
Nov 24, 2023
Messages
8
Office Version
  1. 365
Platform
  1. Windows
Hi.

I'm trying and failing to get my head around VBA so thought I'd ask here.

I'm trying to write some VBA to attach to a button so that the user can copy a specific range of cells to clipboard in order to paste elsewhere.

This is the basic of my code:

Sub Copy_Test()
Range("A36:F39").Copy
End Sub

However - where it says F39, this could be anywhere between F38 and F50, where the row is taken to be the last non blank row (it would also have TOTAL in column E if that means the code is easier to write?)

Any ideas anybody please?
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Try this
VBA Code:
Sub CopyRange()
Dim wb As Workbook, sht As Worksheet, rng As Range, lRow As Long, lCol As Long
Set wb = ThisWorkbook: Set sht = wb.ActiveSheet
lRow = sht.Rows(sht.Rows.Count).End(xlUp).Row
lCol = sht.Columns(sht.Columns.Count).End(xlToLeft).Column
Set rng = sht.Range("A36", Cells(lRow, lCol))
rng.Copy
End Sub
 
Upvote 0
That did something good, but still copies everything up until row 50 as there is shading in those cells (it is a table that gets pasted into another program entirely, with formatting to make the dividers bold.

Is there a way to get it to ignore formatting but to look at what is actually written within cells?
 
Upvote 0
The code in #3 copied everything from A36 to R50 - not sure what went wrong :(

Thanks for trying though
 
Upvote 0
When you say "another program", do you mean another Workbook, or a totally different application?
 
Upvote 0
Sorry If I wasn't clear - I need to copy from cell A36 to cell F & (whatever row is the last one that isn't blank). There will also be text in the last non-blank row in column E saying "TOTAL" if that makes it easier
 
Upvote 0

Forum statistics

Threads
1,224,592
Messages
6,179,787
Members
452,942
Latest member
VijayNewtoExcel

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