Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Array creating duplicates 2

Status
Not open for further replies.

UnsolvedCoding

Technical User
Jul 20, 2011
424
US
Can anyone tell me why the code below will produce incorrect results?

Code:

For A = 0 To COUNTER

ReDim Preserve IO_Array(D)
IO_Array(D) = Sheets("io" & Temp_Nmbr).Cells(B, C).Value
D = D + 1

ReDim Preserve IO_Array(D)
IO_Array(D) = Sheets("io" & Temp_Nmbr).Cells(B, C + 3).Value
Debug.Print IO_Array(D - 1) & " " & IO_Array(D)

B = B + 1
D = D + 1

Next A

D = 0

B = 1
C = 1

For A = 0 To COUNTER

Sheets("Sheet1").Cells(B, C).Value = IO_Array(D)
Debug.Print IO_Array(D)
D = D + 1
Sheets("Sheet1").Cells(B, C + 1).Value = IO_Array(D)
Debug.Print IO_Array(D)
Debug.Print IO_Array(D - 1) & " " & IO_Array(D)
D = D + 1
B = B + 1

Next A


Original List......Resulting list
V........................V
V........................V
V........................V
V........................V
V........................V
V........................V
LV.......................LV
LV.......................LV
PL.......................LV
DO.......................LV
DO.......................PL
DO.......................DO


 
HI,

Well what are the "correct" results that you expect?

Skip,
[sub]
[glasses]Just traded in my OLD subtlety...
for a NUance![tongue][/sub]
 
The original list on the left side but I think I figured it out. There are hidden cells I am unhiding them now.
 
Here's a few suggestions about your code:
1. You don't need all of the REDIM statements, you know the maximum number of you array is 2*COUNTER. Before the first For/Next loop have your REDIM IO_ARRAY(2*COUNTER)
2. There is no reason to have the B variable. Since the code is already using the A variable in the loop, just modify the value in the CELLS command to be A + 1 instead of B
3. There is no reason to have the D = D + 1 after getting the data from CELL(B,C). Change IO_Array(D) = Sheets("io" & Temp_Nmbr).Cells(B, C + 3).Value to IO_Array(D+1) = Sheets("io" & Temp_Nmbr).Cells(B, C + 3).Value
4. Change the 2nd D = D + 1 to D = D + 2
 
... and wrap all of it in TGML tags
[tt][ignore]
Code:
 ...
[/ignore][/tt]

Have fun.

---- Andy

A bus station is where a bus stops. A train station is where a train stops. On my desk, I have a work station.
 
Could that not be rewritten as something like:

Code:
[blue]Sheets("io" & Temp_Nmbr).Range("A1:A" & Counter + 1).Copy Sheets("Sheet1").Range("A1")
Sheets("io" & Temp_Nmbr).Range("D1:D" & Counter + 1).Copy Sheets("Sheet1").Range("B1")[/blue]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top