Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Excel macro only works on one sheet

Posted on 2014-01-12
4
Medium Priority
?
565 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 1200 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
This article describes a serious pitfall that can happen when deleting shapes using VBA.
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…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …

604 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