PC Review


Reply
Thread Tools Rate Thread

Broken: Set Cells in Range to 0 if blank

 
 
ktoth04
Guest
Posts: n/a
 
      22nd Feb 2008
I can't seem to get this script working properly... Any help would be
appreciated, I am a complete beginner at VBA
Cell Rnage is N13:T27, If the cell is blank, on a save, it should be set to 0.

Sub Workbook_BeforeSave(rwNumber, clNumber)
For rwNumber = 13 To 27
For clNumber = 14 To 20
Application.ScreenUpdating = False
If IsEmpty(Worksheets("Sheet1").Cells(nRow, nCol)) Then
Cells(rwNumber, clNumber).Value = 0
Else: End If
Next clNumber
Next rwNumber
Application.ScreenUpdating = True
End Sub
 
Reply With Quote
 
 
 
 
Rick Rothstein \(MVP - VB\)
Guest
Posts: n/a
 
      22nd Feb 2008
Where did you get that Work_BeforeSave procedure header from? I ask, because
its argument list is not correct. Are you aware that the VBA environment
will provide event headers for you? Go into the VBA editor and double click
the ThisWorkbook entry in the Project Explorer window in the upper left part
of the screen. Doing this will open a code window for the workbook (as
opposed to one of the individual worksheets). Next, click the left-hand
combo box at the top of the code window that appeared and select Workbook
from the drop down list. After you do this, all the possible workbook events
will be available for selection from the right-hand combo box. Select the
BeforeSave entry from this combo box. Place your active code inside this
automatically generated event procedure and see if that works for you.

Rick


"ktoth04" <(E-Mail Removed)> wrote in message
news:531A5B0A-5E37-4BE3-8686-(E-Mail Removed)...
>I can't seem to get this script working properly... Any help would be
> appreciated, I am a complete beginner at VBA
> Cell Rnage is N13:T27, If the cell is blank, on a save, it should be set
> to 0.
>
> Sub Workbook_BeforeSave(rwNumber, clNumber)
> For rwNumber = 13 To 27
> For clNumber = 14 To 20
> Application.ScreenUpdating = False
> If IsEmpty(Worksheets("Sheet1").Cells(nRow, nCol)) Then
> Cells(rwNumber, clNumber).Value = 0
> Else: End If
> Next clNumber
> Next rwNumber
> Application.ScreenUpdating = True
> End Sub


 
Reply With Quote
 
ward376
Guest
Posts: n/a
 
      22nd Feb 2008
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As
Boolean)
Range("n13:t27"). _
Replace What:="", _
Replacement:="0"
End Sub

Cliff Edwards

 
Reply With Quote
 
ktoth04
Guest
Posts: n/a
 
      22nd Feb 2008
I copy and pasted it from some website somewhere >.>
Thanks! I'll try that and see if it works

"Rick Rothstein (MVP - VB)" wrote:

> Where did you get that Work_BeforeSave procedure header from? I ask, because
> its argument list is not correct. Are you aware that the VBA environment
> will provide event headers for you? Go into the VBA editor and double click
> the ThisWorkbook entry in the Project Explorer window in the upper left part
> of the screen. Doing this will open a code window for the workbook (as
> opposed to one of the individual worksheets). Next, click the left-hand
> combo box at the top of the code window that appeared and select Workbook
> from the drop down list. After you do this, all the possible workbook events
> will be available for selection from the right-hand combo box. Select the
> BeforeSave entry from this combo box. Place your active code inside this
> automatically generated event procedure and see if that works for you.
>
> Rick
>
>
> "ktoth04" <(E-Mail Removed)> wrote in message
> news:531A5B0A-5E37-4BE3-8686-(E-Mail Removed)...
> >I can't seem to get this script working properly... Any help would be
> > appreciated, I am a complete beginner at VBA
> > Cell Rnage is N13:T27, If the cell is blank, on a save, it should be set
> > to 0.
> >
> > Sub Workbook_BeforeSave(rwNumber, clNumber)
> > For rwNumber = 13 To 27
> > For clNumber = 14 To 20
> > Application.ScreenUpdating = False
> > If IsEmpty(Worksheets("Sheet1").Cells(nRow, nCol)) Then
> > Cells(rwNumber, clNumber).Value = 0
> > Else: End If
> > Next clNumber
> > Next rwNumber
> > Application.ScreenUpdating = True
> > End Sub

>
>

 
Reply With Quote
 
Mike H
Guest
Posts: n/a
 
      22nd Feb 2008
Hi,

You before save event looks a bit dodgy to me, try this approach

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Application.ScreenUpdating = False
Sheets("Sheet1").Select
Set myrange = Range("N13:T27")
For Each C In myrange
If IsEmpty(C) Then
C.Value = 0
End If
Next
Application.ScreenUpdating = True
End Sub

Mike

"ktoth04" wrote:

> I can't seem to get this script working properly... Any help would be
> appreciated, I am a complete beginner at VBA
> Cell Rnage is N13:T27, If the cell is blank, on a save, it should be set to 0.
>
> Sub Workbook_BeforeSave(rwNumber, clNumber)
> For rwNumber = 13 To 27
> For clNumber = 14 To 20
> Application.ScreenUpdating = False
> If IsEmpty(Worksheets("Sheet1").Cells(nRow, nCol)) Then
> Cells(rwNumber, clNumber).Value = 0
> Else: End If
> Next clNumber
> Next rwNumber
> Application.ScreenUpdating = True
> End Sub

 
Reply With Quote
 
ktoth04
Guest
Posts: n/a
 
      22nd Feb 2008
