0

我正在尝试编写一个代码,该代码使用 IF 语句提取数据并扫描数据行,如果它有数据,则复制它和所有相关数据,如果它什么都不做。

我现在的计划是这样的

  1. 如果单元格 X 有数据,则在最新输入后将该数据和所有相关数据复制到主表。然后转到下一行并做同样的事情。
  2. 创建一个代码将整个宏偏移一定数量的列(Quarters 列使这变得困难,因为我提取的数据是几个月)
  3. 创建一个宏,从不是第一个工作表选项卡的主工作表选项卡之后的所有选项卡中获取数据。

这是我的excel文件。不确定是否有地方可以在 stackoverflow 上上传,但这里是发送空间。这是我获取数据的确切格式。这是一个更大的工作文件的一小部分,其中包含 60 多个数据选项卡。 http://www.sendspace.com/file/3irz5n

到目前为止,这是我的代码,它不起作用。我想我有一些语法错误。

编辑:修复了几个问题。除了这个问题,代码运行良好。

这是我当前的代码。我的源数据中的空白单元格有问题。当有一个空白单元格时,它不会复制任何内容并在我的主表中偏移我的数据。例如,如果 A1 列中的数据为 200,则 A2 为 300。假设 B2 中的数据转到 B1,因为 B1 为空白。问题代码从“将单元格复制到母版表”开始

Sub Test()

Dim imaxrow As Double
Dim ws As Worksheet

For Each ws In Worksheets
    'Need to find a way to take data from sheets AFTER macro test sheet
    If ws.Name <> "Macro Test Sheet" Then
    'Rows of data to be extracted
    imaxrow = 22 'Max rows
    'Use to offset columns to next dataset
        For Each E In Array(0, 11)
        'Starting row to copy data
        For iRow = 10 To imaxrow
            If ws.Cells(iRow, 21).Value = "" Then
                'Nothing in this cell.
                'Do nothing.
                Else
                ' Copy Cell data to mastersheet
                ws.Cells(iRow, 21 + E).Copy Destination:=Worksheets("macro test sheet").Cells(Rows.Count, "F").End(xlUp).Offset(1, 0)
                ws.Cells(iRow, 22 + E).Copy Destination:=Worksheets("macro test sheet").Cells(Rows.Count, "G").End(xlUp).Offset(1, 0)
                ws.Cells(iRow, 23 + E).Copy Destination:=Worksheets("macro test sheet").Cells(Rows.Count, "H").End(xlUp).Offset(1, 0)
                ws.Cells(iRow, 24 + E).Copy Destination:=Worksheets("macro test sheet").Cells(Rows.Count, "I").End(xlUp).Offset(1, 0)
                ws.Cells(iRow, 4).Copy Destination:=Worksheets("macro test sheet").Cells(Rows.Count, "C").End(xlUp).Offset(1, 0)
                ws.Cells(iRow, 3).Copy Destination:=Worksheets("macro test sheet").Cells(Rows.Count, "B").End(xlUp).Offset(1, 0)
                ws.Cells(5, 1).Copy Destination:=Worksheets("macro test sheet").Cells(Rows.Count, "A").End(xlUp).Offset(1, 0)
                ws.Cells(6, 21 + E).Copy Destination:=Worksheets("macro test sheet").Cells(Rows.Count, "D").End(xlUp).Offset(1, 0)
                ws.Cells(7, 21 + E).Copy Destination:=Worksheets("macro test sheet").Cells(Rows.Count, "E").End(xlUp).Offset(1, 0)
            End If
        Next iRow
        Next E
     End If
    Next ws

End Sub

再次感谢您的帮助和提示。

4

1 回答 1

0

“我认为我有一些语法错误”没有帮助。您需要告诉我们您收到的错误消息以及该错误所指的行。

但这可能是错误的:

WS.Range(.Cells(10, 7))

除非.Cells(10, 7)碰巧包含一个单元格地址。

如果您需要进一步的帮助,您需要提出一个具体问题。

添加

If Cells(10, 7) = "" Then

我认为这应该在“宏测试表”中查看,所以这也应该在前面加上一个点,否则它正在查看当前的 ActiveSheet。

于 2013-07-21T10:43:08.580 回答