peri hokiperihokiclarustologisticsrimashaop.compola taktis menang gates of olympus x1000 perihokipemain perihoki bongkar trik jepep starlight princess x1000perihoki beri panduan lengkap bermain mahjong ways 2 pgsofttips cara penghasil uang pgsoft mahjong ways 2 perihokimahjong wins 3 perihoki memberikan penggali terbesarfenomena gates of olympus buka peluang besar bagi duta76cara dapat pola emas rahasia di duta7 wild west goldtrik menang pgsoft mahjong ways 2 modal receh maxwin duta76duta76 main pgsoft mahjong ways 2 di sela istirahatscatter hitam mahjong wins 3 spin pasti jepe duta76perihoki spin mahjong ways 2 pgsoft scatter hitam maxwinsimbol mahjong wins 3 yang sering muncul di awal perihokitrik gampang menang mahjong winscuan mahjong wins 3 tanpa hentipola spin taruhan kecil mahjong winsstrategi kunci scatter hitam beruntungacor total mahjong wayskemenangan maksimal mahjong wayskemenangan pria pekanbaru mahjong waysmahjong wins3 fenomena baru sumatera baratmenangkan mahjong dengan lincahmenembus batas keberuntungan mahjong waysmenjajal keberuntungan di mahjong wayspola gratis mahjong ways1scatter hitam senjata rahasia mahjong winsteknik spinning menguntungkan untuk scatter hitamanalisis rtp tertinggi mahjong winsfenomena scatter hitam mahjong winsibu rumah tangga dan keberuntungan mahjongkeseruan main mahjong wins3menang mahjong pengguna baru di sosial mediamenghargai setiap momen di mahjong waysserunya main mahjong di warung internetspin slow motion mancing scatterstrategi jitu maxwin mahjong waysstrategi jitu menang mahjong modal kecil jackpot besaranak kampung papua menggemparkan mahjongkeajaiban scatter hitam mahjong wayskeberhasilan joni mahjong winskejaiban mahjong ways di ujung jalanketua mahjong wins3 kehadiran kemenangan dimulai dari balimahjong kehadiran yang menguntungkanmahjong menggila adat88panduan kilat raih 108 jutatips memenangkan mahjongbarista kopi menang 110 juta spin mahjongdakota76 scatter hitam mahjong wins 3gagal cpns menang mahjong wins 3 rp120 jutakisah karyawan minimarket dapat thr lebih awal mahjong winskisah pelajar sma kuasai mahjong wins 3 inspirasi sekolahkisah penjaga parkir tanah abang motor baru mahjong winskisah sukses ibu rumah tangga bandung mahjong winsmahasiswa kkn beli iphone15 mahjong wins3 dakotamantan anak band influencer mahjong scatter hitampola rtp senin gates of olympus guru matematikaidola scatter hitam gampang meledakidola sensasi jackpotidola pola scatter hitamidola hasilkan jackpot dalam waktu singkatidola mahjong wins 3 bocortukang cuci motor menemukan keberuntungan di mahjong

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: Session/Session.php

Line Number: 282

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: session_set_cookie_params(): Cannot change session cookie parameters when headers already sent

Filename: Session/Session.php

Line Number: 294

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: Session/Session.php

Line Number: 304

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: Session/Session.php

Line Number: 314

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: Session/Session.php

Line Number: 315

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: Session/Session.php

Line Number: 316

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: Session/Session.php

Line Number: 317

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: Session/Session.php

Line Number: 375

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: drivers/Session_files_driver.php

Line Number: 108

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: session_set_save_handler(): Cannot change save handler when headers already sent

Filename: Session/Session.php

Line Number: 110

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: session_start(): Cannot start session when headers already sent

Filename: Session/Session.php

Line Number: 143

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

Xử lý mảng trong VBA thông qua ví dụ đơn giản
MACRO
EXCEL OBJECT
VBA BASIC
VBA FORM
BÀI TẬP
CÁC CHỦ ĐỀ
BÀI MỚI NHẤT
MỚI CẬP NHẬT

Xử lý mảng trong VBA thông qua ví dụ đơn giản

Trong bài này mình sẽ hướng dẫn cách xử lý mảng trong VBA, đây là kiểu dữ liệu dạng danh sách và được sử dụng rất nhiều, nhất là lập trinh liên quan đến vòng lặp.

