Cám ơn bạn. Nhưng mà như vậy thì VB nó có hiểu không? Vì ý định của mình là chọn vùng nào đó dựa vào biến rồi copy qua một sheet khác.Thế này nhé bạn! Cells(2,5).Address hoặc Range("E2").Address nó trả về $E$2. Còn Range(cells(1,1),cells(5,8)).Address nó trả về $A$1:$H$5
range(cells(x,1),cells(x,y)).address
Sub TEST()
Dim r1, r2 As String
r1 = Cells(1, 1).Address: r2 = Cells(2, 5).Address
Range(r1 & ":" & r2).Select
MsgBox r1 & ":" & r2
End Sub
Sub Macro1() Dim i, j, r, h As Integer
Dim row, note As Long
Dim vung, day, shtname, x, y As String
i = Sheets.Count
j = 1
r = 1
h = 1
row = 15
note = 2
Sheets.Add after:=Sheets(Sheets.Count)
Sheets(i + 1).Name = "Total"
Do While (j <= i)
Do While (Sheets(j).Cells(r, 1) = "")
r = r + 1
Loop
Do While (Sheets(j).Cells(r - 1, h) <> "")
h = h + 1
Loop
If (j > i) Then
Sheets(j).Select
x = Cells(r - 1, 1).Address
y = Cells(r - 1, h - 1).Address
Range(x & ":" & y).Select
Selection.Copy
Sheets("Total").Select
day = "B" & note
Range(day).Select
ActiveSheet.Paste
Cells(1, 1) = "Region"
note = note + 1
End If
Do While (Sheets(j).Cells(row, 1) <> "")
row = row + 1
Loop
Sheets(j).Select
x = Cells(r - 1, 1)
y = Cells(row - 1, h - 1)
Range(x & ":" & y).Select
Selection.Copy
Sheets("Total").Select
day = "B" & note
Range(day).Select
ActiveSheet.Paste
day = "A" & note
Range(day) = Sheets(j).Name
shtname = day & ":A" & note + row - 2
Range(shtname).Select
Selection.FillDown
note = note + row - 2
row = 2
j = j + 1
Loop
Sheets("Total").Select
ActiveCell.SpecialCells(xlLastCell) = ""
Cells.Select
Cells.EntireColumn.AutoFit
Cells.EntireRow.AutoFit
Range("A1").Select
End Sub
Thay vì hỏi sao bạn không thí nghiệm trực tiếp?Cám ơn bạn. Nhưng mà như vậy thì VB nó có hiểu không?
Excel của mình là 2010, còn dữ liệu của mình thì bắt đầu từ A6:F6 trở đi, có cái A8:E8, có cái A14:H14, nói chung là không xác định trước được. Mà lỗi mình nhận đc là :Lỗi mà mình thấy có thể xảy ra như sau: trường hợp r hoặc h =1 thì sẽ lỗi. Cái nữa là bạn không giới hạn giá trị để thoát vòng lặp cho r và h, vì nếu dùng excel 2003, mà r=66537 sẽ báo lỗi, vì excel 2003 chỉ có 66536 dòng thôi.
runtime error '1004':
application-defined or object-defined error
y = Cells(row - 1, h - 1)
Bạn cho chạy code, sau khi báo lỗi, bạn rê chuột vào vùng code bị lỗi, rê lần lượt đến các biến y, row, h để check xem các biến đó đang ở giá trị nào, từ đó tìm ra lỗi.
Sub Macro1() Dim i, j, r, h As Integer
Dim row, note As Long
Dim vung, day, shtname, x, y As String
i = Sheets.Count
j = 1
r = 1
h = 1
row = 15
note = 2
Sheets.Add after:=Sheets(Sheets.Count)
Sheets(i + 1).Name = "Total"
Sheets(j).Select
Do While (Sheets(j).Cells(r, 1) = "")
r = r + 1
Loop
Do While (Sheets(j).Cells(r, h) <> "")
h = h + 1
Loop
x = Cells(r, 1).Address
y = Cells(r, h).Address
Range(x & ":" & y).Select
Selection.Copy
Sheets("Total").Select
Range("B1").Select
ActiveSheet.Paste
Cells(1, 1) = "Region"
Do While (j <= i)
Do While (Sheets(j).Cells(r, 1) = "")
r = r + 1
Loop
Do While (Sheets(j).Cells(r, h) <> "")
h = h + 1
Loop
If (j > i) Then
Sheets(j).Select
x = Cells(r, 1).Address
y = Cells(r, h).Address
Range(x & ":" & y).Select
Selection.Copy
Sheets("Total").Select
day = "B" & note
Range(day).Select
ActiveSheet.Paste
note = note + 1
End If
Do While (Sheets(j).Cells(row, 2) <> "")
row = row + 1
Loop
Sheets(j).Select
x = Cells(r + 1, 1).Address
y = Cells(row - 1, h - 1).Address
Range(x & ":" & y).Select
Selection.Copy
Sheets("Total").Select
day = "B" & note
Range(day).Select
ActiveSheet.Paste
day = "A" & note
Range(day) = Sheets(j).Name
[COLOR=#ff0000]shtname = day & ":A" & note + row - r - 1
Range(shtname).Select
Selection.FillDown
note = note + row - r - 1
row = 15[/COLOR]
j = j + 1
Loop
Sheets("Total").Select
ActiveCell.SpecialCells(xlLastCell) = ""
Cells.Select
Cells.EntireColumn.AutoFit
Cells.EntireRow.AutoFit
Range("A1").Select
End Sub
note = note + row - r - 1