【发布时间】:2016-11-17 17:59:36
【问题描述】:
我实际上是 VBA 的新手,但我正在做一些编码来简化我的办公室工作。我知道这对你们大多数人来说都是一些业余水平的问题,但我尝试用谷歌搜索了很长一段时间,但没有找到满意的答案。
我有一个基于输入参数的 excel 文件,它最终应该引用正确的工作表 -> 复制选定的单元格 -> 生成一封电子邮件,正文粘贴复制的单元格和附件
我可以完成大部分工作,只是我不能在我的代码中将“正确表”作为变量引用。请为我点亮一些灯。谢谢。
这里是大部分代码,其余的无关紧要,太笨拙,无法粘贴所有我猜的。
Sub GenerateEmail()
Dim olApp As Object
Dim olMailItm As Object
Dim iCounter As Integer
Dim Dest As Variant
Dim SDest As String
Dim StrAtt1 As String
Dim rng As Range
Set rng = Nothing
On Error Resume Next
Set rng = Sheets("test").Range("A1:Q500").SpecialCells(xlCellTypeVisible)
On Error GoTo 0
If rng Is Nothing Then
MsgBox "The selection is not a range or the sheet is protected" & _
vbNewLine & "please correct and try again.", vbOKOnly
Exit Sub
End If
With Application
.EnableEvents = False
.ScreenUpdating = False
End With
Set olApp = CreateObject("Outlook.Application")
Set olMailItm = olApp.CreateItem(0)
On Error Resume Next
With olMailItm
SDest = ""
StrAtt1 = ThisWorkbook.Path & "\PDF\" & Sheets("Email_Generator").Range("B16")
.To = Worksheets("Email_Generator").Range("B14")
.CC = "Myself"
.BCC = ""
.Subject = Worksheets("Email_Generator").Range("B18")
.HTMLBody = RangetoHTML(rng)
.attachments.Add StrAtt1
.Display
End With
Set olMailItm = Nothing
Set olApp = Nothing
End Sub
具体来说,我希望此代码“Sheets("test") 作为工作表“Test”中的单元格,它是基于我在 Excel 中输入的参数的变量,以便此代码将引用正确的工作表
Set rng = Sheets("test").Range("A1:Q500").SpecialCells(xlCellTypeVisible)
但是当我将工作表识别为命名工作表时,例如Sheets("Email1"),它完美地工作,只是它不能成为一个变量。
我希望这篇文章不会太长,因为我试图尽可能具体。感谢所有阅读本文并试图提供帮助的人。我真的很感激。
【问题讨论】: