# 情報基礎 A Lecture 10 Takeshi Tokuyama Tohoku University Graduate School of Information Sciences System Information Sciences Design and Analysis of Information.

## Presentation on theme: "情報基礎 A Lecture 10 Takeshi Tokuyama Tohoku University Graduate School of Information Sciences System Information Sciences Design and Analysis of Information."— Presentation transcript:

Programming VBA Cell Data

2-dimentional Array Integer Array x(9) Integer Array y(5) Integer Array z(5) Integer 2-dimentional Array a(5) x(9) y(9) z(9)

2-dimentional Array Declaration ArrayName(Max row index, Max column index ) Dim a(9,2) As Integer Array nameMaximum index number Data Type 2-dimentional Integer array Array name: a Number of box: 10*3=30

2-dimentional Array Declaration ArrayName(Row index range, Column index range) Dim a(1 to 10,1 to 3) As Integer Array nameIndex RangeData Type 2-dimentional Integer array Array name: a Number of box: 10*3=30

Cell Data Row number starts from 1 cells(row index, column index)

Cell format on Excel Range(“Column Row”) Cell Index ： C2 Range(“C2”) or Cells(2,3) in VBA Range(“C2”) or Cells(2,3) in VBA Cells(Column index, Row index)

Procedure Code begins from “Sub” to end with “End Sub” 1Sub exercise1() 2 ThisWorkbook.Worksheets("Sheet1").Range("A:A").Value = 1 3End Sub 1Sub exercise2() 2 ThisWorkbook.Worksheets("Sheet1").Range("A:A").Interior.ColorIndex=4 3End Sub 1Sub exercise3() 2 ThisWorkbook.Worksheets("Sheet1").Range("A:A").Delete 3End Sub

Things to deal with in Excel –Worksheets, Cells, and more Object Period to connect objects Object Period to connect objects 1 ThisWorkbook. Worksheets("Sheet1"). Range("A:A"). Value = 1 1 ThisWorkbook. Worksheets("Sheet1"). Range("A:A"). Interior.ColorIndex = 4 1 ThisWorkbook. Worksheets("Sheet1"). Range("A:A"). Delete

Operation for object –Delete, Open and more Method Period to connect object and method Method Period to connect object and method 1 ThisWorkbook.Worksheets("Sheet1”).Range("A:A”).Delete

Attributes for object appearance Property Substitution Method Period to connect object and method Method Period to connect object and method 1ThisWorkbook.Worksheets(“Sheet1”).Range(“A:A”).Interior. ColorIndex = 3

Cell Reference –Range –Cells Set cell A10 to 10 –Both procedure below results the same 1Sub Example_Range1() 2 ActiveSheet.Range(“A1”).Value = 10 3End Sub 1Sub Example_Cells1() 2 ActiveSheet.Range(1, 1).Value = 10 3End Sub

Range Property Property to see an arbitrary cell or cell range –Read value –Set value Range(“Cell”) 1Sub Example_Range2() 2 ‘Set a value in cells 3 ActiveSheet.Range(“A1”).Value=10 4 ActiveSheet.Range(“A2”).Value=5 5 ActiveSheet.Range(“B1:B10”).Value=10 6End Sub

Cells Property See an arbitrary cell Cells(RowIndex,ColIndex) 1Sub Example_Cells2() 2 ‘Set a value in cells 3 ActiveSheet.Cells(1,5).Value=25 4 ActiveSheet.Cells(2,5).Value=5 5End Sub

Cells Property Procedure to set row number in cells F1- F100 Property ”.value” is omitted 1Sub Example_Cells3() 2 ‘Procedure to set row number in cells F1-F100 3 Dim i As Integer 4 5 For i = 1 to 100 6 Cells(i, 6) = i 7 Next i 8End Sub

Sum of Cell A1 and A2 Output the sum of A1 and A2 to A3 with Range Output the sum of A1 and A2 to A4 with Cells 1Sub Example_Range3() 2 Range(“A3”) = Range(“A1”) + Range(“A2”) 3End Sub 1Sub Example_Cells3() 2 Cells(4,1) = Cells(1,1) + Cells(2,1) 3End Sub

Sum of Cells B1 to B10 Output the sum of B1 to B10 to B11 with Range 1Sub Example_Range4() 2 Range(“B11”) = Range(“B1”) + Range(“B2”) + Range(“B3”) + Range(“B4”) + Range(“B5”) + Range(“B6”) + Range(“B7”) + Range(“B8”) + Range(“B9”) + Range(“B10”) 3End Sub

Sum of Cells B1 to B10 Output the sum of B1 to B10 to B11 with Cells 1Sub Example_Cells5() 2 Dim i As Integer 3 Dim sum1 As Integer 4 sum1=0 5 6 For i = 1 to 100 7 sum1 = sum1 + Cells(i, 2) 8 Next i 9 10 Cells(i, 2) = sum1 11End Sub

Download ppt "情報基礎 A Lecture 10 Takeshi Tokuyama Tohoku University Graduate School of Information Sciences System Information Sciences Design and Analysis of Information."

Similar presentations