我从Excel电子表格中获取信息并将其放入要发送的电子邮件中。
我有一个表格显示在邮件的末尾。我使用HTML创建表,其中变量由数组填充,以更有效地创建这些变量。
该表的一列应该使用百分比格式化,另一列作为货币格式化。如何在保持数组的同时格式化这些变量?
Sub email()
Dim mailsubject As String 'Variable that carries the subject of the email
Dim mailbody As String 'Variable that carries the body of the email
Dim tabledata(2 To 7, 21 To 28) As String
Dim i As Integer 'i and j are auxiliary variables to create the array
Dim j As Integer
'::::::::::::::Among these are the variables I want to format:::::::::::::::
For i = 2 To 7
For j = 21 To 28
tabledata(i, j) = Cells(i, j).Value
'::::::::::::::tabledata(3 to 7, 27) needs to be formatted as percentage, and tabledata(3 to 7, 28) as percentage with two decimal digits.:::::::::::::::::::
Next
Next
Dim OutlookApp As Outlook.Application
Dim MItem As Outlook.mailitem
Set OutlookApp = New Outlook.Application
mailsubject = "PRODUCT IDEA"
mailbody = "<TABLE BORDER='1' WIDTH='10%' CELLPADDING='1' CELLSPACING='1'><TR><TH COLSPAN='8'><BR><H3></H3>" _
& "</TH></TR><TR><TH>" & tabledata(2, 21) & "</TH><TH>" & tabledata(2, 22) & "</TH><TH>" & tabledata(2, 23) & "</TH><TH>" & tabledata(2, 24) _
& "</TH><TH>" & tabledata(2, 25) & "</TH><TH>" & tabledata(2, 26) & "</TH><TH>" & tabledata(2, 27) & "</TH><TH>" & tabledata(2, 28) & "</TH></TR>" _
& "<TR><TD>" & tabledata(3, 21) & "</TD><TD>" & tabledata(3, 22) & "</TD><TD>" & tabledata(3, 23) & "</TD><TD>" & tabledata(3, 24) & "</TD>" _
& "<TD>" & tabledata(3, 25) & "</TD><TD>" & tabledata(3, 26) & "</TD><TD>" & tabledata(3, 27) & "</TD><TD>" & tabledata(3, 28) & "</TD></TR>" _
& "<TR><TD>" & tabledata(4, 21) & "</TD><TD>" & tabledata(4, 22) & "</TD><TD>" & tabledata(4, 23) & "</TD><TD>" & tabledata(4, 24) & "</TD>" _
& "<TD>" & tabledata(4, 25) & "</TD><TD>" & tabledata(4, 26) & "</TD><TD>" & tabledata(4, 27) & "</TD><TD>" & tabledata(4, 28) & "</TD></TR>" _
& "<TR><TD>" & tabledata(5, 21) & "</TD><TD>" & tabledata(5, 22) & "</TD><TD>" & tabledata(5, 23) & "</TD><TD>" & tabledata(5, 24) & "</TD>" _
& "<TD>" & tabledata(5, 25) & "</TD><TD>" & tabledata(5, 26) & "</TD><TD>" & tabledata(5, 27) & "</TD><TD>" & tabledata(5, 28) & "</TD></TR>" _
& "<TR><TD>" & tabledata(6, 21) & "</TD><TD>" & tabledata(6, 22) & "</TD><TD>" & tabledata(6, 23) & "</TD><TD>" & tabledata(6, 24) & "</TD>" _
& "<TD>" & tabledata(6, 25) & "</TD><TD>" & tabledata(6, 26) & "</TD><TD>" & tabledata(6, 27) & "</TD><TD>" & tabledata(6, 28) & "</TD></TR>" _
& "<TR><TD>" & tabledata(7, 21) & "</TD><TD>" & tabledata(7, 22) & "</TD><TD>" & tabledata(7, 23) & "</TD><TD>" & tabledata(7, 24) & "</TD>" _
& "<TD>" & tabledata(7, 25) & "</TD><TD>" & tabledata(7, 26) & "</TD><TD>" & tabledata(7, 27) & "</TD><TD>" & tabledata(7, 28) & "</TD></TR>" _
& "</TABLE>"
Set MItem = OutlookApp.CreateItem(olmailitem)
With MItem
.Subject = mailsubject
.To = "example@xyz.com"
.HTMLBody = mailbody
.Save
.Display
End With
End Sub
答案 0 :(得分:0)
根据j
列号从单元格值填充tabledata时格式化它们:
For j = 21 to 28
tabledata(2, j) = Cells(2, j).Value
Next
For i = 3 to 7
For j = 21 to 26
tabledata(i, j) = Cells(i, j).Value
Next
tabledata(i, 27) = Format(Cells(i, 27).Value, "0%")
tabledata(i, 28) = Format(Cells(i, 28).Value, "0.00%")
Next