Solved

Excel macro only works on one sheet

Posted on 2014-01-12
4
494 Views
Last Modified: 2014-01-12
Hello,
I would like this macro to run on sheets 1-4 but it just repeats itself on Sheet1 four times instead of moving on to Sheet 2 etc...

Sub FillColumns1()

    Dim r1 As Long, r2 As Long, i As Long
    Dim ws As Worksheet
    Dim wsCounter As Long


    'For wsCount = 1 To ActiveWorkbook.Worksheets.Count
      For wsCount = 1 To 4
        Set ws = ActiveWorkbook.Worksheets(wsCount)
        With ws
            r1 = .range("A" & .Rows.Count).End(xlUp).Row
            r2 = .range("H" & .Rows.Count).End(xlUp).Row
            range(Cells(r2, 8), Cells(r2, 22)).AutoFill range(Cells(r2, 8), Cells(r1, 22))
        End With
    Next

End Sub
0
Comment
Question by:CC10
  • 2
4 Comments
 
LVL 27

Expert Comment

by:MacroShadow
ID: 39774729
What is it:
Dim wsCounter As Long
or
For wsCount = 1 To 4
0
 
LVL 43

Expert Comment

by:Saqib Husain, Syed
ID: 39774757
Try

            range(ws.Cells(r2, 8), ws.Cells(r2, 22)).AutoFill range(ws.Cells(r2, 8), ws.Cells(r1, 22))
0
 
LVL 43

Accepted Solution

by:
Saqib Husain, Syed earned 300 total points
ID: 39774759
or simply

            range(.Cells(r2, 8), .Cells(r2, 22)).AutoFill range(.Cells(r2, 8), .Cells(r1, 22))
0
 

Author Closing Comment

by:CC10
ID: 39774905
Thats fine. Thanks very much.
CC
0

Featured Post

ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Drop Down List with Unique/Distinct Values (Part II - ComboBox or ListBox and Data Validation List Bonus!) David Miller (dlmille) Intro This article focuses on delivering unique, sorted lists to list objects (e.g., ComboBox, ListBox) and Dat…
Convert between Excel file formats (.XLS, .XLSX, .XLSM) with/without macro option David Miller (dlmille) Intro Over this past Fall, I've had the opportunity to see several similar requests and have developed a couple related solutions associate…
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question