test php

banquyen png
Bài viết này được đăng tại freetuts.net, không được copy dưới mọi hình thức.

Vì các bạn không chuyên về lập trình nên mình sẽ giải thích thật đơn sơ nhất có thể nhé. Mảng là tập hợp một nhóm các giá trị có cùng kiểu dữ liệu được lưu trữ trong một danh sách, được đánh dấu để có thể truy xuất đúng phần tử mong muốn.

Ví dụ bạn có một cái tủ đựng sách và có 100 ngăn, trong đó mỗi ngăn được đánh số từ 0 đến 100, mỗi ngăn được dùng để lưu trữ các loại sách, và ta có thể xem cái tủ này là một mảng, còn các ngăn tủ là các phần tử của mảng.

Bạn có thể xem chi tiết hơn về mảng trong Visual Basic tại bài viết này.

Bài viết này được đăng tại [free tuts .net]

1. Khai báo mảng một chiều VBA

Việc khai báo mảng cũng giống như khai báo một biến thông thường, chỉ khác là ta phải khai báo thêm tổng số phần tử của mảng bằng cách đặt nó trong cặp dấu ngoặc ().

'Cách 1 : Using Dim
Dim arr1()	'không khai báo kích thước

'Cách 2 : có khai báo kích thước
Dim arr2(5)  'Khai báo mảng có 5 phần tử

'Cách 3 : sử dụng đối tượng 'Array'
Dim arr3
arr3 = Array("apple","Orange","Grapes")

Lưu ý:

  • Chỉ mục của mảng bắt đầu từ 0
  • Khi bạn chỉ định kích thước của mảng là 5 thì nó sẽ lưu được 6 phần tử, bởi chỉ mục bắt đầu từ 0
  • Mảng có thể lưu trữ mọi loại dữ liệu
  • Mỗi phần tử trong mảng có thể chứa nhiều kiểu dữ liệu khác nhau

2. Gán giá trị vào mảng một chiều VBA

Để gán giá trị vào mảng thì ta cần quan tâm đến hai yếu tố:

  • Dữ liệu cần gán
  • Vị trí phần tử cần gán

Ví dụ dưới đây mình khai báo mảng một chiều, sau đó gán giá trị cho chúng, cuối cùng là in dữ liệu ra bằng hộp thoại MsgBox.

Private Sub Constant_demo_Click()
   Dim arr(5)
   arr(0) = "1"
   arr(1) = "VBScript"
   arr(2) = 100 
   arr(3) = 2.45
   arr(4) = #10/07/2013# 
   arr(5) = #12.45 PM#
  
   MsgBox("Giá trị phần tử thứ 0 : " & arr(0))
   MsgBox("Giá trị phần tử thứ 1 : " & arr(1))
   MsgBox("Giá trị phần tử thứ 2 : " & arr(2))
   MsgBox("Giá trị phần tử thứ 3 : " & arr(3))
   MsgBox("Giá trị phần tử thứ 4 : " & arr(4))
   MsgBox("Giá trị phần tử thứ 5 : " & arr(5))
End Sub

3. Mảng hai chiều VBA

Mảng một chiều rất đơn giản, bạn có thể hiểu nó là một cột hoặc một hàng trong Excel.

Ví dụ cột A gồm các ô: 1, 2, 3, 4, 5, 6 thì

  • Tên mảng là cột A
  • các ô trong cột A: 1, 2, 3, 4, 5, 6 là các phần tử.

mang mot chieu JPG

Còn mảng hai chiều thì khác, nó giống như một bảng tính Excel nên được xác định bởi hai chỉ mục.

Ví dụ có bảng tính B2:E7 như sau:

mang hai chieu JPG

Mỗi phần tử sẽ được xác định bởi hai thông số, đó là chỉ mục hàng và cột: B2, C2, D2, E2, .....

Trên là mình sử dụng bảng tính trong Excel để trình bày cấu trúc của mảng. Thực tế là dữ liệu được lưu trong bộ nhớ của máy tính nên bạn không thể thấy bằng mắt thường được.

4. Khai báo mảng hai chiều VBA

Để khai báo mảng hai chiều ta sử dụng cú pháp sau:

