PC Review


Reply
Thread Tools Rate Thread

Activate Sheet switching

 
 
Imran J Khan
Guest
Posts: n/a
 
      21st Jul 2008
I need to be able to activate sheets. I have tried sheets.sheet3.activate and
worksheets("sheet3").activate in a couple of places each in the following
code and it does not work. I need to activate alternately between Sheet2 and
Sheet3 to insert data collected in a vb userform. But if I the sheet where
the data is tobe inserted is not on top, I get an error. If I move the sheet
to the top, the macro runs fine.

Private Sub cmdEnter_Click()

Dim LastRow As Object

Set LastRow = Sheet2.Range("a65536").End(xlUp)
LastRow.Offset(-1, 0).Select
Selection.EntireRow.Insert
ActiveCell.Offset(0, 0).Value = Date
ActiveCell.Offset(0, 1).Value = "Buy"
ActiveCell.Offset(0, 2).Value = TextBox1.Text
ActiveCell.Offset(0, 3).Value = TextBox2.Text
ActiveCell.Offset(0, 4).Value = TextBox3.Text
ActiveCell.Offset(0, 5).Value = -TextBox1.Text * TextBox3.Text
ActiveCell.Offset(0, 7).Value = -TextBox1.Text * TextBox3.Text

Set LastRow = Sheet3.Range("PrfrShrs").End(xlDown)
LastRow.Offset(1, 0).Select
Selection.EntireRow.Insert
ActiveCell.Offset(0, 0).Value = Date
ActiveCell.Offset(0, 1).Value = "Buy"
ActiveCell.Offset(0, 2).Value = TextBox1.Text
ActiveCell.Offset(0, 3).Value = TextBox2.Text
ActiveCell.Offset(0, 4).Value = TextBox3.Text
ActiveCell.Offset(0, 5).Value = -TextBox1.Text * TextBox3.Text
ActiveCell.Offset(0, 7).Value = -TextBox1.Text * TextBox3.Text


Unload Me
End Sub

Imran
 
Reply With Quote
 
 
 
 
Dave Peterson
Guest
Posts: n/a
 
      21st Jul 2008
I'm not sure if this is what you want--do you want to insert a new line directly
above the last row with data?

This avoids the .selects and the selection. and the activecell stuff:

Option Explicit
Private Sub cmdEnter_Click()

Dim LastCell As Range

With Sheet2
Set LastCell = .Cells(.Rows.Count, "A").End(xlUp)
LastCell.EntireRow.Insert
With LastCell.Offset(-1, 0)
'and populate the data
.Offset(0, 0).Value = Date
.Offset(0, 1).Value = "Buy"
.Offset(0, 2).Value = TextBox1.Text
.Offset(0, 3).Value = TextBox2.Text
.Offset(0, 4).Value = TextBox3.Text
.Offset(0, 5).Value = -TextBox1.Text * TextBox3.Text
.Offset(0, 7).Value = -TextBox1.Text * TextBox3.Text
End With
End With

With Sheet3
Set LastCell = .Range("PrfrShrs").End(xlDown)
LastCell.EntireRow.Insert
'come down a row
With LastCell.Offset(-1, 0)
'and populate the data
.Offset(0, 0).Value = Date
.Offset(0, 1).Value = "Buy"
.Offset(0, 2).Value = TextBox1.Text
.Offset(0, 3).Value = TextBox2.Text
.Offset(0, 4).Value = TextBox3.Text
.Offset(0, 5).Value = -TextBox1.Text * TextBox3.Text
.Offset(0, 7).Value = -TextBox1.Text * TextBox3.Text
End With
End With

Unload Me
End Sub

But I'm not sure this code is putting things where you want them. Test it
against a copy of your worksheets.



Imran J Khan wrote:
>
> I need to be able to activate sheets. I have tried sheets.sheet3.activate and
> worksheets("sheet3").activate in a couple of places each in the following
> code and it does not work. I need to activate alternately between Sheet2 and
> Sheet3 to insert data collected in a vb userform. But if I the sheet where
> the data is tobe inserted is not on top, I get an error. If I move the sheet
> to the top, the macro runs fine.
>
> Private Sub cmdEnter_Click()
>
> Dim LastRow As Object
>
> Set LastRow = Sheet2.Range("a65536").End(xlUp)
> LastRow.Offset(-1, 0).Select
> Selection.EntireRow.Insert
> ActiveCell.Offset(0, 0).Value = Date
> ActiveCell.Offset(0, 1).Value = "Buy"
> ActiveCell.Offset(0, 2).Value = TextBox1.Text
> ActiveCell.Offset(0, 3).Value = TextBox2.Text
> ActiveCell.Offset(0, 4).Value = TextBox3.Text
> ActiveCell.Offset(0, 5).Value = -TextBox1.Text * TextBox3.Text
> ActiveCell.Offset(0, 7).Value = -TextBox1.Text * TextBox3.Text
>
> Set LastRow = Sheet3.Range("PrfrShrs").End(xlDown)
> LastRow.Offset(1, 0).Select
> Selection.EntireRow.Insert
> ActiveCell.Offset(0, 0).Value = Date
> ActiveCell.Offset(0, 1).Value = "Buy"
> ActiveCell.Offset(0, 2).Value = TextBox1.Text
> ActiveCell.Offset(0, 3).Value = TextBox2.Text
> ActiveCell.Offset(0, 4).Value = TextBox3.Text
> ActiveCell.Offset(0, 5).Value = -TextBox1.Text * TextBox3.Text
> ActiveCell.Offset(0, 7).Value = -TextBox1.Text * TextBox3.Text
>
>
> Unload Me
> End Sub
>
> Imran


