Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Create an array inside a For...Next loop

I will be looping through a dynamic range of cells, and when my condition is
met I want to add that value to an array, and then empty the contents of the
array onto a different spreadsheet. As a simple example:

'Adding item to array
For each cel in Range("A1:A100")
If cell.value = "X" Then
add cell value to my array
end
Next cell

'Writing data to new spreadsheet
For each cell in Range("B1:B100")
add array item to current cell
Next cell

Does anyone have any suggestions? Thanks in a advance!
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,452
Default Create an array inside a For...Next loop

Sub test()

Dim r As Long
Dim arr(1 To 100, 1 To 1) As String

For r = 1 To 100
If Cells(r, 1) = "X" Then
arr(r, 1) = "found"
End If
Next

Workbooks.Open Filename:="C:\Test.xls"

Sheets("Sheet2").Select

Range(Cells(2), Cells(100, 2)) = arr

End Sub


RBS


"Eric Winegarner" wrote in
message ...
I will be looping through a dynamic range of cells, and when my condition
is
met I want to add that value to an array, and then empty the contents of
the
array onto a different spreadsheet. As a simple example:

'Adding item to array
For each cel in Range("A1:A100")
If cell.value = "X" Then
add cell value to my array
end
Next cell

'Writing data to new spreadsheet
For each cell in Range("B1:B100")
add array item to current cell
Next cell

Does anyone have any suggestions? Thanks in a advance!


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,302
Default Create an array inside a For...Next loop

Hi Eric,

Tyr something like:

'==============
Public Sub DeleteRange()

Dim Rng As Range
Dim rCell As Range
Dim copyRng As Range
Dim WB As Workbook
Dim SH As Worksheet
Dim destSH As Worksheet
Dim destRng As Range
Dim CalcMode As Long

Set WB = ActiveWorkbook '<<======== CHANGE
Set SH = WB.Sheets("Sheet1") ' <<======= CHANGE
Set Rng = SH.Range("A1:A100") '<<======= CHANGE
Set destSH = WB.Sheets("Sheet2") '<<======= CHANGE
Set destRng = destSH.Range("B2") '<<======= CHANGE

With Application
CalcMode = .Calculation
.Calculation = xlCalculationManual
.ScreenUpdating = False
End With

For Each rCell In Rng.Cells
If rCell.Value = "X" Then
If copyRng Is Nothing Then
Set copyRng = rCell
Else
Set copyRng = Union(rCell, copyRng)
End If
End If
Next rCell

If Not copyRng Is Nothing Then
copyRng.Copy Destination:=destRng
Else
'nothing found, do nothing
End If

With Application
.Calculation = CalcMode
.ScreenUpdating = True
End With

End Sub
'==============

---
Regards,
Norman



"Eric Winegarner" wrote in
message ...
I will be looping through a dynamic range of cells, and when my condition
is
met I want to add that value to an array, and then empty the contents of
the
array onto a different spreadsheet. As a simple example:

'Adding item to array
For each cel in Range("A1:A100")
If cell.value = "X" Then
add cell value to my array
end
Next cell

'Writing data to new spreadsheet
For each cell in Range("B1:B100")
add array item to current cell
Next cell

Does anyone have any suggestions? Thanks in a advance!



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,302
Default Create an array inside a For...Next loop

Hi Eric,

Please ignore my response.

I mis-read your question!

---
Regards,
Norman



"Norman Jones" wrote in message
...
Hi Eric,

Tyr something like:

'==============
Public Sub DeleteRange()

Dim Rng As Range
Dim rCell As Range
Dim copyRng As Range
Dim WB As Workbook
Dim SH As Worksheet
Dim destSH As Worksheet
Dim destRng As Range
Dim CalcMode As Long

Set WB = ActiveWorkbook '<<======== CHANGE
Set SH = WB.Sheets("Sheet1") ' <<======= CHANGE
Set Rng = SH.Range("A1:A100") '<<======= CHANGE
Set destSH = WB.Sheets("Sheet2") '<<======= CHANGE
Set destRng = destSH.Range("B2") '<<======= CHANGE

With Application
CalcMode = .Calculation
.Calculation = xlCalculationManual
.ScreenUpdating = False
End With

For Each rCell In Rng.Cells
If rCell.Value = "X" Then
If copyRng Is Nothing Then
Set copyRng = rCell
Else
Set copyRng = Union(rCell, copyRng)
End If
End If
Next rCell

If Not copyRng Is Nothing Then
copyRng.Copy Destination:=destRng
Else
'nothing found, do nothing
End If

With Application
.Calculation = CalcMode
.ScreenUpdating = True
End With

End Sub
'==============

---
Regards,
Norman



"Eric Winegarner" wrote in
message ...
I will be looping through a dynamic range of cells, and when my condition
is
met I want to add that value to an array, and then empty the contents of
the
array onto a different spreadsheet. As a simple example:

'Adding item to array
For each cel in Range("A1:A100")
If cell.value = "X" Then
add cell value to my array
end
Next cell

'Writing data to new spreadsheet
For each cell in Range("B1:B100")
add array item to current cell
Next cell

Does anyone have any suggestions? Thanks in a advance!





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
use time functions inside if loop saedeepu Excel Worksheet Functions 2 September 17th 09 12:55 PM
Using Loop to create Array davidm Excel Programming 4 June 15th 05 11:41 AM
declaring active cell inside a for loop and offsetting from it l1075[_4_] Excel Programming 2 May 5th 04 08:40 PM
Check if a String is inside an Array Dave Peterson[_3_] Excel Programming 3 September 3rd 03 08:41 PM
Check if a String is inside an Array Alan Beban[_3_] Excel Programming 0 September 3rd 03 07:44 PM


All times are GMT +1. The time now is 07:59 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"