简体   繁体   English

VBA-复制和粘贴正确的数据

[英]VBA - Copy and Pasting the correct data

I am having trouble copying over various ranges into corresponding sheets. 我在将各种范围的复印到相应的工作表时遇到麻烦。

dateRng = 12/19/2016 00:00:00 - 12/19/2016 23:59:59
num = 11
NumPts = 33
Sheets.Count = 16

In this exaple this is what each variable is equal to ^. 在这个示例中,这就是每个变量等于^的含义。

Dim dateRng As String, num As Integer, j As Integer, sh As Worksheet
dateRng = Sheets("Input Raw Data").Range("B" & counter + 2).Value
num = Sheets("Tool Setup").Range("C18").Value
NumPts = num * 3

For s = 1 To Sheets.Count
    For y = 1 To NumPts
        With Sheets(s)
        For j = 1 To num
            If .Name = j Then
                Sheets("Reporting").Range("A" & (12 * y - 7) & ":" & fConvertToLetter & (12 * y + 1)) _
                .Copy (.Range("A2"))
                .Range("A1") = dateRng
                .Name = Sheets("Point Names").Range("B" & (3 * j - 1))
            End If
            Columns("B:B").EntireColumn.AutoFit
        Next j
        End With
    Next y
Next s

So currently, I am getting the first range copied into the first corresponding sheet. 因此,目前,我正在将第一个范围复制到第一个相应的工作表中。 But it is also pasting into all the rest of the sheets afterwards. 但此后它也将粘贴到所有其他工作表中。 Rather than pairing up the second range with the second sheet. 而不是将第二个范围与第二个表配对。 Thanks in advance for your help. 在此先感谢您的帮助。

I don't quite understand the question. 我不太明白这个问题。 If you want to paste on different ranges through different sheets I believe you have your cycles mixed up. 如果您想通过不同的纸张粘贴在不同的范围上,我相信您的循环是混乱的。

Since your For j is running inside For s, you go through all the ranges in your first sheet before passing to the next sheet. 由于您的For j在For s内部运行,因此您需要遍历第一张工作表中的所有范围,然后再传递到下一张工作表。

So maybe: 所以也许:

For s = 1 To Sheets.Count
For y = 1 To NumPts
    With Sheets(s)
        If .Name = s Then
            Sheets("Reporting").Range("A" & (12 * y - 7) & ":" & fConvertToLetter & (12 * y + 1)) _
            .Copy (.Range("A2"))
            .Range("A1") = dateRng
            .Name = Sheets("Point Names").Range("B" & (3 * s - 1))
        End If
        Columns("B:B").EntireColumn.AutoFit
    End With
Next y
Next s

This would only apply if I'm understanding correctly and you want your ranges to move with every next sheet. 仅当我正确理解并且您希望范围随下一页移动时,这才适用。

I was able to fix the issue by breaking up the For Statements into two blocks. 我能够通过将For Statements分成两个块来解决此问题。

For s = 1 To Sheets.Count
    With Sheets(s)
    For j = 1 To num
        If .Name = j Then
            .Range("A1:C1").Merge
            .Range("A1") = dateRng
            .Name = Sheets("Point Names").Range("B" & (3 * j - 1))
        End If
    Next j
    End With
Next s
For s = 1 To Sheets.Count
    With Sheets(s)
    For y = 1 To NumPts
        If .Name = Sheets("Reporting").Range("B" & (12 * y - 5)) Then
            Sheets("Reporting").Range("A" & (12 * y - 6) & _
        ":" & fConvertToLetter & (12 * y + 20)) _
        .Copy (.Range("A2"))
        .Columns("A:A").ColumnWidth = 12
        .Columns("B:B").EntireColumn.AutoFit
    End If
    y = y + 2
    Next y
End With
Next s

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

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