Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 852
Default Array code troubles

With the three sheets in the array, find the text "DEPR-GENERATORS" in columb B and with the next five rows below "DEPR-GENERATORS", preceed the text in those cells with the text "DEPR".

This code finds the "DEPR-GENERATORS" in columb B and preceeds the five rows each with 3 "DEPR"'s and only on Sheet1.

A sample row before and after code runs:

-COMPUTER EQUIP

and after:

DEPRDEPRDEPR-COMPUTER EQUIP

And only runs on sheet 1.

Thanks.
Howard

Sub AFindIt()

Dim i As Long, ii As Long
Dim MyArr As Variant

MyArr = Array("Sheet1", "Sheet2", "Sheet3")

Application.ScreenUpdating = False

For i = LBound(MyArr) To UBound(MyArr)

With MyArr(i)

' "DEPR-GENERATORS" will be in Column B1:Bn
Cells.Find(What:="DEPR-GENERATORS", After:=ActiveCell, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext).Activate

' Put DEPR in front of the text in the next 5 rows below found text "DEPR-GENERATORS"
For ii = 1 To 5
ActiveCell.Offset(ii) = "DEPR" & Trim(ActiveCell.Offset(ii))
Next ' ii

End With 'MyArr(i)
Next 'i
Application.ScreenUpdating = True
End Sub
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,872
Default Array code troubles

Hi Howard,

Am Sat, 9 Aug 2014 03:50:22 -0700 (PDT) schrieb L. Howard:

With MyArr(i)


an array is not an object so you cannot write With MyArr(i)

Try it this way:

Sub AFindIt()

Dim i As Long, ii As Long
Dim MyArr As Variant
Dim c As Range

MyArr = Array("Sheet1", "Sheet2", "Sheet3")

Application.ScreenUpdating = False

For i = LBound(MyArr) To UBound(MyArr)
With Sheets(MyArr(i))
' "DEPR-GENERATORS" will be in Column B1:Bn
Set c = .Range("B:B").Find(What:="DEPR-GENERATORS",
After:=.Range("B1"), _
LookAt:=xlWhole, SearchOrder:=xlByRows,
SearchDirection:=xlNext)

' Put DEPR in front of the text in the next 5 rows below found
text "DEPR-GENERATORS"
For ii = 1 To 5
c.Offset(ii) = "DEPR" & Trim(c.Offset(ii))
Next ' ii
End With

Next 'i
Application.ScreenUpdating = True
End Sub


Regards
Claus B.
--
Vista Ultimate / Windows7
Office 2007 Ultimate / 2010 Professional
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,872
Default Array code troubles

Hi Howard,

Am Sat, 9 Aug 2014 13:19:11 +0200 schrieb Claus Busch:

if DEPR-GENERATORS occurs more than once in the column you have to use
FindNext:

Sub AFindIt()

Dim i As Long, ii As Long
Dim MyArr As Variant
Dim c As Range
Dim FirstAddress As String

MyArr = Array("Sheet1", "Sheet2", "Sheet3")

Application.ScreenUpdating = False

For i = LBound(MyArr) To UBound(MyArr)
With Sheets(MyArr(i))
' "DEPR-GENERATORS" will be in Column B1:Bn
Set c = .Range("B:B").Find(What:="DEPR-GENERATORS",
After:=.Range("B1"), _
LookAt:=xlWhole, SearchOrder:=xlByRows,
SearchDirection:=xlNext)
If Not c Is Nothing Then
FirstAddress = c.Address
Do
' Put DEPR in front of the text in the next 5 rows below found
text "DEPR-GENERATORS"
For ii = 1 To 5
c.Offset(ii) = "DEPR" & Trim(c.Offset(ii))
Next ' ii
Set c = .Range("B:B").FindNext(c)
Loop While Not c Is Nothing And c.Address < FirstAddress
End If
End With
Next 'i
Application.ScreenUpdating = True
End Sub


Regards
Claus B.
--
Vista Ultimate / Windows7
Office 2007 Ultimate / 2010 Professional
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 852
Default Array code troubles



I was sorta close, but no cigar.

Works very well.

Thanks much, Claus.

Regards,
Howard
  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,514
Default Array code troubles

For clarity...

[A]
MyArr contains sheetnames: The With block requires a fully qualified
ref to an object...

With Sheets(MyArr(i))

...otherwise your code implicitly refs the active sheet.

[b]
Setting a ref to an object requires its properties be ref'd via a
'dot'...

With Sheets(MyArr(i))
.Cells...

-OR-

With Sheets(MyArr(i)).Cells
.Find...

...since what you're actually doing is searching the Cells collection of
each sheet.

--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion


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
modifying code troubles Carlee Excel Programming 1 May 30th 07 05:58 PM
COM Add-in Troubles Howard Excel Discussion (Misc queries) 0 May 22nd 07 01:59 PM
IF troubles JG Excel Worksheet Functions 6 December 24th 06 04:58 AM
Code Troubles merritts[_13_] Excel Programming 4 July 27th 06 07:17 PM
Troubles opening a CSV file from code Bas van Dijk Excel Programming 0 January 19th 04 01:21 PM


All times are GMT +1. The time now is 02:42 PM.

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

About Us

"It's about Microsoft Excel"