PC Review


Reply
Thread Tools Rate Thread

dim problem when using cell() reference

 
 
aimee209
Guest
Posts: n/a
 
      10th Mar 2008
I am trying to write codes that will add or subtract a number from a previous
number, depending on whether or not certain cells are blank. It's an excel
version of a checkbook: if there's nothing listed in credit, subtract the
debit and if the debit is blank, add the credit to the previous balance.
I'm having trouble with the dim part for the total. I don't know what to
set it as.
I've attached the code I have so far.
Thanks!

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim row As Integer
row = 3
Dim total As Long

Do
Set total = Sheets("Sheet1").Cells(row, F)

If Cells(row, D) = 0 Then
If Cells(row, E) = 0 Then
total.Value = 0
Else
total.Value = Cells(row - 1, F) + Cells(row, E)
End If
Else: total.Value = Cells(row - 1, F) - Cells(row, D)
End If

If row < 100 Then
row = row + 1
Else: End
End If

Loop
End Sub
 
Reply With Quote
 
 
 
 
PCLIVE
Guest
Posts: n/a
 
      10th Mar 2008
I don't think you can use "row" as a variable. Try just "r".

HTH,
Paul

--

"aimee209" <(E-Mail Removed)> wrote in message
news:7FD88EF7-57B0-4681-962C-(E-Mail Removed)...
>I am trying to write codes that will add or subtract a number from a
>previous
> number, depending on whether or not certain cells are blank. It's an
> excel
> version of a checkbook: if there's nothing listed in credit, subtract the
> debit and if the debit is blank, add the credit to the previous balance.
> I'm having trouble with the dim part for the total. I don't know what to
> set it as.
> I've attached the code I have so far.
> Thanks!
>
> Private Sub Worksheet_SelectionChange(ByVal Target As Range)
> Dim row As Integer
> row = 3
> Dim total As Long
>
> Do
> Set total = Sheets("Sheet1").Cells(row, F)
>
> If Cells(row, D) = 0 Then
> If Cells(row, E) = 0 Then
> total.Value = 0
> Else
> total.Value = Cells(row - 1, F) + Cells(row, E)
> End If
> Else: total.Value = Cells(row - 1, F) - Cells(row, D)
> End If
>
> If row < 100 Then
> row = row + 1
> Else: End
> End If
>
> Loop
> End Sub



 
Reply With Quote
 
aimee209
Guest
Posts: n/a
 
      10th Mar 2008
I changed to just "r" and I'm still seeing the same error message that an
object is required.


"PCLIVE" wrote:

> I don't think you can use "row" as a variable. Try just "r".
>
> HTH,
> Paul
>
> --
>
> "aimee209" <(E-Mail Removed)> wrote in message
> news:7FD88EF7-57B0-4681-962C-(E-Mail Removed)...
> >I am trying to write codes that will add or subtract a number from a
> >previous
> > number, depending on whether or not certain cells are blank. It's an
> > excel
> > version of a checkbook: if there's nothing listed in credit, subtract the
> > debit and if the debit is blank, add the credit to the previous balance.
> > I'm having trouble with the dim part for the total. I don't know what to
> > set it as.
> > I've attached the code I have so far.
> > Thanks!
> >
> > Private Sub Worksheet_SelectionChange(ByVal Target As Range)
> > Dim row As Integer
> > row = 3
> > Dim total As Long
> >
> > Do
> > Set total = Sheets("Sheet1").Cells(row, F)
> >
> > If Cells(row, D) = 0 Then
> > If Cells(row, E) = 0 Then
> > total.Value = 0
> > Else
> > total.Value = Cells(row - 1, F) + Cells(row, E)
> > End If
> > Else: total.Value = Cells(row - 1, F) - Cells(row, D)
> > End If
> >
> > If row < 100 Then
> > row = row + 1
> > Else: End
> > End If
> >
> > Loop
> > End Sub

>
>
>

 
Reply With Quote
 
PCLIVE
Guest
Posts: n/a
 
      10th Mar 2008
Aimee,

I may have been mistaken about the row as a variable. It's just not good
practice since Row is already a qualifier.

Anyway, I see a view issues.
When using "Cells([Row],[Column})", the Row and Colunm references should be
numbers or variables that are numbers.
So:
Sheets("Sheet1").Cells(r, F)
Should be:
Sheets("Sheet1").Cells(r, 6)

Also, don't use "Set" for your "total" value.
total = Sheets("Sheet1").Cells(r, 6)

Then when referencing "total" later in the code, remove the ".Value".
So instead of :
total.Value = Cells(row - 1, F) + Cells(row, E)
Use:
total = Cells(r - 1, 6) + Cells(r, 5)

Hope this helps,
Paul




--

