Solved

VDB : Change of working sheet

Posted on 2000-05-12
4
350 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

ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

821 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