使用For循环

时间:2017-04-04 10:03:01

标签: excel vba excel-vba

我写了这段代码。这段代码在做什么。它在第一列中查找,当找到时,它会复制从第2列开始的值row意味着第15行一步一步地将第I列添加到其他工作簿中然后它应该转到下一行,其中存在下一个是意味着在我的情况下它的第17行但不幸的是它不会。因为我已经习惯了循环所以我希望它在复制第15行的值之后它将继续下去是的意思是第17行,但不幸的是它不会发生。

  1. 找到是。在我的情况下,First Yes在第15行。
  2. 将值从下一列复制到其他工作簿。
  3. 然后在完成第15行中的所有值后,它将移至下一个是。
  4. 下一个是我的情况是在第17行,但它应该在任何一行,这就是为什么我为此使用For循环。
  5. 有人能让我知道我错在哪里吗?

    另外看看图片

       Dim i As Integer
       Dim Percent As Variant
       Dim Search As String
       Dim wkb As Workbook
       Dim WrkSht As Worksheet
    
         Search = InputBox("Enter your search word here")
         If Search = "" Then Exit Sub
         For i = 1 To Range("A65536").Cells.End(xlUp).Row
         If Cells(i, 1) = Search Then GoTo Other
         Next i
         MsgBox "Not avaiable"
         Exit Sub
        Other:
        For i = 1 To Range("A65536").Cells.End(xlUp).Row
         If Cells(i, 1) = Search Then
    
            Set wb = Workbooks.Add
            ThisWorkbook.Activate
            Worksheets("Template").Copy Before:=wb.Sheets(1)
            wb.Activate
            wb.SaveAs "U:\test1.xlsx"
    
            Workbooks("Mappe.xlsm").Worksheets("Sheet1").Activate
    
    
            Cells(i, 2).Select
    
    
        Workbooks("Mappe.xlsm").Worksheets("Sheet1").Cells(i, 2).Copy
    
       Workbooks("test1.xlsx").Worksheets("Template").Activate
        Cells(16, 3).Select
       ActiveSheet.Paste
    
      Workbooks("Mappe.xlsm").Worksheets("Sheet1").Cells(i, 3).Copy
      Workbooks("test1.xlsx").Worksheets("Template").Activate
      Cells(17, 3).Select
      ActiveSheet.Paste
    
      Workbooks("Mappe.xlsm").Worksheets("Sheet1").Cells(i, 4).Copy
      Workbooks("test1.xlsx").Worksheets("Template").Activate
      Cells(18, 3).Select
      ActiveSheet.Paste
    
      Workbooks("Mappe.xlsm").Worksheets("Sheet1").Cells(i, 5).Copy
      Workbooks("test1.xlsx").Worksheets("Template").Activate
      Cells(19, 3).Select
      ActiveSheet.Paste
      Workbooks("Mappe.xlsm").Worksheets("Sheet1").Cells(i, 6).Copy
      Workbooks("test1.xlsx").Worksheets("Template").Activate
      Cells(20, 3).Select
      ActiveSheet.Paste
      Workbooks("Mappe.xlsm").Worksheets("Sheet1").Cells(i, 7).Copy
      Workbooks("test1.xlsx").Worksheets("Template").Activate
      Cells(21, 3).Select
      ActiveSheet.Paste
      Workbooks("Mappe.xlsm").Worksheets("Sheet1").Cells(i, 8).Copy
      Workbooks("test1.xlsx").Worksheets("Template").Activate
      Cells(22, 3).Select
      ActiveSheet.Paste
      Workbooks("Mappe.xlsm").Worksheets("Sheet1").Cells(i, 9).Copy
      Workbooks("test1.xlsx").Worksheets("Template").Activate
      Cells(23, 3).Select
      ActiveSheet.Paste
    
       Workbooks("Mappe.xlsm").Worksheets("Sheet1").Cells(i, 2).Copy
    
       Workbooks("test1.xlsx").Worksheets("Template").Activate
       Cells(16, 4).Select
       ActiveSheet.Paste
        Workbooks("Mappe.xlsm").Worksheets("Sheet1").Cells(i, 3).Copy
        Workbooks("test1.xlsx").Worksheets("Template").Activate
        Cells(17, 4).Select
        ActiveSheet.Paste
        Workbooks("Mappe.xlsm").Worksheets("Sheet1").Cells(i, 4).Copy
        Workbooks("test1.xlsx").Worksheets("Template").Activate
        Cells(18, 4).Select
        ActiveSheet.Paste
        Workbooks("Mappe.xlsm").Worksheets("Sheet1").Cells(i, 5).Copy
        Workbooks("test1.xlsx").Worksheets("Template").Activate
        Cells(19, 4).Select
        ActiveSheet.Paste
        Workbooks("Mappe.xlsm").Worksheets("Sheet1").Cells(i, 6).Copy
        Workbooks("test1.xlsx").Worksheets("Template").Activate
        Cells(20, 4).Select
        ActiveSheet.Paste
        Workbooks("Mappe.xlsm").Worksheets("Sheet1").Cells(i, 7).Copy
        Workbooks("test1.xlsx").Worksheets("Template").Activate
        Cells(21, 4).Select
        ActiveSheet.Paste
        Workbooks("Mappe.xlsm").Worksheets("Sheet1").Cells(i, 8).Copy
        Workbooks("test1.xlsx").Worksheets("Template").Activate
        Cells(22, 4).Select
        ActiveSheet.Paste
        Workbooks("Mappe.xlsm").Worksheets("Sheet1").Cells(i, 9).Copy
        Workbooks("test1.xlsx").Worksheets("Template").Activate
        Cells(23, 4).Select
        ActiveSheet.Paste
    
       End If
       Next i
    

    Pictural Explanation of problem

    Values in new formed sheets

1 个答案:

答案 0 :(得分:0)

此子应确定活动工作表的已用行,循环使用的行,如果行的第一列为yes,则它会创建一个新工作簿,从而复制template活动工作簿并将第2列复制到该列({1}}到该模板中(在新工作簿中)。然后它保存新工作簿并关闭它。然后它循环进入下一行。

这对你来说应该是一个良好的开端。

I