Dim arr(2,3) as Variant

Mình đã khai báo mảng tên là arr gồm 2 hàng và 3 cột. Tuy nhiên thực tế thì lại có 3 hàng và 4 cột vì chỉ mục bắt đầu từ 0.

Ví dụ dưới đây mình vừa khai báo vừa gán dữ liệu cho mảng luôn.

Private Sub Constant_demo_Click()
   Dim arr(2,3) as Variant	' Khai báo mảng gồm 3 hàng và 4 cột
   arr(0,0) = "Việt nam" 
   arr(0,1) = "Thái Lan"
   arr(0,2) = "lào"           
   arr(0,3) = "Campuchia" 
   arr(1,0) = "Indonesia"           
   arr(1,1) = "Malaysia"           
   arr(1,2) = "Myanma"           
   arr(1,3) = "Philippin"           
   arr(2,0) = "Singapore"             
   arr(2,1) = "Dontimo"            
   arr(2,2) = "Brunei"             
   arr(2,3) = "Hàn Quốc"            
           
   MsgBox("Giá trị của phần tử 0,1 : " &  arr(0,1))
   MsgBox("Giá trị của phần tử 2,2 : " &  arr(2,2))
End Sub

Kết quả sẽ in ra hai giá trị "Thái Lan" và "Brunei".

Trên là cách sử dụng mảng trong VBA ở mức cơ bản nhất. Chúc bạn học tốt!

Cùng chuyên mục:

Cách chèn link ảnh vào Excel trên máy tính

Cách chèn link ảnh vào Excel trên máy tính

Ngoài ra, bài viết còn hướng dẫn cách chèn link ảnh trực tuyến hàng loạt…

Cách tách và gộp ô trong Excel

Cách tách và gộp ô trong Excel

Trong bảng tính Microsoft Excel, có thể sẽ có lúc các bạn cần tách ô…

Hàm MONTH() trong Excel

Hàm MONTH() trong Excel

Cách dùng hàm MONTH() trong Excel

Hàm YEAR () trong Excel

Hàm YEAR () trong Excel

Cách dùng hàm YEAR () trong Excel

Hàm HLOOKUP() trong Excel

Hàm HLOOKUP() trong Excel

Cách dùng hàm HLOOKUP() trong Excel

Hàm VLOOKUP() trong Excel

Hàm VLOOKUP() trong Excel

Cách dùng hàm VLOOKUP() trong Excel

Hàm LOOKUP() trong Excel

Hàm LOOKUP() trong Excel

Cách dùng hàm LOOKUP() trong Excel

Hàm ABS() trong Excel

Hàm ABS() trong Excel

Cách dùng hàm ABS() trong Excel

Hàm SUM() trong Excel

Hàm SUM() trong Excel

Cách dùng hàm SUM() trong Excel

Hàm SQRT() trong Excel

Hàm SQRT() trong Excel

Cách dùng hàm SQRT() trong Excel

Hàm MOD() trong Excel

Hàm MOD() trong Excel

Cách dùng hàm MOD() trong Excel

Hàm SUMIF() trong Excel

Hàm SUMIF() trong Excel

Cách dùng hàm SUMIF() trong Excel

Hàm SUBTOTAL() trong Excel

Hàm SUBTOTAL() trong Excel

Cách dùng hàm SUBTOTAL() trong Excel

Hàm ROUND() trong Excel

Hàm ROUND() trong Excel

Cách dùng hàm ROUND() trong Excel

Hàm SUMPRODUCT() trong Excel

Hàm SUMPRODUCT() trong Excel

Cách dùng hàm SUMPRODUCT() trong Excel

Hàm COUNT() trong Excel

Hàm COUNT() trong Excel

Cách dùng hàm COUNT() trong Excel

Hàm COUNTA() trong Excel

Hàm COUNTA() trong Excel

Cách dùng hàm COUNTA() trong Excel

Hàm COUNTBLANK() trong Excel

Hàm COUNTBLANK() trong Excel

Cách dùng hàm COUNTBLANK() trong Excel

Hàm MAX() trong Excel

Hàm MAX() trong Excel

Cách dùng hàm MAX() trong Excel

Hàm MIN() trong Excel

Hàm MIN() trong Excel

Cách dùng hàm MIN() trong Excel

Top