PC Review


Reply
Thread Tools Rate Thread

assigning values to cells in worksheet

 
 
KAH
Guest
Posts: n/a
 
      26th Jun 2009

I wrote the function below and received the error message 'Object variable or
With block variable not set' for the line:
wsSorting.Cells(idx, 1).Value = myArray(idx)
I think I haven't written this proplery. How do I put a value (number) into
the cell of a worksheet?
I also have a general question about using worksheets in VisualBasic. Are
they all the same or can they be virtual or real? I am using a worksheet to
sort and rank numbers but it may take too long if the worksheet is not
virtual. I also do not want to show the worksheet in Excel. I hope this makes
sense.
Thanks for any help.


Public Function Sorting(myArray() As Double) As Double
'Sorting the permutations and identifying the rank of the observed value
For idx = 1 To UBound(myArray)
wsSorting.Cells(idx, 1).Value = myArray(idx)
wsSorting.Cells(idx, 2).Value = idx
Next idx
wsSorting.Range("A:B").Sort Key1:=wsSorting.Cells(1, 1),
Order1:=xlAscending
For idx = 1 To UBound(myArray)
wsSorting.Cells(idx, 3).Value = idx
Next idx
wsSorting.Range("A:C").Sort Key1:=wsSorting.Cells(1, 2),
Order1:=xlAscending

End Function
 
Reply With Quote
 
 
 
 
Jacob Skaria
Guest
Posts: n/a
 
      26th Jun 2009

Sheets("Sheet1").cells(1,1).value = 5 will work. So your code should also
work.

Is wsSorting referred to a sheet; and when this is used wihtin a function is
this delcared as a global variable..
Dim wsSorting as Worksheet
Set wsSorting = Workbooks("filename.xls").Sheets("sheetname")


If this post helps click Yes
---------------
Jacob Skaria


"KAH" wrote:

> I wrote the function below and received the error message 'Object variable or
> With block variable not set' for the line:
> wsSorting.Cells(idx, 1).Value = myArray(idx)
> I think I haven't written this proplery. How do I put a value (number) into
> the cell of a worksheet?
> I also have a general question about using worksheets in VisualBasic. Are
> they all the same or can they be virtual or real? I am using a worksheet to
> sort and rank numbers but it may take too long if the worksheet is not
> virtual. I also do not want to show the worksheet in Excel. I hope this makes
> sense.
> Thanks for any help.
>
>
> Public Function Sorting(myArray() As Double) As Double
> 'Sorting the permutations and identifying the rank of the observed value
> For idx = 1 To UBound(myArray)
> wsSorting.Cells(idx, 1).Value = myArray(idx)
> wsSorting.Cells(idx, 2).Value = idx
> Next idx
> wsSorting.Range("A:B").Sort Key1:=wsSorting.Cells(1, 1),
> Order1:=xlAscending
> For idx = 1 To UBound(myArray)
> wsSorting.Cells(idx, 3).Value = idx
> Next idx
> wsSorting.Range("A:C").Sort Key1:=wsSorting.Cells(1, 2),
> Order1:=xlAscending
>
> End Function

 
Reply With Quote
 
KAH
Guest
Posts: n/a
 
      26th Jun 2009

It was declared as a worksheet earlier:
Public wsSorting As Worksheet
But now I think I see the problem - I did not set it as anything. Is it
possible to just keep wsSorting as a virtual worksheet where I can just add
values?

"Jacob Skaria" wrote:

> Sheets("Sheet1").cells(1,1).value = 5 will work. So your code should also
> work.
>
> Is wsSorting referred to a sheet; and when this is used wihtin a function is
> this delcared as a global variable..
> Dim wsSorting as Worksheet
> Set wsSorting = Workbooks("filename.xls").Sheets("sheetname")
>
>
> If this post helps click Yes
> ---------------
> Jacob Skaria
>
>
> "KAH" wrote:
>
> > I wrote the function below and received the error message 'Object variable or
> > With block variable not set' for the line:
> > wsSorting.Cells(idx, 1).Value = myArray(idx)
> > I think I haven't written this proplery. How do I put a value (number) into
> > the cell of a worksheet?
> > I also have a general question about using worksheets in VisualBasic. Are
> > they all the same or can they be virtual or real? I am using a worksheet to
> > sort and rank numbers but it may take too long if the worksheet is not
> > virtual. I also do not want to show the worksheet in Excel. I hope this makes
> > sense.
> > Thanks for any help.
> >
> >
> > Public Function Sorting(myArray() As Double) As Double
> > 'Sorting the permutations and identifying the rank of the observed value
> > For idx = 1 To UBound(myArray)
> > wsSorting.Cells(idx, 1).Value = myArray(idx)
> > wsSorting.Cells(idx, 2).Value = idx
> > Next idx
> > wsSorting.Range("A:B").Sort Key1:=wsSorting.Cells(1, 1),
> > Order1:=xlAscending
> > For idx = 1 To UBound(myArray)
> > wsSorting.Cells(idx, 3).Value = idx
> > Next idx
> > wsSorting.Range("A:C").Sort Key1:=wsSorting.Cells(1, 2),
> > Order1:=xlAscending
> >
> > End Function

 
Reply With Quote
 
