Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default VBS Script and ClearContents -- Code Throws An Error

Need VBS script to clear the contents for range all worksheets in workbook.
Must run as VBS script. Having issue with line

"currentWorkSheet .Range( cellRange ).ClearContents"; error generated

excelPath = "<Enter a UNC path to a workbook with 1+ worksheets"
cellRange = "A1:Z100"

WScript.Echo "Reading Data from " & excelPath
WScript.Echo "cellRange = " & cellRange

Set oExcel = CreateObject("excel.application")
oExcel.Workbooks.open( excelPath )

workSheetCount = oExcel.Worksheets.Count

WScript.Echo "We have " & workSheetCount & " worksheets"

For counter = 1 to workSheetCount
WScript.Echo "Reading data from worksheet " & counter & vbCRLF

Set currentWorkSheet = oExcel.ActiveWorkbook.Worksheets( counter )

WScript.Echo "Call to Range method next" & vbCrLf

currentWorkSheet .Range( cellRange ).ClearContents ' <-- Fails

Set currentWorkSheet = Nothing
Next

oExcel.Workbooks(1).Close
oExcel.Quit

Set currentWorkSheet = Nothing
Set oExcel = Nothing



  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 367
Default VBS Script and ClearContents -- Code Throws An Error

I changed it a little:
(Created Exc_WB, and there was a space between
currentWorksheet and .Range)
'-----------------------------------------------------------------------------
excelPath = "D:\test.xls"
cellRange = "A1:Z100"

WScript.Echo "Reading Data from " & excelPath
WScript.Echo "cellRange = " & cellRange

Set oExcel = CreateObject("excel.application")
Set Exc_Wb = oExcel.Workbooks.open( excelPath )

workSheetCount = Exc_Wb.worksheets.Count

WScript.Echo "We have " & workSheetCount & " worksheets"

For counter = 1 to workSheetCount
WScript.Echo "Reading data from worksheet " & counter &
vbCRLF

Set currentWorkSheet = Exc_Wb.Worksheets( counter )

WScript.Echo "Call to Range method next" & vbCrLf

currentWorkSheet.Range( cellRange ).ClearContents ' <-- Fails
Next

Exc_Wb.Close(True)
oExcel.Quit

WScript.Echo "finished"
'-----------------------------------------------------------------------------

test worked

hth

Carlo

On Nov 15, 9:58 am, garibaldi wrote:
Need VBS script to clear the contents for range all worksheets in workbook.
Must run as VBS script. Having issue with line

"currentWorkSheet .Range( cellRange ).ClearContents"; error generated

excelPath = "<Enter a UNC path to a workbook with 1+ worksheets"
cellRange = "A1:Z100"

WScript.Echo "Reading Data from " & excelPath
WScript.Echo "cellRange = " & cellRange

Set oExcel = CreateObject("excel.application")
oExcel.Workbooks.open( excelPath )

workSheetCount = oExcel.Worksheets.Count

WScript.Echo "We have " & workSheetCount & " worksheets"

For counter = 1 to workSheetCount
WScript.Echo "Reading data from worksheet " & counter & vbCRLF

Set currentWorkSheet = oExcel.ActiveWorkbook.Worksheets( counter )

WScript.Echo "Call to Range method next" & vbCrLf

currentWorkSheet .Range( cellRange ).ClearContents ' <-- Fails

Set currentWorkSheet = Nothing
Next

oExcel.Workbooks(1).Close
oExcel.Quit

Set currentWorkSheet = Nothing
Set oExcel = Nothing


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10,593
Default VBS Script and ClearContents -- Code Throws An Error

This worked fine for me

excelPath = "<Enter a UNC path to a workbook with 1+ worksheets"
cellRange = "A1:Z100"

WScript.Echo "Reading Data from " & excelPath
WScript.Echo "cellRange = " & cellRange

Set oExcel = CreateObject("excel.application")

Set oWB = oExcel.Workbooks.Open(excelPath)

workSheetCount = oWB.Worksheets.Count

WScript.Echo "We have " & workSheetCount & " worksheets"

For counter = 1 To workSheetCount
WScript.Echo "Reading data from worksheet " & counter & vbCrLf

Set currentWorkSheet = oWB.Worksheets(counter)