I got a runtime error 9, subscript out of range on the If IsEmpty line...

"Rick Rothstein (MVP - VB)" wrote:

> Where did you get that Work_BeforeSave procedure header from? I ask, because
> its argument list is not correct. Are you aware that the VBA environment
> will provide event headers for you? Go into the VBA editor and double click
> the ThisWorkbook entry in the Project Explorer window in the upper left part
> of the screen. Doing this will open a code window for the workbook (as
> opposed to one of the individual worksheets). Next, click the left-hand
> combo box at the top of the code window that appeared and select Workbook
> from the drop down list. After you do this, all the possible workbook events
> will be available for selection from the right-hand combo box. Select the
> BeforeSave entry from this combo box. Place your active code inside this
> automatically generated event procedure and see if that works for you.
>
> Rick
>
>
> "ktoth04" <(E-Mail Removed)> wrote in message
> news:531A5B0A-5E37-4BE3-8686-(E-Mail Removed)...
> >I can't seem to get this script working properly... Any help would be
> > appreciated, I am a complete beginner at VBA
> > Cell Rnage is N13:T27, If the cell is blank, on a save, it should be set
> > to 0.
> >
> > Sub Workbook_BeforeSave(rwNumber, clNumber)
> > For rwNumber = 13 To 27
> > For clNumber = 14 To 20
> > Application.ScreenUpdating = False
> > If IsEmpty(Worksheets("Sheet1").Cells(nRow, nCol)) Then
> > Cells(rwNumber, clNumber).Value = 0
> > Else: End If
> > Next clNumber
> > Next rwNumber
> > Application.ScreenUpdating = True
> > End Sub

>
>

 
Reply With Quote
 
ktoth04
Guest
Posts: n/a
 
      22nd Feb 2008
Works like a charm, thanks!

"Mike H" wrote:

> Hi,
>
> You before save event looks a bit dodgy to me, try this approach
>
> Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
> Application.ScreenUpdating = False
> Sheets("Sheet1").Select
> Set myrange = Range("N13:T27")
> For Each C In myrange
> If IsEmpty(C) Then
> C.Value = 0
> End If
> Next
> Application.ScreenUpdating = True
> End Sub
>
> Mike
>
> "ktoth04" wrote:
>
> > I can't seem to get this script working properly... Any help would be
> > appreciated, I am a complete beginner at VBA
> > Cell Rnage is N13:T27, If the cell is blank, on a save, it should be set to 0.
> >
> > Sub Workbook_BeforeSave(rwNumber, clNumber)
> > For rwNumber = 13 To 27
> > For clNumber = 14 To 20
> > Application.ScreenUpdating = False
> > If IsEmpty(Worksheets("Sheet1").Cells(nRow, nCol)) Then
> > Cells(rwNumber, clNumber).Value = 0
> > Else: End If
> > Next clNumber
> > Next rwNumber
> > Application.ScreenUpdating = True
> > End Sub

 
Reply With Quote
 
ktoth04
Guest
Posts: n/a
 
      22nd Feb 2008
Haha, my sheet wasn't named Sheet1... nevermind...

"ktoth04" wrote:

> I got a runtime error 9, subscript out of range on the If IsEmpty line...
>
> "Rick Rothstein (MVP - VB)" wrote:
>
> > Where did you get that Work_BeforeSave procedure header from? I ask, because
> > its argument list is not correct. Are you aware that the VBA environment
> > will provide event headers for you? Go into the VBA editor and double click
> > the ThisWorkbook entry in the Project Explorer window in the upper left part
> > of the screen. Doing this will open a code window for the workbook (as
> > opposed to one of the individual worksheets). Next, click the left-hand
> > combo box at the top of the code window that appeared and select Workbook
> > from the drop down list. After you do this, all the possible workbook events
> > will be available for selection from the right-hand combo box. Select the
> > BeforeSave entry from this combo box. Place your active code inside this
> > automatically generated event procedure and see if that works for you.
> >
> > Rick
> >
> >
> > "ktoth04" <(E-Mail Removed)> wrote in message
> > news:531A5B0A-5E37-4BE3-8686-(E-Mail Removed)...
> > >I can't seem to get this script working properly... Any help would be
> > > appreciated, I am a complete beginner at VBA
> > > Cell Rnage is N13:T27, If the cell is blank, on a save, it should be set
> > > to 0.
> > >
> > > Sub Workbook_BeforeSave(rwNumber, clNumber)
> > > For rwNumber = 13 To 27
> > > For clNumber = 14 To 20
> > > Application.ScreenUpdating = False
> > > If IsEmpty(Worksheets("Sheet1").Cells(nRow, nCol)) Then
> > > Cells(rwNumber, clNumber).Value = 0
> > > Else: End If
> > > Next clNumber
> > > Next rwNumber
> > > Application.ScreenUpdating = True
> > > 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
Range naming cells with blank cells through coding Naveen J V Microsoft Excel Misc 1 27th Mar 2008 01:46 PM
If certain cells not blank, and cells in range are, set values to ktoth04 Microsoft Excel Misc 0 21st Feb 2008 09:01 PM
Maximum Number of Blank Cells between Non Blank Cells in a Range Mal Microsoft Excel Worksheet Functions 5 3rd Nov 2007 08:21 AM
Determine if range has NO Blank Cells without looping through each cell in range Excelenator Microsoft Excel Programming 4 4th Aug 2006 06:30 AM
How do I skip blank cells when copying over a range of cells? =?Utf-8?B?dGF3ZWxscw==?= Microsoft Excel Misc 2 7th Jun 2005 09:36 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 11:38 AM.