我又回到了我的VBA代码之一挣扎!我创建了以下代码来执行验证 - 如果在单元格A1中未找到任何值,则找到另一个打开的Excel WB,复制日期并进一步继续处理。这是行得通的,但是如果发现值简单地启动了这个过程。我觉得我没有在正确的地方放置一个“Else”,任何建议都会很有帮助! 我在说的ELSE是在“找我”之下。逻辑如果其他人不在Excel中工作VBA
Sub Cvent003_Uploads()
Sheets("Add File Here").Select
If IsEmpty(Range("A1")) Then
Worksheets("Master Mapper").Activate
Dim answer003 As Integer
answer003 = MsgBox("Please check the Data Sheet. No value found in first row! Do you wish to find Cvent003 file in open workbooks and start process?", vbYesNo + vbQuestion, "Review & Proceed")
If answer003 = vbYes Then
'Starts here
Dim wSheet As Worksheet
Dim wBook As Workbook
Dim rFound As Range
Dim bFound As Boolean
Dim lngLastRow2 As Long
On Error Resume Next
For Each wBook In Application.Workbooks
For Each wSheet In wBook.Worksheets
Set rFound = Nothing
Set rFound = wSheet.Range("D1:D2").Find(What:="Meeting Manager", SearchFormat:=True, After:=wSheet.Range("D1"), _
LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, MatchCase:=True)
'rFound.Cells.Select
If Not rFound Is Nothing Then
bFound = True
Application.Goto rFound, True
'Rows(1, 2).EntireRow.Hidden = True
lngLastRow2 = Cells(Cells.Rows.Count, "B").End(xlUp).Row
Range("A1:G" & lngLastRow2).Copy
ThisWorkbook.Worksheets("Add File Here").Activate
Range("A1").Select
ActiveSheet.Paste
Application.CutCopyMode = False
Exit For
End If
Next wSheet
If bFound = True Then Exit For
Next wBook
If rFound Is Nothing Then
MsgBox "No open file for Cvent003 Meetings Found. Make sure the most recent Cvent003 Excel WB is open!", vbCritical + vbOKOnly
Exit Sub
End If
'FIND ME
Else
Sheets("Add File Here").Select
Columns("A:A").Insert Shift:=xlToLeft, CopyOrigin:=xlFormatFromRightOrAbove
Range("A1").Value = "Meeting Name"
Dim lngLastRow As Long
lngLastRow = Cells(Cells.Rows.Count, "B").End(xlUp).Row
Range("A2:A" & lngLastRow).Value = Evaluate("=C2:C" & lngLastRow & "&"" - ""&" & "B2:B" & lngLastRow)
Columns(2).EntireColumn.Delete
Columns("A").Replace _
What:=";", Replacement:=""
Columns("A").Replace _
What:=":", Replacement:=""
Columns("A").Replace _
What:=",", Replacement:=""
Columns("A").Replace _
What:="(", Replacement:=""
Columns("A").Replace _
What:=")", Replacement:=""
Columns("A").Replace _
What:="{", Replacement:=""
Columns("A").Replace _
What:="}", Replacement:=""
Columns("A").Replace _
What:="[", Replacement:=""
Columns("A").Replace _
What:="]", Replacement:=""
Columns("A").Replace _
What:="~+", Replacement:=""
Columns("A").Replace _
What:="~*", Replacement:=""
Columns("A").Replace _
What:="~?", Replacement:=""
Columns("A").Replace _
What:="_", Replacement:=""
Columns("A").Replace _
What:=".", Replacement:=""
Columns("A").Replace _
What:="'", Replacement:=""
Columns("A").Replace _
What:="\", Replacement:=""
Columns("A").Replace _
What:="/", Replacement:=""
Columns("A").Replace _
What:=".", Replacement:=""
Columns("A").Replace _
What:="@", Replacement:=""
Columns("A").Replace _
What:=Chr(34), Replacement:=""
Columns("C:C").Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("C1").Value = "Client ID"
Columns("E:E").Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("E1").Value = "Planner Name"
Columns("J:J").Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("J1").Value = "External System Name"
Dim rngID As Range
Dim PID As Long
Dim ClientID As Long
ClientID = Range("B2:B" & lngLastRow).Copy
'Set the range in column A you want to loop through
Set rngID = Range("B2:B500")
For Each cell In rngID
'test if cell is empty
If cell.Value <> "" Then
'write to adjacent cell
'Range("G2:G" & lngLastRow).Value.Copy
Range("C2:C" & lngLastRow).Value = Range("B2:B" & lngLastRow).Value
'cell.Offset(0, 1).Value = EndDate.PasteSpecial
End If
Next
Dim cellID As Range
For Each cell In ThisWorkbook.ActiveSheet.Range("C2:C" & lngLastRow)
'If Len(cell.Value) > 3 Then cell.Value = Left(cell.Value, 3)
cell.Value = Left(cell.Value, 3)
Next cell
Columns(6).EntireColumn.Delete
Dim rngP As Range
Dim Pi As Long
'Set the range in column A you want to loop through
Set rngP = Range("D2:D500")
For Each cell In rngP
'test if cell is empty
If cell.Value <> "" Then
'write to adjacent cell
cell.Offset(0, 1).Value = "NA"
End If
Next
Dim rngE As Range
Dim Ei As Long
'Set the range in column A you want to loop through
Set rngE = Range("H2:H500")
For Each cell In rngE
'test if cell is empty
If cell.Value <> "" Then
'write to adjacent cell
cell.Offset(0, 1).Value = "Cvent"
End If
Next
ThisWorkbook.ActiveSheet.Cells.Interior.ColorIndex = 0
Dim answer As Integer
answer = MsgBox("Temporary File Prepared for Cvent003. Do you wish to proceed with MMS_NewMtgs file creation?", vbYesNo + vbQuestion, "Review & Proceed")
If answer = vbYes Then
Call Prepare_OutputFile
Else
MsgBox "Output file not created!! Please select - Click to create MMS Formatted File from Master Mapper.", vbOKOnly
End If
End If
End If
'MsgBox "File has been formatted for Cvent002 and is ready for MMS upload. Please copy values and paste to Standard Format File on your system!", vbOKOnly
ThisWorkbook.Saved = True
End Sub
什么是附加的ELSE? '如果answer003 = vbYes Then'行?另外,如果您使用'F8'逐步执行代码,那么当您希望它发生时,它会在何处跳过“ELSE”? – BruceWayne
帮你一个忙 - 学习如何始终缩进你的代码。这将使查找这些问题变得更容易。 – YowE3K
我对VBA还是很新的,只是让我的日常报告变得简单。 @BruceWayne - 寻找即后的其他“FIND ME” 否则应该被附接到 如果为IsEmpty(范围(“A1”))然后 –