Cant Set FirstSel = ws.select. SecondSel = ws.select

Vanda_a

Well-known Member
Joined
Oct 29, 2012
Messages
934
Dear all

Could someone help me with this thing please

Rich (BB code):
Dim FirstSel As .....
Dim SecondSel As ....
Dim ws As Worksheets

    For Each ws In Worksheets
        Select Case True
            Case ws.Name Like "INV*"
                FirstSel = ws.Select '''''''''''''''''''''''''''''Debug here.
            Case ws.Name Like "Reim*"
                SecondSel = ws.Select '''''''''''''''''''''''''''''Debug here.
            Case ws.Name Like "DN*"
                MsgBox ws.Name
            Case Else
                MsgBox ws.Name
        End Select
    Next ws

Why can't I do the thing like that?
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
40,681
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
  2. MacOS
Select doesn't return an object - it just selects something. If you are trying to assign a variable, use:
Code:
Set FirstSel = ws
 
Upvote 0

Vanda_a

Well-known Member
Joined
Oct 29, 2012
Messages
934
Select doesn't return an object - it just selects something. If you are trying to assign a variable, use:
Rich (BB code):
Set FirstSel = ws

What to dim FirstSel and Second Sel? Dim as Variant?

And how can I select both of them?

my next code
Rich (BB code):
If FirstSel Like "INV*" And SecondSel Like "Reim*" Then
Selection.SaveAs Filename:=Path & FirstSel.Name & ". xlsx         "''''''''''''''''Select both of them and save as
End if

Thank you very much
 
Upvote 0

Vanda_a

Well-known Member
Joined
Oct 29, 2012
Messages
934
Rich (BB code):
If FirstSel Like "INV*" And SecondSel Like "Reim*" Then ''''''''''''''also Debug




hmmmm (headbang)
 
Upvote 0

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
40,681
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
  2. MacOS
ADVERTISEMENT
It appears you want String variables, in which case you'll need:
Code:
Dim FirstSel As String
Dim SecondSel As String
Dim ws As Worksheet

    For Each ws In Worksheets
        Select Case True
            Case ws.Name Like "INV*"
                FirstSel = ws.Name
            Case ws.Name Like "Reim*"
                SecondSel = ws.Name
            Case ws.Name Like "DN*"
                MsgBox ws.Name
            Case Else
                MsgBox ws.Name
        End Select
    Next ws
 
Upvote 0

Vanda_a

Well-known Member
Joined
Oct 29, 2012
Messages
934
It appears you want String variables, in which case you'll need:
Rich (BB code):
Dim FirstSel As String
Dim SecondSel As String
Dim ws As Worksheet

    For Each ws In Worksheets
        Select Case True
            Case ws.Name Like "INV*"
                FirstSel = ws.Name
            Case ws.Name Like "Reim*"
                SecondSel = ws.Name
            Case ws.Name Like "DN*"
                MsgBox ws.Name
            Case Else
                MsgBox ws.Name
        End Select
    Next ws

Yeah. Thanks so much.

Rich (BB code):
    If FirstSel Like "INV*" And SecondSel Like "Reim*" Then ''''''''''''''''''''''''''''If FirstSel = "INV*" and SecondSel = "Reim*" Then
        Worksheets(FirstSel).Select
        Worksheets(SecondSel).Select
        Selection.SaveAs Filename:=Path & FirstSel & ". xlsx" ''''''''''''''''''''''Debug here
    End If
Please kindly help to adjust
 
Upvote 0

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
40,681
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
  2. MacOS
ADVERTISEMENT
Try:
Code:
    If FirstSel Like "INV*" And SecondSel Like "Reim*" Then ''''''''''''''''''''''''''''If FirstSel = "INV*" and SecondSel = "Reim*" Then
        Worksheets(FirstSel).Select
        Worksheets(SecondSel).Select False
        Activewindow.Selectedsheets.COpy
activeworkbook.SaveAs Filename:=Path & FirstSel & ". xlsx" ''''''''''''''''''''''Debug here
    End If
 
Upvote 0

Vanda_a

Well-known Member
Joined
Oct 29, 2012
Messages
934
Try:
Rich (BB code):
    If FirstSel Like "INV*" And SecondSel Like "Reim*" Then ''''''''''''''''''''''''''''If FirstSel = "INV*" and SecondSel = "Reim*" Then
        Worksheets(FirstSel).Select
        Worksheets(SecondSel).Select False
        Activewindow.Selectedsheets.Copy
activeworkbook.SaveAs Filename:=Path & FirstSel & ". xlsx" ''''''''''''''''''''''Debug here
    End If

Yeah. It works so well. Thank you very much

May I have some explanation please?
Rich (BB code):
Worksheets(SecondSel).Select False '''''''''''''False means none select?
Activewindow.Selectedsheets.Copy ''''''''''''''''Ís it necessary?
Activeworkbook.SaveAs Filename:=Path & FirstSel & ".xlsx" '''''''''Can use ActiveSheets as a substitute?
 
Last edited:
Upvote 0

Vanda_a

Well-known Member
Joined
Oct 29, 2012
Messages
934
One more please
Rich (BB code):
If FirstSel Like "INV*" And SecondSel Like "Reim*" Then  is the same as below one 
If SecondSel Like "Reim*" And FirstSel Like "INV*" Then

How to make it not the same?
Ex: Sheet1(INV123) and Sheet2(Reim123). The code executes ''''''''''''Í would like the code to execute only this part
Sheet1(Reim123) and Sheet2(INV123). The code still executes.... @_@
 
Upvote 0

Forum statistics

Threads
1,195,631
Messages
6,010,783
Members
441,569
Latest member
PeggyLee

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top