Solved

VDB : Change of working sheet

Posted on 2000-05-12
4
354 Views
Last Modified: 2011-10-03
I have written a "macro" for excell that make some changes on two given sheets. Both traitments are similar on the two sheets. Run as two separated macros, they works properly but if I put both macros in one, a part of the second traitement is done on the first sheet instead of the second sheet, even if I change the working sheet. A part of the second traitement is properly done on the right sheet but at the end of a "with" the second traitement is done on the first sheet????
Here is the code :

Sub SMSProfSkills()

Set ws = ActiveWorkbook.Sheets("Professional Skills")
    ws.AutoFilterMode = False

    ws.Columns("A:A").Delete Shift:=xlToLeft
    With ws.Rows("3:3")
        .HorizontalAlignment = xlRight
        .VerticalAlignment = xlBottom
        .WrapText = False
        .Orientation = 90
        .ShrinkToFit = False
        .MergeCells = False
    End With
    Cells.Select
    Selection.ColumnWidth = 3
    Columns("A:A").ColumnWidth = 7
    Columns("B:B").ColumnWidth = 20
    Columns("C:C").ColumnWidth = 15
    Columns("G:G").ColumnWidth = 5
    Range("A3").CurrentRegion.Select
    nbrows = Selection.Rows.Count - 3
    nbcols = Selection.Columns.Count - 7

    For i = 1 To nbrows
       For j = 1 To nbcols
       Set celltoclean = Range("G3").Offset(i, j)
       celltoclean.Select
       celltoclean.Value = celltoclean.Value
       Select Case celltoclean.Value
         Case "2"
           celltoclean.Cells.Interior.ColorIndex = 43
         Case "3"
           celltoclean.Cells.Interior.ColorIndex = 5
         Case "4"
           celltoclean.Cells.Interior.ColorIndex = 6
         Case "5"
           celltoclean.Cells.Interior.ColorIndex = 3
           
       End Select
       Next j
    Next i
    Range("A1").Select
    With ActiveSheet.PageSetup
        .PrintTitleRows = "$1:$3"
        .PrintTitleColumns = "$A:$B"
    End With

    Rows("3:3").RowHeight = 160
End Sub

HERE is the second traitement
'
' Macro recorded 11/05/00

    Dim ws As Worksheet

    Set ws = ActiveWorkbook.Sheets("Product Skills")
    ws.AutoFilterMode = False

    ws.Columns("A:A").Delete Shift:=xlToLeft
    With ws.Rows("3:3")
        .HorizontalAlignment = xlRight
        .VerticalAlignment = xlBottom
        .WrapText = False
        .Orientation = 90
        .ShrinkToFit = False
        .MergeCells = False
    End With            
AFTER THIS LINE, THE MACRO IS WORKING ON THE FIRST SHEET INSTEAD OF THE SECOND
    Cells.Select
     Selection.ColumnWidth = 3
    Columns("A:A").ColumnWidth = 7
    Columns("B:B").ColumnWidth = 20
    Columns("C:C").ColumnWidth = 15
    Columns("G:G").ColumnWidth = 5
    Range("A3").CurrentRegion.Select
    nbrows = Selection.Rows.Count - 3
    nbcols = Selection.Columns.Count - 7
'    celltoclean = Range("H4")
   
    For i = 1 To nbrows
       For j = 1 To nbcols
       Set celltoclean = Range("G3").Offset(i, j)
       celltoclean.Select
       celltoclean.Value = celltoclean.Value
       Select Case celltoclean.Value
         Case "2"
           celltoclean.Cells.Interior.ColorIndex = 43
         Case "3"
           celltoclean.Cells.Interior.ColorIndex = 5
         Case "4"
           celltoclean.Cells.Interior.ColorIndex = 6
         Case "5"
           celltoclean.Cells.Interior.ColorIndex = 3
           
       End Select
       Next j
    Next i
    Range("A1").Select
    With ActiveSheet.PageSetup
        .PrintTitleRows = "$1:$3"
        .PrintTitleColumns = "$A:$B"
    End With

    Rows("3:3").RowHeight = 160
End Sub

0
Comment
Question by:pverbeeck
  • 2
4 Comments
 
LVL 4

Expert Comment

by:arcusd
ID: 2803739
try putting a with Ws statement before the line....

with ws
                          Cells.Select
                         Selection.ColumnWidth = 3
                        Columns("A:A").ColumnWidth = 7
                        Columns("B:B").ColumnWidth = 20
                        Columns("C:C").ColumnWidth = 15
                        Columns("G:G").ColumnWidth = 5
                        Range("A3").CurrentRegion.Select
                        nbrows = Selection.Rows.Count - 3
                        nbcols = Selection.Columns.Count - 7
                    '    celltoclean = Range("H4")
                         
                        For i = 1 To nbrows
                           For j = 1 To nbcols
                           Set celltoclean = Range("G3").Offset(i, j)
                           celltoclean.Select
                           celltoclean.Value = celltoclean.Value
                           Select Case celltoclean.Value
                             Case "2"
                               celltoclean.Cells.Interior.ColorIndex = 43
                             Case "3"
                               celltoclean.Cells.Interior.ColorIndex = 5
                             Case "4"
                               celltoclean.Cells.Interior.ColorIndex = 6
                             Case "5"
                               celltoclean.Cells.Interior.ColorIndex = 3
                               
                           End Select
                           Next j
                        Next i
                        Range("A1").Select

end with
0
 
LVL 43

Accepted Solution

by:
TimCottee earned 101 total points
ID: 2803753
pverbeeck, use ws.Activate immediately after the Set ws = ActiveWorkbook.Sheets("Sheetname") line in each section. This should ensure that the correct worksheet is active when the Cells.Select method is called.
0
 

Author Comment

by:pverbeeck
ID: 2803792
It doesn't work. But thanks for your help.
0
 

Author Comment

by:pverbeeck
ID: 2803803
Sorry timcottee, I did not see your comment before posting my previous one. But your response is working properly.
Thanks a lot
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

762 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