简体   繁体   English

VBA Excel宏多次复制第一行,而不是转到第二行

[英]VBA Excel Macro copies the first line multiple times instead of going to the second line

I need your input. 我需要您的输入。

On Sheet 1 "In Stock", I have 13 columns of data with about 300 rows. 在工作表1“库存”中,我有13列数据,大约有300行。 I need to copy this data into sheet 2 "Low" based on the value of column 11. 我需要根据第11列的值将此数据复制到工作表2“低”中。

If column 11 is less than 98%, I need to copy the data. 如果第11列小于98%,则需要复制数据。

Here is my macro: 这是我的宏:

Dim z As Integer

z = 3

For i = 20 To Worksheets("In Stock").TextBox1.Value
If Worksheets("In Stock").Cells(i, 11) < "98" Then GoTo JMP1
GoTo JMP2

JMP1:
Worksheets("Low").Cells(z, 1).Value = Worksheets("In Stock").Cells(20,   1).Value
Worksheets("Low").Cells(z, 2).Value = Worksheets("In Stock").Cells(20, 2).Value
Worksheets("Low").Cells(z, 3).Value = Worksheets("In Stock").Cells(20, 3).Value
Worksheets("Low").Cells(z, 4).Value = Worksheets("In Stock").Cells(20, 4).Value
Worksheets("Low").Cells(z, 5).Value = Worksheets("In Stock").Cells(20, 5).Value
Worksheets("Low").Cells(z, 6).Value = Worksheets("In Stock").Cells(20, 6).Value
Worksheets("Low").Cells(z, 7).Value = Worksheets("In Stock").Cells(20, 7).Value
Worksheets("Low").Cells(z, 8).Value = Worksheets("In Stock").Cells(20, 8).Value
Worksheets("Low").Cells(z, 9).Value = Worksheets("In Stock").Cells(20, 9).Value
Worksheets("Low").Cells(z, 10).Value = Worksheets("In Stock").Cells(20, 10).Value
Worksheets("Low").Cells(z, 11).Value = Worksheets("In Stock").Cells(20, 11).Value
Worksheets("Low").Cells(z, 12).Value = Worksheets("In Stock").Cells(20, 12).Value
Worksheets("Low").Cells(z, 13).Value = Worksheets("In Stock").Cells(20, 13).Value

z = z + 1

JMP2:
Next i
Worksheets("Low").Select

As you can see, it's a simple copy/paste based on an if but for some reason- the macro just copies the first line instead of skipping to the second line that is under 98%. 如您所见,它是基于if的简单复制/粘贴,但出于某些原因-宏仅复制第一行,而不是跳到98%以下的第二行。

Also, instead of copying the full value of the data in the columns- I am just seeing zeros right now. 另外,我没有复制列中数据的完整值,而是现在看到的是零。

Many thanks for your help. 非常感谢您的帮助。

GoTo is generally considered the bane of modern society and should be avoided whenever possible. GoTo通常被认为是现代社会的祸根,应尽可能避免使用。 It makes code very difficult to follow for the person coming along behind you (or for you when you have to revisit it in 6 months). 对于跟在你身后的人(或当你不得不在6个月内重新审视它),这会使代码很难遵循。 This will make your code much more readable: 这将使您的代码更具可读性:

Option Explicit
Dim SourceRow As Integer
Dim DestRow as Integer
Dim Col as Integer 

SourceRow = 3

For DestRow = 20 To Worksheets("In Stock").TextBox1.Value
  If Worksheets("In Stock").Cells(DestRow, 11) < 0.98 Then  
    With Worksheets("Low")
      For Col = 1 to 13
        .Cells(SourceRow, Col) = Worksheets("In Stock").Cells(DestRow, Col)
      Next
    End With
    SourceRow = SourceRow + 1
  End If
Next 

Worksheets("Low").Select

As was mentioned in the comments, since you're looking for a percentage, your If should be comparing to 0.98 , and since it's a numeric, there's no reason to put it in quotes "98" and force Excel to convert it from a string to a float every time. 正如评论中提到的那样,由于您要查找百分比,因此If应该与0.98进行比较,并且由于它是数字,因此没有理由将其放在引号"98"并强制Excel将其从字符串转换为每次都浮动。

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

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