excel - 如果多个范围内的值为空,则为 Msgbox
问题描述
下面的代码(当 F 列中有数据时)创建一个文件夹,将其超链接并从 Excel 数据创建一个 Word 文档,完成后会出现一个 msgbox - "Licence(s) and/ or folder(s) created, please delete text from column F."
。
当 F 列中没有数据时,不会发生这种情况,并且 msbox 会显示"No licence(s) to create - please enter text in column F for the appropriate licence(s) you wish to create."
。我想得到它,以便它在它为空白时以及 N 列中的相应单元格时出现,但无论我尝试什么,我都无法让它正常工作。
它应该只"Licence(s) and/ or folder(s) created, please delete text from column F."
在 N 和 F 列中有文本时显示。
Sub CreateLicenceFull()
Dim objWord
Dim objDoc
Dim objRange
Dim dirName As String
On Error Resume Next
Dim Foldername As String
r = Cells(Rows.Count, 1).End(xlUp).Row
For i = 5 To r
With Cells(i, 5)
If .Value = "Mobile Plant" And Cells(i, 6) <> "" And Cells(i,14) <>"" Then
ActiveSheet.Hyperlinks.Add Anchor:=Cells(i, 4), Address:="\\Wbcphfil01.wbc.lan\dts\Groups\Operational_Services\opserv_group\Enforcement\NRSWA\Licences\Mobile Plant\Applications 2019-20\" & Cells(i, 4) & " (" & Cells(i, 12) & ")", TextToDisplay:=.Value
dirName = Cells(4, i).Values
MkDir ("\\Wbcphfil01.wbc.lan\dts\Groups\Operational_Services\opserv_group\Enforcement\NRSWA\Licences\Mobile Plant\Applications 2019-20\" & Cells(i, 4) & " (" & Cells(i, 12) & ")")
Call Shell("explorer.exe" & " " & "\\Wbcphfil01.wbc.lan\dts\Groups\Operational_Services\opserv_group\Enforcement\NRSWA\Licences\Mobile Plant\Applications 2019-20\" & Cells(i, 4) & " (" & Cells(i, 12) & ")", vbNormalFocus)
Set objWord = CreateObject("Word.Application")
objWord.Visible = True
Set objDoc = objWord.Documents.Add(Template:="\\Wbcphfil01.wbc.lan\dts\Groups\Operational_Services\opserv_group\Enforcement\NRSWA\Licences\Mobile Plant\Mobile Plant Licence.docx", NewTemplate:=False, DocumentType:=0)
Set objRange = objDoc.Bookmarks("LicenceNo").Range
objRange.InsertAfter Cells(i, 4)
Set objRange = objDoc.Bookmarks("Date").Range
objRange.InsertAfter Cells(i, 29)
Set objRange = objDoc.Bookmarks("Company").Range
objRange.InsertAfter Cells(i, 7)
Set objRange = objDoc.Bookmarks("Address").Range
objRange.InsertAfter Cells(i, 8)
Set objRange = objDoc.Bookmarks("Location").Range
objRange.InsertAfter Cells(i, 13)
Set objRange = objDoc.Bookmarks("Location2").Range
objRange.InsertAfter Cells(i, 12)
Set objRange = objDoc.Bookmarks("From").Range
objRange.InsertAfter Cells(i, 18)
Set objRange = objDoc.Bookmarks("To").Range
objRange.InsertAfter Cells(i, 19)
Set objRange = objDoc.Bookmarks("Date2").Range
objRange.InsertAfter Cells(i, 29)
Set objRange = objDoc.Bookmarks("Name").Range
objRange.InsertAfter Cells(i, 53)
objWord.Visible = True
objDoc.SaveAs ("\\Wbcphfil01.wbc.lan\dts\Groups\Operational_Services\opserv_group\Enforcement\NRSWA\Licences\Mobile Plant\Applications 2019-20\" & Cells(i, 4) & " (" & Cells(i, 12) & ")\" & Cells(i, 4) & " (" & Cells(i, 12) & ")")
MsgBox Cells(i, 4) & " - Licence(s) and/ or folder(s) created, please delete text from column F."
End If
If .Value = "Mobile Plant" And Cells(i, 6) <> "" And Cells(i, 14) = "" Then
MsgBox "No licence(s) to create - please enter the application date in column N for the appropriate licence(s) you wish to create."
End If
End With
next i
If WorksheetFunction.CountA(Range("F5:F1000")) = 0 Then
MsgBox "No licence(s) to create - please enter text in column F for the appropriate licence(s) you wish to create."
End If
End Sub
解决方案
我认为你应该在行之间的循环内移动下面的代码
End With
,Next i
如果你想在每一行都得到一个 msgbox。虽然在每一行都获得一个 Msgbox 会非常混乱。如果您有 1000 行数据,则表示 1000 个 Msgbox 弹出窗口。
If Len(Range("F" & i)) = 0 And Len(Range("N" & i)) = 0 Then
MsgBox "No licence(s) to create - please enter text in column F for the appropriate licence(s) you wish to create."
Else
MsgBox "Licence(s) and/ or folder(s) created, please delete text from column F."
End If
推荐阅读
- tensorflow - Google Colab:为什么 CPU 比 TPU 快?
- c# - 需要使用反射从类方法返回值
- google-cloud-platform - Cloud Pub/Sub 和 Cloud Scheduler 的 ip-ranges 是什么?
- assembly - 我可以在 gdb 中向上显示增加的内存地址吗
- python - 如何将 Matplotlb 显示的轴注释、x 轴:以秒为单位的时间更改为 600 秒或 10 分钟的块?
- react-native - Geolocation.getCurrentPosition() 正在工作,但在本机反应中得到错误的坐标,如何解决这个问题?
- maven - nexus repo 路径更改
- .net - 从 Azure Devops 构建时指定 Nuget 依赖项
- postgresql - 当master在ARM上运行并且复制服务器是x86时复制postgresql数据库
- java - 如何获取从昨天到现在的 created_at 时间的对象列表