VBA Blank Cells在复制时偏移数据

时间:2013-07-21 05:21:07

标签: vba if-statement statements

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

我目前的计划是

  1. 如果单元格X在最近输入后将数据和所有相关数据复制到主表单。然后转到下一行并执行相同操作。
  2. 创建一个代码将整个宏偏移一定数量的列(Quarters列使得这很困难,因为我提取的数据是几个月)
  3. 创建一个宏,该宏从主工作表标签(而不是第一个工作表标签)后的所有选项卡中获取数据。
  4. 这是我的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
    

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

1 个答案:

答案 0 :(得分:0)

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

但这可能是错误的:

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

除非.Cells(10, 7)恰好包含单元格地址。

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

<强>加

If Cells(10, 7) = "" Then

我认为这应该在“宏测试表”中查找,所以这也应该以一个点开头,否则它会查找当前的ActiveSheet。