--

Dave Peterson
 
Reply With Quote
 
Imran J Khan
Guest
Posts: n/a
 
      21st Jul 2008
Thanks Dave, with a little modification, it worked. And yes, i did want to
insert a new line directly above the last row with data.


"Dave Peterson" wrote:

> I'm not sure if this is what you want--do you want to insert a new line directly
> above the last row with data?
>
> This avoids the .selects and the selection. and the activecell stuff:
>
> Option Explicit
> Private Sub cmdEnter_Click()
>
> Dim LastCell As Range
>
> With Sheet2
> Set LastCell = .Cells(.Rows.Count, "A").End(xlUp)
> LastCell.EntireRow.Insert
> With LastCell.Offset(-1, 0)
> 'and populate the data
> .Offset(0, 0).Value = Date
> .Offset(0, 1).Value = "Buy"
> .Offset(0, 2).Value = TextBox1.Text
> .Offset(0, 3).Value = TextBox2.Text
> .Offset(0, 4).Value = TextBox3.Text
> .Offset(0, 5).Value = -TextBox1.Text * TextBox3.Text
> .Offset(0, 7).Value = -TextBox1.Text * TextBox3.Text
> End With
> End With
>
> With Sheet3
> Set LastCell = .Range("PrfrShrs").End(xlDown)
> LastCell.EntireRow.Insert
> 'come down a row
> With LastCell.Offset(-1, 0)
> 'and populate the data
> .Offset(0, 0).Value = Date
> .Offset(0, 1).Value = "Buy"
> .Offset(0, 2).Value = TextBox1.Text
> .Offset(0, 3).Value = TextBox2.Text
> .Offset(0, 4).Value = TextBox3.Text
> .Offset(0, 5).Value = -TextBox1.Text * TextBox3.Text
> .Offset(0, 7).Value = -TextBox1.Text * TextBox3.Text
> End With
> End With
>
> Unload Me
> End Sub
>
> But I'm not sure this code is putting things where you want them. Test it
> against a copy of your worksheets.
>
>
>
> Imran J Khan wrote:
> >
> > I need to be able to activate sheets. I have tried sheets.sheet3.activate and
> > worksheets("sheet3").activate in a couple of places each in the following
> > code and it does not work. I need to activate alternately between Sheet2 and
> > Sheet3 to insert data collected in a vb userform. But if I the sheet where
> > the data is tobe inserted is not on top, I get an error. If I move the sheet
> > to the top, the macro runs fine.
> >
> > Private Sub cmdEnter_Click()
> >
> > Dim LastRow As Object
> >
> > Set LastRow = Sheet2.Range("a65536").End(xlUp)
> > LastRow.Offset(-1, 0).Select
> > Selection.EntireRow.Insert
> > ActiveCell.Offset(0, 0).Value = Date
> > ActiveCell.Offset(0, 1).Value = "Buy"
> > ActiveCell.Offset(0, 2).Value = TextBox1.Text
> > ActiveCell.Offset(0, 3).Value = TextBox2.Text
> > ActiveCell.Offset(0, 4).Value = TextBox3.Text
> > ActiveCell.Offset(0, 5).Value = -TextBox1.Text * TextBox3.Text
> > ActiveCell.Offset(0, 7).Value = -TextBox1.Text * TextBox3.Text
> >
> > Set LastRow = Sheet3.Range("PrfrShrs").End(xlDown)
> > LastRow.Offset(1, 0).Select
> > Selection.EntireRow.Insert
> > ActiveCell.Offset(0, 0).Value = Date
> > ActiveCell.Offset(0, 1).Value = "Buy"
> > ActiveCell.Offset(0, 2).Value = TextBox1.Text
> > ActiveCell.Offset(0, 3).Value = TextBox2.Text
> > ActiveCell.Offset(0, 4).Value = TextBox3.Text
> > ActiveCell.Offset(0, 5).Value = -TextBox1.Text * TextBox3.Text
> > ActiveCell.Offset(0, 7).Value = -TextBox1.Text * TextBox3.Text
> >
> >
> > Unload Me
> > End Sub
> >
> > Imran

>
> --
>
> Dave Peterson
>

 
Reply With Quote
 
Dave Peterson
Guest
Posts: n/a
 
      21st Jul 2008
Glad it worked.

But it did work without modification for me <vbg>.