"aimee209" <(E-Mail Removed)> wrote in message
news:46FB3889-DBD6-4F2D-B2DF-(E-Mail Removed)...
>I changed to just "r" and I'm still seeing the same error message that an
> object is required.
>
>
> "PCLIVE" wrote:
>
>> I don't think you can use "row" as a variable. Try just "r".
>>
>> HTH,
>> Paul
>>
>> --
>>
>> "aimee209" <(E-Mail Removed)> wrote in message
>> news:7FD88EF7-57B0-4681-962C-(E-Mail Removed)...
>> >I am trying to write codes that will add or subtract a number from a
>> >previous
>> > number, depending on whether or not certain cells are blank. It's an
>> > excel
>> > version of a checkbook: if there's nothing listed in credit, subtract
>> > the
>> > debit and if the debit is blank, add the credit to the previous
>> > balance.
>> > I'm having trouble with the dim part for the total. I don't know what
>> > to
>> > set it as.
>> > I've attached the code I have so far.
>> > Thanks!
>> >
>> > Private Sub Worksheet_SelectionChange(ByVal Target As Range)
>> > Dim row As Integer
>> > row = 3
>> > Dim total As Long
>> >
>> > Do
>> > Set total = Sheets("Sheet1").Cells(row, F)
>> >
>> > If Cells(row, D) = 0 Then
>> > If Cells(row, E) = 0 Then
>> > total.Value = 0
>> > Else
>> > total.Value = Cells(row - 1, F) + Cells(row, E)
>> > End If
>> > Else: total.Value = Cells(row - 1, F) - Cells(row, D)
>> > End If
>> >
>> > If row < 100 Then
>> > row = row + 1
>> > Else: End
>> > End If
>> >
>> > Loop
>> > End Sub

>>
>>
>>



 
Reply With Quote
 
Jim Thomlinson
Guest
Posts: n/a
 
      10th Mar 2008