Jacob Skaria
Guest
Posts: n/a
 
      27th Jun 2009

Pass wsSorting as an argument...like below



Public Function Sorting(wsTemp As workSheet, myArray() As Double) As Double
'Sorting the permutations and identifying the rank of the observed value
For idx = 1 To UBound(myArray)
wsTemp.Cells(idx, 1).Value = myArray(idx)
wsTemp.Cells(idx, 2).Value = idx
Next idx
wsTemp.Range("A:B").Sort Key1:=wsTemp.Cells(1, 1),
Order1:=xlAscending
For idx = 1 To UBound(myArray)
wsTemp.Cells(idx, 3).Value = idx
Next idx
wsTemp.Range("A:C").Sort Key1:=wsTemp.Cells(1, 2),
Order1:=xlAscending

End Function

If this post helps click Yes
---------------
Jacob Skaria


"KAH" wrote:

> It was declared as a worksheet earlier:
> Public wsSorting As Worksheet
> But now I think I see the problem - I did not set it as anything. Is it
> possible to just keep wsSorting as a virtual worksheet where I can just add
> values?
>
> "Jacob Skaria" wrote:
>
> > Sheets("Sheet1").cells(1,1).value = 5 will work. So your code should also
> > work.
> >
> > Is wsSorting referred to a sheet; and when this is used wihtin a function is
> > this delcared as a global variable..
> > Dim wsSorting as Worksheet
> > Set wsSorting = Workbooks("filename.xls").Sheets("sheetname")
> >
> >
> > If this post helps click Yes
> > ---------------
> > Jacob Skaria
> >
> >
> > "KAH" wrote:
> >
> > > I wrote the function below and received the error message 'Object variable or
> > > With block variable not set' for the line:
> > > wsSorting.Cells(idx, 1).Value = myArray(idx)
> > > I think I haven't written this proplery. How do I put a value (number) into
> > > the cell of a worksheet?
> > > I also have a general question about using worksheets in VisualBasic. Are
> > > they all the same or can they be virtual or real? I am using a worksheet to
> > > sort and rank numbers but it may take too long if the worksheet is not
> > > virtual. I also do not want to show the worksheet in Excel. I hope this makes
> > > sense.
> > > Thanks for any help.
> > >
> > >
> > > Public Function Sorting(myArray() As Double) As Double
> > > 'Sorting the permutations and identifying the rank of the observed value
> > > For idx = 1 To UBound(myArray)
> > > wsSorting.Cells(idx, 1).Value = myArray(idx)
> > > wsSorting.Cells(idx, 2).Value = idx
> > > Next idx
> > > wsSorting.Range("A:B").Sort Key1:=wsSorting.Cells(1, 1),
> > > Order1:=xlAscending
> > > For idx = 1 To UBound(myArray)
> > > wsSorting.Cells(idx, 3).Value = idx
> > > Next idx
> > > wsSorting.Range("A:C").Sort Key1:=wsSorting.Cells(1, 2),
> > > Order1:=xlAscending
> > >
> > > End Function

 
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
Assigning values to adjacent cells from a function MJ_N_CO Microsoft Excel Worksheet Functions 1 6th Jan 2009 01:40 AM
Assigning values from source cells Asif Microsoft Excel Programming 1 12th Aug 2008 08:32 PM
Assigning values to other cells from one cells formula harris.rb@gmail.com Microsoft Excel Misc 1 20th Sep 2007 07:46 PM
Re: Programatically assigning values to table cells Robert M. Franz (RMF) Microsoft Word Document Management 0 18th Jan 2007 02:40 PM
assigning values to cells on itemdatabound gane Microsoft ASP .NET 3 5th Jun 2006 03:03 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:05 PM.