Page 1 of 1
จะทำการนำข้อมูลที่อยู่ใน array มาใช้ตามที่ต้องการได้ยังไงคะ
Posted: Sun Jan 13, 2013 9:52 am
by tantanz

คือตอนนี้ได้สร้าง array เพื่อเก็บค่า x และ y จาก sheet2แล้ว หากจะนำออกมาใช้เพื่อการคำนวณต่อจะเรียกมาใช้ได้ยังไงคะ
โดยใน sheet3 จะมีตัวเลขที่รันลงมา แล้วนำค่าจาก array มาคำนวณให้อยู่ในเซลล์ C
เช่น
A B C
1 1 =abs(x(1)-x(1))+(y(1)-y(1))
1 2 =abs(x(1)-x(2))+(y(1)-y(2))
1 3 =abs(x(1)-x(3))+(y(1)-y(3))
2 1 =abs(x(2)-x(1))+(y(2)-y(1))
2 2 =abs(x(2)-x(2))+(y(2)-y(2))
2 3 =abs(x(2)-x(3))+(y(2)-y(3))
3 1 =abs(x(3)-x(1))+(y(3)-y(1))
3 2 =abs(x(3)-x(2))+(y(3)-y(2))
3 3 =abs(x(3)-x(3))+(y(3)-y(3))
Re: จะทำการนำข้อมูลที่อยู่ใน array มาใช้ตามที่ต้องการได้ยังไ
Posted: Sun Jan 13, 2013 10:14 am
by snasui

ลองดูตัวอย่าง Code ตามด้านล่างครับ
Code: Select all
Public Sub distance()
Dim i As Integer, j As Integer
Dim rAll As Range, rx As Range
Dim ry As Range, rCal As Range
Dim k As Integer, l As Integer
With Sheets("Sheet2")
Set rAll = .Range("A2", .Range("A" & Rows.Count) _
.End(xlUp))
j = rAll.Rows.Count
End With
Sheets("Sheet3").Cells.Clear
For i = 1 To j
With Sheets("Sheet3")
If .Range("B1") = "" Then
rAll.Copy .Range("B1")
.Range("A1").Resize(j) = i
Else
rAll.Copy .Range("B" & Rows.Count) _
.End(xlUp).Offset(1, 0)
.Range("A" & Rows.Count).End(xlUp) _
.Offset(1, 0).Resize(j) = i
End If
End With
Next i
With Sheets("Sheet3")
Set rx = .Range("A1", .Range("A" & Rows.Count).End(xlUp))
Set ry = rx.Offset(0, 1)
Set rCal = ry.Offset(0, 1)
k = rx.Count
End With
For l = 1 To k
rCal(l) = Abs(rx(l) - ry(l) + (rx(l) - ry(l)))
Next l
End Sub
Re: จะทำการนำข้อมูลที่อยู่ใน array มาใช้ตามที่ต้องการได้ยังไ
Posted: Sun Jan 13, 2013 10:26 am
by tantanz
อาจจะอธิบายไม่ละเอียดมาก ^^"
คือเวลาคำนวณจะดึงค่ามาใช้ค่ะ
sheet2
เครื่อง x y
1 3 4
2 6 12
3 12 4
sheet3 (เซลล์ c) ได้ดังนี้
=abs(3-3)+(4-4) '=abs(x(1)-x(1))+(y(1)-y(1)) คือ x(1) มีค่าเท่ากับ 3 y(1) มีค่าเท่ากับ 4 เป็นต้นค่ะ
=abs(3-6)+(4-9)
=abs(3-12)+(4-4)
Re: จะทำการนำข้อมูลที่อยู่ใน array มาใช้ตามที่ต้องการได้ยังไ
Posted: Sun Jan 13, 2013 10:56 am
by snasui

ลองดูตัวอย่าง Code ตามด้านล่าง ปกติควรบอกผลลัพธ์ที่ต้องการมาด้วยจะเข้าใจมากขึ้นและสามารถเปรียบเทียบได้ครับ
Code: Select all
Option Explicit
Public Sub distance()
Dim i As Integer, j As Integer
Dim rAll As Range, rx As Range
Dim ry As Range, rCal As Range
Dim k As Integer, l As Integer
With Sheets("Sheet2")
Set rAll = .Range("A2", .Range("A" & Rows.Count) _
.End(xlUp))
j = rAll.Rows.Count
End With
Sheets("Sheet3").Cells.Clear
For i = 1 To j
With Sheets("Sheet3")
If .Range("B1") = "" Then
rAll.Copy .Range("B1")
.Range("A1").Resize(j) = i
Else
rAll.Copy .Range("B" & Rows.Count) _
.End(xlUp).Offset(1, 0)
.Range("A" & Rows.Count).End(xlUp) _
.Offset(1, 0).Resize(j) = i
End If
End With
Next i
With Sheets("Sheet2")
Set rx = .Range("B2", .Range("B" & Rows.Count).End(xlUp))
Set ry = rx.Offset(0, 1)
End With
With Sheets("Sheet3")
Set rCal = .Range("B1", .Range("B" & Rows.Count).End(xlUp)).Offset(0, 1)
k = rCal.Offset(0, -1).Count
End With
For l = 1 To k
rCal(l) = Abs(rx(rCal(l).Offset(0, -2)) - _
rx(rCal(l).Offset(0, -1))) + _
(ry(rCal(l).Offset(0, -2)) - _
ry(rCal(l).Offset(0, -1)))
Next l
End Sub
Re: จะทำการนำข้อมูลที่อยู่ใน array มาใช้ตามที่ต้องการได้ยังไ
Posted: Sun Jan 13, 2013 11:05 am
by tantanz
ได้ผลลัพธ์ตามที่ต้องการแล้วค่ะ ขอบคุณมาก
และขออภัยที่ไม่ได้บอกลายละเอียดให้ครบถ้วนค่ะ