You are correct in that you have a Dim problem but it is not what you
think... In the line:
Set total = Sheets("Sheet1").Cells(row, F)
F needs to be in Quateation marks otherwise F will be decalred on the fly as
a variable of typw variant.
Set total = Sheets("Sheet1").Cells(row, "F")
To ensure that this does not happen in the future in the VBE select Tools ->
Options -> Editor and check "Require Variable Declarations". This will put
the compiler directive "Option Explicit at the top of any new code modules.
This will mean that you ahve to decalre all varaibles. If you do not declare
a variable (similar to F" then it will give you an error.
Check out this link...

http://www.cpearson.com/excel/variables.htm

Additionally, don't use row as a vairable. Instead you could use something
like
dim lngRow as long
Note that row should be a long as integer is too small to hold 56,536.
Finally Total should probably be a double...
--
HTH...

Jim Thomlinson


"aimee209" wrote:

> I am trying to write codes that will add or subtract a number from a previous
> number, depending on whether or not certain cells are blank. It's an excel
> version of a checkbook: if there's nothing listed in credit, subtract the
> debit and if the debit is blank, add the credit to the previous balance.
> I'm having trouble with the dim part for the total. I don't know what to
> set it as.
> I've attached the code I have so far.
> Thanks!
>
> Private Sub Worksheet_SelectionChange(ByVal Target As Range)
> Dim row As Integer
> row = 3
> Dim total As Long
>
> Do
> Set total = Sheets("Sheet1").Cells(row, F)
>
> If Cells(row, D) = 0 Then
> If Cells(row, E) = 0 Then
> total.Value = 0
> Else
> total.Value = Cells(row - 1, F) + Cells(row, E)
> End If
> Else: total.Value = Cells(row - 1, F) - Cells(row, D)
> End If
>
> If row < 100 Then
> row = row + 1
> Else: End
> End If
>
> Loop
> End Sub

 
Reply With Quote
 
Jim Thomlinson
Guest
Posts: n/a
 
      10th Mar 2008
Oops...
Dim Total as Range
or better yet change Total to something like
Dim rngTotal as range
--
HTH...

Jim Thomlinson


"Jim Thomlinson" wrote:

> You are correct in that you have a Dim problem but it is not what you
> think... In the line:
> Set total = Sheets("Sheet1").Cells(row, F)
> F needs to be in Quateation marks otherwise F will be decalred on the fly as
> a variable of typw variant.
> Set total = Sheets("Sheet1").Cells(row, "F")
> To ensure that this does not happen in the future in the VBE select Tools ->
> Options -> Editor and check "Require Variable Declarations". This will put
> the compiler directive "Option Explicit at the top of any new code modules.
> This will mean that you ahve to decalre all varaibles. If you do not declare
> a variable (similar to F" then it will give you an error.
> Check out this link...
>
> http://www.cpearson.com/excel/variables.htm
>
> Additionally, don't use row as a vairable. Instead you could use something
> like
> dim lngRow as long
> Note that row should be a long as integer is too small to hold 56,536.
> Finally Total should probably be a double...
> --
> HTH...
>
> Jim Thomlinson
>
>
> "aimee209" wrote:
>
> > I am trying to write codes that will add or subtract a number from a previous
> > number, depending on whether or not certain cells are blank. It's an excel
> > version of a checkbook: if there's nothing listed in credit, subtract the
> > debit and if the debit is blank, add the credit to the previous balance.
> > I'm having trouble with the dim part for the total. I don't know what to
> > set it as.
> > I've attached the code I have so far.
> > Thanks!
> >
> > Private Sub Worksheet_SelectionChange(ByVal Target As Range)
> > Dim row As Integer
> > row = 3
> > Dim total As Long
> >
> > Do
> > Set total = Sheets("Sheet1").Cells(row, F)
> >
> > If Cells(row, D) = 0 Then
> > If Cells(row, E) = 0 Then
> > total.Value = 0
> > Else
> > total.Value = Cells(row - 1, F) + Cells(row, E)
> > End If
> > Else: total.Value = Cells(row - 1, F) - Cells(row, D)
> > End If
> >
> > If row < 100 Then
> > row = row + 1
> > Else: End
> > End If
> >
> > Loop
> > End Sub

 
Reply With Quote
 
aimee209
Guest
Posts: n/a
 
      10th Mar 2008
Thanks...that helped


"Jim Thomlinson" wrote:

> Oops...
> Dim Total as Range
> or better yet change Total to something like
> Dim rngTotal as range
> --
> HTH...
>
> Jim Thomlinson
>
>
> "Jim Thomlinson" wrote:
>
> > You are correct in that you have a Dim problem but it is not what you
> > think... In the line:
> > Set total = Sheets("Sheet1").Cells(row, F)
> > F needs to be in Quateation marks otherwise F will be decalred on the fly as
> > a variable of typw variant.
> > Set total = Sheets("Sheet1").Cells(row, "F")
> > To ensure that this does not happen in the future in the VBE select Tools ->
> > Options -> Editor and check "Require Variable Declarations". This will put
> > the compiler directive "Option Explicit at the top of any new code modules.
> > This will mean that you ahve to decalre all varaibles. If you do not declare
> > a variable (similar to F" then it will give you an error.
> > Check out this link...
> >
> > http://www.cpearson.com/excel/variables.htm
> >
> > Additionally, don't use row as a vairable. Instead you could use something
> > like
> > dim lngRow as long
> > Note that row should be a long as integer is too small to hold 56,536.
> > Finally Total should probably be a double...
> > --
> > HTH...
> >
> > Jim Thomlinson
> >
> >
> > "aimee209" wrote:
> >
> > > I am trying to write codes that will add or subtract a number from a previous
> > > number, depending on whether or not certain cells are blank. It's an excel
> > > version of a checkbook: if there's nothing listed in credit, subtract the
> > > debit and if the debit is blank, add the credit to the previous balance.
> > > I'm having trouble with the dim part for the total. I don't know what to
> > > set it as.
> > > I've attached the code I have so far.
> > > Thanks!
> > >
> > > Private Sub Worksheet_SelectionChange(ByVal Target As Range)
> > > Dim row As Integer
> > > row = 3
> > > Dim total As Long
> > >
> > > Do
> > > Set total = Sheets("Sheet1").Cells(row, F)
> > >
> > > If Cells(row, D) = 0 Then
> > > If Cells(row, E) = 0 Then
> > > total.Value = 0
> > > Else
> > > total.Value = Cells(row - 1, F) + Cells(row, E)
> > > End If
> > > Else: total.Value = Cells(row - 1, F) - Cells(row, D)
> > > End If
> > >
> > > If row < 100 Then
> > > row = row + 1
> > > Else: End
> > > End If
> > >
> > > Loop
> > > End Sub

 
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
cell reference problem David Funnell Microsoft Excel Misc 1 24th Jan 2005 03:09 AM
problem with cell reference in =sum(offset(cell reference,x,y,z,a)). Want cell ref to be variable. twister212atop@yahoo.com Microsoft Excel Worksheet Functions 2 11th Dec 2004 12:05 AM
Problem with =sum(offset(cell reference,w,x,y,z). I want cell reference to be variable twister212atop@yahoo.com Microsoft Excel Worksheet Functions 2 11th Dec 2004 12:00 AM
cell reference problem corphealth1607 Microsoft Excel Misc 2 28th Jul 2004 09:37 PM
Convert text string in format of cell reference to a cell reference =?Utf-8?B?RnJhbms=?= Microsoft Excel Misc 1 16th Dec 2003 08:52 AM


Features
 

Advertising
 

Newsgroups
 


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