2017-07-31 65 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