excel - 如何在选择中提取特定单元格的对应值?
问题描述
我从 Excel 工作簿中选择数据并发送到 Outlook。
我需要更新下面的 VBA 代码以提取与我的选择相对应的列(F、G、H)中特定单元格的值,以放入电子邮件主题。
例如:如果选择是F3,G3,H3输出应该是North,RMDN,G277
Sub Send_Selections_To_OutlookEmail()
Dim objSelection As Excel.Range
Dim objTempWorkbook As Excel.Workbook
Dim objTempWorksheet As Excel.Worksheet
Dim strTempHTMLFile As String
Dim objTempHTMLFile As Object
Dim objFileSystem As Object
Dim objTextStream As Object
Dim objOutlookApp As Outlook.Application
Dim objNewEmail As Outlook.MailItem
Dim Strbody As String
'Set the selection
Set objSelection = Nothing
Set objSelection = Selection.SpecialCells(xlCellTypeVisible)
Selection.Copy
'Paste the copied selected ranges into a temp worksheet
Set objTempWorkbook = Excel.Application.Workbooks.Add(1)
Set objTempWorksheet = objTempWorkbook.Sheets(1)
'Keep the values, column widths and formats in pasting
With objTempWorksheet.Cells(1)
.PasteSpecial xlPasteValues
.PasteSpecial xlPasteColumnWidths
.PasteSpecial xlPasteFormats
End With
'Save the temp worksheet as a HTML file
Set objFileSystem = CreateObject("Scripting.FileSystemObject")
strTempHTMLFile = objFileSystem.GetSpecialFolder(2).Path & "\Temp for Excel" & Format(Now, "YYYY-MM-DD hh-mm-ss") & ".htm"
Set objTempHTMLFile = objTempWorkbook.PublishObjects.Add(xlSourceRange, strTempHTMLFile, objTempWorksheet.name, objTempWorksheet.UsedRange.Address)
objTempHTMLFile.Publish (True)
'Create a new email
Set objOutlookApp = CreateObject("Outlook.Application")
Set objNewEmail = objOutlookApp.CreateItem(olMailItem)
'Read the HTML file data and insert into the email body
objNewEmail.Display
Set objTextStream = objFileSystem.OpenTextFile(strTempHTMLFile)
Strbody = "<H5>Eng.</H5>" & "Kindly review the below item to close.<br>"
objNewEmail.HTMLBody = Strbody & "<table align=""left"">" & objTextStream.ReadAll & "<br>" & "<br>" & objNewEmail.HTMLBody
'You can specify the new email recipients, subjects here using the following lines:
'objNewEmail.To = "johnsmith@datanumen.com"
objNewEmail.Subject = " PM need review to close @"
'objNewEmail.Send --> directly send out this email
objTextStream.Close
objTempWorkbook.Close (False)
objFileSystem.DeleteFile (strTempHTMLFile)
End Sub
解决方案
从选择的第一行返回值。
Option Explicit
Sub Append_GHIValues_To_Subject()
Dim objOutlookApp As Outlook.Application
Dim objNewEmail As Outlook.MailItem
Dim rwSel As Range
Dim GValue As String
Dim HValue As String
Dim IValue As String
Set rwSel = Selection
Debug.Print rwSel.Row
With rwSel.EntireRow
GValue = .Cells(7).Value
Debug.Print GValue
HValue = .Cells(8).Value
Debug.Print HValue
IValue = .Cells(9).Value
Debug.Print IValue
End With
'Create a new email
Set objOutlookApp = CreateObject("Outlook.Application")
Set objNewEmail = objOutlookApp.CreateItem(olMailItem)
objNewEmail.Display
objNewEmail.Subject = " PM need review to close @" & " " & GValue & " " & HValue & " " & IValue
End Sub
推荐阅读
- javascript - 如何借助 RecordRTC 在 JavaScript 中创建 WAV 流?
- postgresql - 如何从 postgres 分组集查询中删除空值
- node.js - mongoose find() 文档具有大小 > 0 的属性数组
- google-chrome - Zalenium:禁用 chrome 中的下载弹出窗口
- java - 无论如何,您可以按键排序以使用 atStart 和 atEnd for Firebase?
- elasticsearch - Logstash 过滤器从日志消息中复制一些文本
- reactjs - 作为响应策略在材料 ui 的手风琴和选项卡之间切换的最佳方式?
- mysql - SQL 从表中获取矩阵
- python - 从名称列表中随机生成团队
- python - 使用 python 使用 windows 任务调度程序在网络共享上写入文件