2017-07-31 169 views
0

我又回到了我的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 
+0

什么是附加的ELSE? '如果answer003 = vbYes Then'行?另外,如果您使用'F8'逐步执行代码,那么当您希望它发生时,它会在何处跳过“ELSE”? – BruceWayne

+1

帮你一个忙 - 学习如何始终缩进你的代码。这将使查找这些问题变得更容易。 – YowE3K

+0

我对VBA还是很新的,只是让我的日常报告变得简单。 @BruceWayne - 寻找即后的其他“FIND ME” 否则应该被附接到 如果为IsEmpty(范围(“A1”))然后 –

回答

0

我不知道你想什么时候执行哪些代码100%,但如果它只是你的情况下,已经放置代码的If answer003 = vbYes Then代替If IsEmpty(Range("A1")) ThenElse块,然后只要将代码后,的If answer003 = vbYes ThenEnd If

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 

     End If 

    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 
    '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 

注:我不知道该代码是否现在具有逻辑意义 - 我只是重新排列的块,而不试图理解你在做什么。我特别不确定如果用户对您的"Please check the Data Sheet. No value found in first row! Do you wish to find Cvent003 file in open workbooks and start process?"问题回答“否”,什么都不做。即如果他们回答“否”,您的代码将工作簿标记为已保存 - 是否真的合适?

+0

我非常抱歉成为一个痛苦的人,但是通过这段代码,现在如果单元格A1不是空白,它会执行,但是如果它是空白的,它会将数据复制到我想要的工作表并停下来! 要回答你以前的评论,如果选择否,我很好,只是退出子和停在那里! –

+0

@AkshaySachdev - 你是否期望'A1'真的是空的,或者有一个公式可以评估为''“'?另外,你可以用'F8'逐行浏览代码 - 我建议这样做,并且看看为什么代码停止,因为可能有一个条件(不)被满足。 – BruceWayne

+0

所以你不希望这个代码作为'Else'的一部分被执行,你希望它总是被执行吗?如果是这样,在'End If'之后移动它。 – YowE3K