Solved

VDB : Change of working sheet

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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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…
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…

705 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