ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Why won't this work? (https://www.excelbanter.com/excel-programming/299649-why-wont-work.html)

Steph[_3_]

Why won't this work?
 
Hello. I'm sure this a pretty dumb question, but any ideas why this
won't work??

Set clrarray = Sheets(Array("Forecast", "LMU", "Kit", "SMLC", "WLG",
"SMLC Cab", "Serv Cab", "Ntwk Kit", _
"TDAX", "EMS", "SCOUT", "Dir Coup"))

ActiveWorkbook.Worksheets(clrarray).Range("A5:EC50 0").ClearContents

Thanks!

Bob Phillips[_6_]

Why won't this work?
 
TRy

For Each sh In ActiveWorkbook.Worksheets(clrarray)
sh.Range("A5:EC500").ClearContents
Next sh


--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)

"Steph" wrote in message
om...
Hello. I'm sure this a pretty dumb question, but any ideas why this
won't work??

Set clrarray = Sheets(Array("Forecast", "LMU", "Kit", "SMLC", "WLG",
"SMLC Cab", "Serv Cab", "Ntwk Kit", _
"TDAX", "EMS", "SCOUT", "Dir Coup"))

ActiveWorkbook.Worksheets(clrarray).Range("A5:EC50 0").ClearContents

Thanks!




Frank Kabel

Why won't this work?
 
Hi
try something like the following:
Sub foo()
Dim sh_array As Variant
Dim wks As Worksheet
Dim i
sh_array = Array("sheet1", "sheet2")
For i = 0 To UBound(sh_array)
Set wks = Worksheets(CStr(sh_array(i)))
wks.range("A5:EC500").clearcontents
'MsgBox wks.Name
Next
End Sub

--
Regards
Frank Kabel
Frankfurt, Germany

"Steph" schrieb im Newsbeitrag
om...
Hello. I'm sure this a pretty dumb question, but any ideas why this
won't work??

Set clrarray = Sheets(Array("Forecast", "LMU", "Kit", "SMLC", "WLG",
"SMLC Cab", "Serv Cab", "Ntwk Kit", _
"TDAX", "EMS", "SCOUT", "Dir Coup"))

ActiveWorkbook.Worksheets(clrarray).Range("A5:EC50 0").ClearContents

Thanks!



Tushar Mehta

Why won't this work?
 
There are two problems with the code.

First, you are setting ClrArray to a collection of sheets. Then, you
are trying to use this collection of *objects* as indices to the
Worksheets collection. That cannot work because the Worksheets
collection can be indexed only by integers or strings (and not
objects).

You should use

'Set ClrArray = ActiveWorkbook.Sheets(Array("Forecast", _
"LMU", "Kit", "SMLC", "WLG", _
"SMLC Cab", "Serv Cab", "Ntwk Kit", _
"TDAX", "EMS", "SCOUT", "Dir Coup"))
ClrArray = Array("Sheet1", "Sheet2", "Sheet3")
ActiveWorkbook.Sheets(ClrArray).Select

In the above example, your statment has been commented out. My test
used just three worksheets. Note that in my case, clrarray contains
*string* values not worksheet objects.

However, that still leaves the 2nd problem. XL's handling of 3D ranges
is something I would describe as a layer slapped on over the existing
object model. Since it doesn't quite fit in with the OM, you have to
rely on the 'selection' to make things happen. The code below works.
Note that, once again, the correct clrarray statement for you exists
only as a comment.

Option Explicit

Option Explicit

Sub testIt()
Dim ClrArray
'ClrArray = Array("Forecast", _
"LMU", "Kit", "SMLC", "WLG", _
"SMLC Cab", "Serv Cab", "Ntwk Kit", _
"TDAX", "EMS", "SCOUT", "Dir Coup")
ClrArray = Array("Sheet1", "Sheet2", "Sheet3")
ActiveWorkbook.Sheets(ClrArray).Select
Sheets(ClrArray(LBound(ClrArray))).Activate
Range("A5:EC500").Select
Selection.ClearContents
End Sub



--
Regards,

Tushar Mehta
www.tushar-mehta.com
Excel, PowerPoint, and VBA add-ins, tutorials
Custom MS Office productivity solutions

In article ,
says...
Hello. I'm sure this a pretty dumb question, but any ideas why this
won't work??

Set clrarray = Sheets(Array("Forecast", "LMU", "Kit", "SMLC", "WLG",
"SMLC Cab", "Serv Cab", "Ntwk Kit", _
"TDAX", "EMS", "SCOUT", "Dir Coup"))

ActiveWorkbook.Worksheets(clrarray).Range("A5:EC50 0").ClearContents

Thanks!



All times are GMT +1. The time now is 12:24 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com