简体   繁体   English

Excel VBA:扫描一张纸并将数据复制到另一张纸

[英]Excel VBA: scanning one sheet and copying data to another

I need some help with this code below. 我需要以下代码的帮助。

I am trying to write a program which will scan a column in sheet 2 and have a conditional statement where if an "x" character is encountered then the data adjacent to that cell in the same row will be copied and pasted into a different sheet. 我正在尝试编写一个程序,该程序将扫描工作表2中的一列并有一个条件语句,其中如果遇到“ x”字符,则将复制同一行中与该单元格相邻的数据并将其粘贴到另一张工作表中。

The program I have currently successfully copies A1 into Sheet 1 but not the other ones, I'm assuming there is some kind of incrementation error, but I can't detect it. 我目前已成功将A1复制到工作表1中的程序,但没有成功将其复制到工作表1中,我假设存在某种增量错误,但我无法检测到它。

Any help is appreciated. 任何帮助表示赞赏。

Sub autofill_DSR()

' Variable Declarations:
'   We want variables which keep a count of the total number of rows,
' the item code character values associated, a count of the total
' number of "x" characters encountered, and a flag to signify
' sheet transfer activation


    Dim sheet_flip_flag, x_count, n As Integer
    Dim item_a, item_b As String

    Process_Control_NumRows = 16

    Sheets(Array("Sheet1", "Sheet2")).Select
    Sheets("Sheet2").Activate
    Range("D1").Select

        For n = 0 To n = (Process_Control_NumRows - 1)

            If (ActiveCell.Offset(n, 0) = "x" Or ActiveCell.Offset(n, 0) = "X") Then

                item_a = ActiveCell.Offset(n, -3).Value
                item_b = ActiveCell.Offset(n, -2).Value

                Sheets("Sheet1").Activate
                Range("A1").Select
                ActiveCell.Offset(n, 0).Value = (item_a & item_b)

                Sheets("Sheet2").Activate
                Range("D1").Select

            End If

            ActiveCell.Offset(n, 1).Value = NN

        Next n

End Sub

Update the code of the For loop in your code to read 更新代码中的For循环代码以进行读取

For n = 0 To (Process_Control_NumRows - 1)

This will increment n as you want it to. 这将根据需要增加n。

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

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