需求如下:
- 最后一列字段名称为“IPTV”;
- 判断最后的数据是在哪一行?
- 某两列中筛选含有“IPTV”或“TV”,最后一列对应就为“IPTV”,否则为空;
- 最后生成的结果另存为指定日期名称的Excel文件;
Sub 判断平台是否为IPTV()
'定义变量
Dim Lastrow, i As Long
Dim Startrow, Startcolumn As Long
'确认当前单元格所在行列
Startrow = 3
Startcolumn = 14
'确认当前单元格最后一行有数据的是在哪一行
Lastrow = Cells(1000000, Startcolumn).End(xlUp).Row
'清除原先的结果内容
'Range("x3" & ":x" & Lastrow).ClearContents
'用来摆放宏准备输出的信息
Columns(24).Select
'定义一个文本变量,用来存放待判断的单元格值
Dim str1, str2 As String
'从选中的单元格开始,一直判断到该列的最后一行
For i = Startrow To Lastrow
str1 = Cells(i, Startcolumn)
str2 = Cells(i, Startcolumn + 1)
'如果单元格值包含IPTV
If InStr(1, str1, "IPTV", 1) Then
'则在该单元格同一行的下一列填入“IPTV”
Cells(i, 24) = "IPTV"
ElseIf InStr(1, str1, "TV", 1) Then
Cells(i, 24) = "IPTV"
ElseIf InStr(1, str2, "IPTV", 1) Then
Cells(i, 24) = "IPTV"
ElseIf InStr(1, str2, "TV", 1) Then
Cells(i, 24) = "IPTV"
End If
Next
'另存重命名
Excel.Application.DisplayAlerts = False
'新表名:获取前一天的日期
wbn = Format(Now() - 1, "m.dd") & "投诉明细" & ".xlsx"
ActiveWorkbook.SaveAs Filename:="C:\Users\18703\Desktop\" & wbn
Excel.Application.DisplayAlerts = True
MsgBox "完成"
End Sub