Imran J Khan wrote:
>
> Thanks Dave, with a little modification, it worked. And yes, i did want to
> insert a new line directly above the last row with data.
>
> "Dave Peterson" wrote:
>
> > I'm not sure if this is what you want--do you want to insert a new line directly
> > above the last row with data?
> >
> > This avoids the .selects and the selection. and the activecell stuff:
> >
> > Option Explicit
> > Private Sub cmdEnter_Click()
> >
> > Dim LastCell As Range
> >
> > With Sheet2
> > Set LastCell = .Cells(.Rows.Count, "A").End(xlUp)
> > LastCell.EntireRow.Insert
> > With LastCell.Offset(-1, 0)
> > 'and populate the data
> > .Offset(0, 0).Value = Date
> > .Offset(0, 1).Value = "Buy"
> > .Offset(0, 2).Value = TextBox1.Text
> > .Offset(0, 3).Value = TextBox2.Text
> > .Offset(0, 4).Value = TextBox3.Text
> > .Offset(0, 5).Value = -TextBox1.Text * TextBox3.Text
> > .Offset(0, 7).Value = -TextBox1.Text * TextBox3.Text
> > End With
> > End With
> >
> > With Sheet3
> > Set LastCell = .Range("PrfrShrs").End(xlDown)
> > LastCell.EntireRow.Insert
> > 'come down a row
> > With LastCell.Offset(-1, 0)
> > 'and populate the data
> > .Offset(0, 0).Value = Date
> > .Offset(0, 1).Value = "Buy"
> > .Offset(0, 2).Value = TextBox1.Text
> > .Offset(0, 3).Value = TextBox2.Text
> > .Offset(0, 4).Value = TextBox3.Text
> > .Offset(0, 5).Value = -TextBox1.Text * TextBox3.Text
> > .Offset(0, 7).Value = -TextBox1.Text * TextBox3.Text
> > End With
> > End With
> >
> > Unload Me
> > End Sub
> >
> > But I'm not sure this code is putting things where you want them. Test it
> > against a copy of your worksheets.
> >
> >
> >
> > Imran J Khan wrote:
> > >
> > > I need to be able to activate sheets. I have tried sheets.sheet3.activate and
> > > worksheets("sheet3").activate in a couple of places each in the following
> > > code and it does not work. I need to activate alternately between Sheet2 and
> > > Sheet3 to insert data collected in a vb userform. But if I the sheet where
> > > the data is tobe inserted is not on top, I get an error. If I move the sheet
> > > to the top, the macro runs fine.
> > >
> > > Private Sub cmdEnter_Click()
> > >
> > > Dim LastRow As Object
> > >
> > > Set LastRow = Sheet2.Range("a65536").End(xlUp)
> > > LastRow.Offset(-1, 0).Select
> > > Selection.EntireRow.Insert
> > > ActiveCell.Offset(0, 0).Value = Date
> > > ActiveCell.Offset(0, 1).Value = "Buy"
> > > ActiveCell.Offset(0, 2).Value = TextBox1.Text
> > > ActiveCell.Offset(0, 3).Value = TextBox2.Text
> > > ActiveCell.Offset(0, 4).Value = TextBox3.Text
> > > ActiveCell.Offset(0, 5).Value = -TextBox1.Text * TextBox3.Text
> > > ActiveCell.Offset(0, 7).Value = -TextBox1.Text * TextBox3.Text
> > >
> > > Set LastRow = Sheet3.Range("PrfrShrs").End(xlDown)
> > > LastRow.Offset(1, 0).Select
> > > Selection.EntireRow.Insert
> > > ActiveCell.Offset(0, 0).Value = Date
> > > ActiveCell.Offset(0, 1).Value = "Buy"
> > > ActiveCell.Offset(0, 2).Value = TextBox1.Text
> > > ActiveCell.Offset(0, 3).Value = TextBox2.Text
> > > ActiveCell.Offset(0, 4).Value = TextBox3.Text
> > > ActiveCell.Offset(0, 5).Value = -TextBox1.Text * TextBox3.Text
> > > ActiveCell.Offset(0, 7).Value = -TextBox1.Text * TextBox3.Text
> > >
> > >
> > > Unload Me
> > > End Sub
> > >
> > > Imran

> >
> > --
> >
> > Dave Peterson
> >


--

Dave Peterson
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
What is the difference between 'Select' a sheet and 'Activate' a sheet RJQMAN Microsoft Excel Programming 9 30th Oct 2010 05:22 AM
Prevent code in "Sheet Activate" from running when sheet made visible from other macr Simon Lloyd Microsoft Excel Programming 10 21st Jun 2006 09:15 AM
Return to Current Sheet in On (sheet activate) event macro =?Utf-8?B?UGF1bCBNb2xlcw==?= Microsoft Excel Programming 1 27th Mar 2005 03:16 PM
fast user switching: how to activate? luis Windows XP General 4 14th Feb 2005 06:43 AM
Why wouldn't calling the Activate sub for a sheet automatically call that sheet's Worksheet_Activate() sub? Scott Lyon Microsoft Excel Programming 3 19th Aug 2003 03:03 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 09:39 PM.