WScript.Echo "Call to Range method next" & vbCrLf

currentWorkSheet.Range(cellRange).ClearContents ' <-- Fails
Next

oWB.Save
oWB.Close
oExcel.Quit
Set currentWorkSheet = Nothing
Set oWB = Nothing
Set oExcel = Nothing


--
---
HTH

Bob

(there's no email, no snail mail, but somewhere should be gmail in my addy)



"garibaldi" wrote in message
...
Need VBS script to clear the contents for range all worksheets in
workbook.
Must run as VBS script. Having issue with line

"currentWorkSheet .Range( cellRange ).ClearContents"; error generated

excelPath = "<Enter a UNC path to a workbook with 1+ worksheets"
cellRange = "A1:Z100"

WScript.Echo "Reading Data from " & excelPath
WScript.Echo "cellRange = " & cellRange

Set oExcel = CreateObject("excel.application")
oExcel.Workbooks.open( excelPath )

workSheetCount = oExcel.Worksheets.Count

WScript.Echo "We have " & workSheetCount & " worksheets"

For counter = 1 to workSheetCount
WScript.Echo "Reading data from worksheet " & counter & vbCRLF

Set currentWorkSheet = oExcel.ActiveWorkbook.Worksheets( counter )

WScript.Echo "Call to Range method next" & vbCrLf

currentWorkSheet .Range( cellRange ).ClearContents ' <-- Fails

Set currentWorkSheet = Nothing
Next

oExcel.Workbooks(1).Close
oExcel.Quit

Set currentWorkSheet = Nothing
Set oExcel = Nothing





  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 118
Default VBS Script and ClearContents -- Code Throws An Error

Hello,

Carlo is right. The error is caused by the space between "currentWorkSheet"
and ".Range". Please remove the space and try it again. It also works well
on my side.

Please let us know if you have any other concerns, or need anything else.

Sincerely,
Jialiang Ge , remove ¡®online.¡¯)
Microsoft Online Community Support

==================================================
For MSDN subscribers whose posts are left unanswered, please check this
document: http://blogs.msdn.com/msdnts/pages/postingAlias.aspx

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications. If you are using Outlook Express/Windows Mail, please make sure
you clear the check box ¡°Tools/Options/Read: Get 300 headers at a time¡±
to see your reply promptly.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
==================================================
This posting is provided ¡°AS IS¡± with no warranties, and confers no
rights.

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default VBS Script and ClearContents -- Code Throws An Error

Thanks to all...

-- G

"garibaldi" wrote:

Need VBS script to clear the contents for range all worksheets in workbook.
Must run as VBS script. Having issue with line

"currentWorkSheet .Range( cellRange ).ClearContents"; error generated

excelPath = "<Enter a UNC path to a workbook with 1+ worksheets"
cellRange = "A1:Z100"

WScript.Echo "Reading Data from " & excelPath
WScript.Echo "cellRange = " & cellRange

Set oExcel = CreateObject("excel.application")
oExcel.Workbooks.open( excelPath )

workSheetCount = oExcel.Worksheets.Count

WScript.Echo "We have " & workSheetCount & " worksheets"

For counter = 1 to workSheetCount
WScript.Echo "Reading data from worksheet " & counter & vbCRLF

Set currentWorkSheet = oExcel.ActiveWorkbook.Worksheets( counter )

WScript.Echo "Call to Range method next" & vbCrLf

currentWorkSheet .Range( cellRange ).ClearContents ' <-- Fails

Set currentWorkSheet = Nothing
Next

oExcel.Workbooks(1).Close
oExcel.Quit

Set currentWorkSheet = Nothing
Set oExcel = Nothing



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Find Throws Error 91 Dave Birley Excel Programming 13 May 17th 07 07:01 PM
workbook.open throws 1004 error [email protected] Excel Programming 3 April 3rd 06 11:37 PM
How do I perform a certain function if VBA throws up an error? Matt[_37_] Excel Programming 2 February 24th 06 03:06 PM
[Q] Save As throws type mismatch error in control's code? Jason Weiss Excel Discussion (Misc queries) 1 July 16th 05 04:21 AM
Format statement no longer throws an error PF[_2_] Excel Programming 6 March 4th 04 02:44 PM


All times are GMT +1. The time now is 12:00 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"