简体   繁体   English

Excel VBA - 不循环通过我的 while 循环

[英]Excel VBA - Not looping through my while loop

So I have a code thats pasting data into an excel column, i'm then trying to loop through the data in that column and create a new sheet from data for every value in column B but it's stopping after completing this action once and not looping through the column.所以我有一个将数据粘贴到 excel 列中的代码,然后我尝试遍历该列中的数据,并从 B 列中的每个值的数据创建一个新工作表,但它在完成此操作一次后停止,而不是循环通过列。

Any ideas?有任何想法吗?

  i = 4

Do While Cells(i, 2).Value <> ""
    Worksheets("Front").Cells(5, 3).Value = Cells(i, 2)
    Worksheets("Front").Select
    Range("C2:M35").Select
    Selection.Copy
    Sheets("PlaceHolder").Select
    Range("C2").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:= _
        xlNone, SkipBlanks:=False, Transpose:=False
    Dim wks As Worksheet
    Set wks = ActiveSheet
    ActiveSheet.Copy After:=Worksheets(Sheets.Count)
    ActiveSheet.Name = wks.Range("C5").Value
    i = i + 1
Loop

"Cells" and "Range" in your code both refer to the ActiveWorksheet, which presumably starts out as Worksheets("Front"), then you change ActiveWorksheet to Worksheets("Placeholder") with the Worksheets("Placeholder").Select statement.代码中的“单元格”和“范围”都指 ActiveWorksheet,它可能以 Worksheets("Front") 开头,然后使用Worksheets("Placeholder").Select语句将 ActiveWorksheet 更改为 Worksheets("Placeholder") . I'm not sure if creating the new worksheet sets ActiveWorksheet to the new worksheet or not and I shouldn't have to know to make the code work.我不确定创建新工作表是否将 ActiveWorksheet 设置为新工作表,我不应该知道使代码工作。 Instead of relying on the implicit reference to ActiveWorksheet, you should use explicit references like this.您应该使用像这样的显式引用,而不是依赖对 ActiveWorksheet 的隐式引用。

'Style note: Always put Option Explict at the top of the module
'and declare all your variables at the top of the subroutine. 
Dim wsFront As Worksheet
Dim wsPlaceholder As Worksheet
Dim wsNewSheet As Worksheet
Dim i As Integer

Set wsFront = Worksheets("Front")
Set wsPlaceholder = Worksheets("Placeholder")

i = 4

Do While wsFront.Cells(i, 2).Value <> ""

    ' Copy data from Worksheets("Front")
    wsFront.Cells(5, 3).Value = Cells(i, 2)
    wsFront.Select
    wsFront.Range("C2:M35").Select
    Selection.Copy
    
    ' Paste data to Worksheets("Placeholder")
    wsPlaceholder.Select
    wsPlaceholder.Range("C2").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:= _
        xlNone, SkipBlanks:=False, Transpose:=False
    
    ' Copy Worksheets("Placeholder") to new worksheet
    wsPlaceholder.Copy After:=Worksheets(Sheets.Count)
    
    'Get a reference to the new worksheet
    Set wsNewSheet = Worksheets(Worksheets.Count)
    wsNewSheet.Name = wsPlaceholder.Range("C5").Value
    i = i + 1
Loop

There's typically no need to select or activate anything when using VBA in Excel (despite what the macro recorder might try to suggest)在 Excel 中使用 VBA 时,通常不需要 select 或激活任何东西(尽管宏记录器可能会尝试建议)

If you plan on doing much VBA work in Excel then this is highly-recommended reading: How to avoid using Select in Excel VBA If you plan on doing much VBA work in Excel then this is highly-recommended reading: How to avoid using Select in Excel VBA

Dim wsData As Worksheet, wsFront As Worksheet, wsPH As Worksheet, v
Dim wb As Workbook, i As Long

Set wb = ThisWorkbook
Set wsData = ActiveSheet 'or some other specific sheet
Set wsFront = wb.Worksheets("Front")
Set wsPH = wb.Worksheets("PlaceHolder")

i = 4

Do While wsData.Cells(i, 2).Value <> ""
    v = wsData.Cells(i, 2).Value
    wsFront.Cells(5, 3).Value = v
    
    wsFront.Range("C2:M35").Copy
    With wsPH.Range("C2")
        .PasteSpecial Paste:=xlPasteValues
        .PasteSpecial Paste:=xlPasteValuesAndNumberFormats
    End With
    
    wsPH.Copy After:=wb.Worksheets(Sheets.Count)
    wb.Worksheets(Sheets.Count).Name = v
Loop

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM