Select and copy a range with values and not with formulas

germanpbv

New Member
Joined
Jan 25, 2020
Messages
4
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2013
Platform
  1. Windows
  2. Web
Hello everyone!! Hopefully somebody can help me!

I am using VBA to create an email from Excel that includes in the body of the message a table that I am selecting as an specific range.

The problem that I have is:
I have not been able to select just the dynamic range that contains values.​
It automatically select all the range that contains also formulas even if the value is empty.​

You can understand better with the image below. Also I am copying the code I am using.

help with macro.PNG


Code:
VBA Code:
Sub sentemails()

Dim Outlook As Object
Dim newEmail As Object

Set Outlook = CreateObject("Outlook.Application")
Set newEmail = Outlook.CreateItem(0)

With newEmail
    .BodyFormat = 2
    .Display
    .HTMLBody = Sheet5.Range("B3").Text & "<br>" & Sheet5.Range("B4").Text & "<br>" & Sheet5.Range("B5").Text & "<br>" & .HTMLBody
    
    .To = Sheet2.Range("E1").Text
    .CC = Sheet5.Range("B1").Text
    .Subject = Sheet5.Range("B2").Text
    
    
    Dim xInspect As Object
    Dim pageEditor As Object
    Set xInspect = newEmail.GetInspector
    Set pageEditor = xInspect.WordEditor
    
    Sheet2.Range("A4:Y50").Copy
    pageEditor.Application.Selection.Start = Len(.Body)
    pageEditor.Application.Selection.End = pageEditor.Application.Selection.Start
    pageEditor.Application.Selection.Paste
    
    Set pageEditor = Nothing
    Set xInspect = Nothing
    '.Send
End With

Set newEmail = Nothing
Set Outlook = Nothing

End Sub

Everything works well creating the email. The problem is the table I am copying brings me all the rows... not only the ones that have data.

Thank you very much!
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Thank you for your help!!

I copied your line code but I got this error:

vberror.PNG


May be this image can explain better what I need:
Captureemail.PNG


Thanks again!!
 
Upvote 0
I was able to save this problem with other post in this forum:


This is the code to select a range that contains values and not the rows with formulas:

VBA Code:
Sub GetRange()
  Dim lr As Long
 
  lr = Columns("A").Find(What:="*", After:=Range("A1"), LookIn:=xlValues, SearchDirection:=xlPrevious, SearchFormat:=False).Row
  Range("A1:E" & lr).Select
End Sub

Thanks!
 
Upvote 0

Forum statistics

Threads
1,224,812
Messages
6,181,088
Members
453,021
Latest member
Justyna P

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