[英]Clear cell if contains data
I am exporting information from emails into a spreadsheet using the following: 我正在使用以下方法将信息从电子邮件导出到电子表格中:
Sub CopyToExcel()
Dim xlApp As Object
Dim xlWB As Object
Dim xlSheet As Object
Dim olItem As Outlook.MailItem
Dim vText As Variant
Dim sText As String
Dim vItem As Variant
Dim i As Long
Dim rCount As Long
Dim bXStarted As Boolean
Const strPath As String = "C:\Users\username\Desktop\Spreadsheet.xlsx" 'the path of the workbook
If Application.ActiveExplorer.Selection.Count = 0 Then
MsgBox "No Items selected!", vbCritical, "Error"
Exit Sub
End If
On Error Resume Next
Set xlApp = GetObject(, "Excel.Application")
If Err <> 0 Then
Application.StatusBar = "Please wait while Excel source is opened ... "
Set xlApp = CreateObject("Excel.Application")
bXStarted = True
End If
On Error GoTo 0
'Open the workbook to input the data
Set xlWB = xlApp.Workbooks.Open(strPath)
Set xlSheet = xlWB.Sheets("Sheet1")
'Process each selected record
rCount = xlSheet.UsedRange.Rows.Count
For Each olItem In Application.ActiveExplorer.Selection
sText = olItem.Body
vText = Split(sText, Chr(13))
'Find the next empty line of the worksheet
rCount = rCount + 1
'Check each line of text in the message body
For i = UBound(vText) To 0 Step -1
If InStr(1, vText(i), "Cell0:") > 0 Then
vItem = Split(vText(i), Chr(58))
xlSheet.Range("B" & rCount) = Trim(vItem(1))
Else
ActiveCell = Null
End If
If InStr(1, vText(i), "Field1:") > 0 Then
vItem = Split(vText(i), Chr(58))
xlSheet.Range("D" & rCount) = Trim(vItem(1))
End If
If InStr(1, vText(i), "Field2:") > 0 Then
vItem = Split(vText(i), Chr(58))
xlSheet.Range("E" & rCount) = Trim(vItem(1))
End If
If InStr(1, vText(i), "Field3:") > 0 Then
vItem = Split(vText(i), Chr(58))
xlSheet.Range("F" & rCount) = Trim(vItem(1))
End If
If InStr(1, vText(i), "Field4:") > 0 Then
vItem = Split(vText(i), Chr(58))
xlSheet.Range("H" & rCount) = Trim(vItem(1))
End If
If InStr(1, vText(i), "Field5:") > 0 Then
vItem = Split(vText(i), Chr(58))
xlSheet.Range("I" & rCount) = Trim(vItem(1))
End If
If InStr(1, vText(i), "Field6:") > 0 Then
vItem = Split(vText(i), Chr(58))
xlSheet.Range("J" & rCount) = Trim(vItem(1))
End If
Next i
xlWB.Save
Next olItem
Set xlApp = Nothing
Set xlWB = Nothing
Set xlSheet = Nothing
Set olItem = Nothing
End Sub
The problem is if this is run on an email that does not contain data for a certain Field, it leaves the previous email's data. 问题是,如果此命令在不包含特定字段数据的电子邮件上运行,则会保留前一封电子邮件的数据。 I would like to have the macro clear the cell if it contains information OR just input null into the cell if nothing is found in the Field.
我想让宏清除包含信息的单元格,或者如果在字段中未找到任何内容,则仅在单元格中输入null。
I know it must be something like ActiveCell.clear
or ActiveCell = Null
but i'm just not sure where to put it in the If statements. 我知道它一定是
ActiveCell.clear
或ActiveCell = Null
但是我不确定在If语句中的位置。
I tried this but it does not work: 我试过了,但是不起作用:
If InStr(1, vText(i), "Activity Number:") > 0 Then
vItem = Split(vText(i), Chr(58))
xlSheet.Range("B" & rCount) = Trim(vItem(1))
ElseIf InStr(1, vText(i), "Activity Number:") = 0 Then
xlSheet.Range("B" & rCount) = Null
End If
you could write a function that returns the range object after clearing it out. 您可以编写一个函数,将其清除后返回范围对象。
Function ClearMyRange(r as Range) as Excel.Range
r.ClearContents
set ClearMyRange = r
End Function
then just call it 然后叫它
ClearMyRange(xlSheet.Range("D" & rCount)) = Trim(vItem(1))
The reason I suggested the function is so you don't have to call it like so 我建议使用此函数的原因是,您不必像这样调用它
ClearMyRange(xlSheet.Range("D" & rCount))
xlSheet.Range("D" & rCount) = Trim(vItem(1))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.