我正在尝试创建一个用于从Outlook发送电子邮件的宏。我对原始数据的编辑很少,因为我已经给出了代码。
由于我刚接触VBA,我不知道如何使用Vlookup功能,而且我也不太精通在excel中使用vlookup :(我试图从主数据中获取数据(工作表名称)-Col A帐号和列B有值。我希望代码在电子邮件输出表中找到Col B的值,如果Col A在那里。
主数据样本
Sub Rename()
With Application
.ScreenUpdating = False
End With
Dim FilesInPath As String, MyPath As String, MyFiles() As String, File As String, Number As String
Dim i As Long
Dim Wbk As Workbook
MyPath = Application.ThisWorkbook.Path + "\"
FilesInPath = Dir(MyPath)
FNum = 0
Do While FilesInPath <> ""
FNum = FNum + 1
ReDim Preserve MyFiles(1 To FNum)
MyFiles(FNum) = FilesInPath
FilesInPath = Dir()
Loop
MsgBox (FNum & " files were found.")
For i = 1 To FNum
File = MyPath & MyFiles(i)
On Error Resume Next
Set Wbk = Workbooks.Open(File)
Workbooks(MyFiles(i)).Activate
If Not ((ActiveWorkbook.Name Like "IEM_######.xl*") Or (ActiveWorkbook.Name Like "Summary.xlsm")) Then
Number = Worksheets("Time allocation").Range("B7")
Name = "IEM_" & Number & ".xlsx"
ActiveWorkbook.SaveAs MyPath & Name
End If
Call Wbk.Close(True)
Next
End Sub
如果帐号在Col A中匹配,我想复制电子邮件输出表第一栏中的值。可以帮忙吗?
Col A Col I
Account Value
803 A
691 B
8010 C
答案 0 :(得分:2)
这是我的代码,我试着不在vba上使用默认的VLOOKUP函数,但是使用该函数后面的逻辑来了解VLOOKUP的工作原理
With Sheets("Master Data")
i1 = 2
Do While Sheets("Email Output").Cells(i1, 1).Value <> vbNullString
i2 = 2
Do While .Cells(i2, 1).Value <> vbNullString
If Sheets("Email Output").Cells(i1, 1).Value = .Cells(i2, 1).Value Then
Sheets("Email Output").Cells(i1, 2).Value = .Cells(i2, 2).Value
Exit Do
End If
i2 = i2 + 1
Loop
i1 = i1 + 1
Loop
End With