在所有工作表上运行VBA

时间:2017-03-24 11:56:46

标签: excel vba excel-vba

我对VBA的了解接近于零,但是我找到了一个可以改变以达到我的目的的代码。现在唯一的事情是这个代码只为名为" test"的工作表运行。我需要为同一文件上的所有工作表运行此代码。

非常感谢。

Sub MyCopyPasteValues()
' CopyPasteValus Macro
' Macro to hard code last weeks data.

Dim i As Byte, ws As Worksheet

Set ws = Worksheets("test")

For y = 7 To 24

    For i = 3 To 4

        If ws.Cells(y, 2).Value < ws.Range("K" & 7).Value Then

            ws.Cells(y, i).Value = ws.Cells(y, i).Value

        End If

    Next i

Next y

End Sub

2 个答案:

答案 0 :(得分:2)

这应该这样做:

Sub MyCopyPasteValues() ' CopyPasteValus Macro ' Macro to hard code last weeks data.
Dim i As Byte, ws As Worksheet
For Each ws In Worksheets
    For y = 7 To 24
        For i = 3 To 4
            If ws.Cells(y, 2).Value < ws.Range("K" & 7).Value Then
                ws.Cells(y, i).Value = ws.Cells(y, i).Value
            End If
        Next i
    Next y
Next
End Sub

答案 1 :(得分:1)

我想这就是你要找的东西:

Option Explicit

Sub MyCopyPasteValues()
' CopyPasteValus Macro
' Macro to hard code last weeks data.

Dim ws As Worksheet
Dim Y As Long, i As Long

For Each ws In ThisWorkbook.Worksheets
    For Y = 7 To 24
        For i = 3 To 4
            If ws.Cells(Y, 2).Value < ws.Range("K7").Value Then
                ws.Cells(Y, i).Value = ws.Cells(Y, i).Value
            End If
        Next i
    Next Y
Next ws

End Sub