[Nhờ giúp Macro] Macro báo lỗi không chạy nếu đặt tên sheet khác

Liên hệ QC

wkiz

Thành viên mới
Tham gia
8/9/19
Bài viết
3
Được thích
0
Xin chào anh/chị,
Em mới học excel vba có 1 đoạn Macro như bên dưới. Vấn đề của em là, nếu em đặt tên sheet là "test" thì đoạn code chạy bình thường, nhưng nếu sửa tên sheet đó khác đi thì sẽ ko chạy đc.

Mã:
Sub taobangchuan()
'
' taobangchuan Macro
'

'
    Columns("A:A").Select
    Selection.ClearContents
    Range("A1").Select
    ActiveCell.FormulaR1C1 = "STT"
    Columns("E:G").Select
    Selection.Delete Shift:=xlToLeft
    Columns("F:F").Select
    Selection.Delete Shift:=xlToLeft
    Columns("G:R").Select
    Selection.Delete Shift:=xlToLeft
    Columns("H:BB").Select
    Selection.Delete Shift:=xlToLeft
    Columns("B:B").Select
    ActiveWorkbook.Worksheets("test").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("test").Sort.SortFields.Add Key:=Range("B1"), _
        SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("test").Sort
        .SetRange Range("A2:G50")
        .Header = xlNo
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    Range("B1").Select
    ActiveCell.FormulaR1C1 = "GDV"
    Range("C1").Select
    ActiveCell.FormulaR1C1 = "Mã Khách Hàng"
    Range("D1").Select
    ActiveCell.FormulaR1C1 = "Sô H?p Ð?ng"
    Range("E1").Select
    ActiveCell.FormulaR1C1 = "Ngày Vay"
    Range("F1").Select
    ActiveCell.FormulaR1C1 = "Du N?"
    Range("G1").Select
    ActiveCell.FormulaR1C1 = "Tên Khách Hàng"
    Cells.Select
    With Selection.Font
        .Name = "Arial"
        .Size = 15
        .Strikethrough = False
        .Superscript = False
        .Subscript = False
        .OutlineFont = False
        .Shadow = False
        .Underline = xlUnderlineStyleNone
        .ColorIndex = xlAutomatic
        .TintAndShade = 0
        .ThemeFont = xlThemeFontNone
    End With
    Cells.EntireColumn.AutoFit
    Columns("F:F").Select
    Selection.Style = "Comma"
    Selection.NumberFormat = "_(* #,##0.0_);_(* (#,##0.0);_(* ""-""??_);_(@_)"
    Selection.NumberFormat = "_(* #,##0_);_(* (#,##0);_(* ""-""??_);_(@_)"
    Range("A2").Select
    ActiveCell.FormulaR1C1 = "1"
    Range("A3").Select
    ActiveCell.FormulaR1C1 = "2"
    Range("A2:A3").Select
    Selection.AutoFill Destination:=Range("A2:A50"), Type:=xlFillDefault
    Range("A2:A50").Select
    Columns("E:E").Select
    Selection.NumberFormat = "dd/mm/yyyy"
    Columns("E:E").Select
    Selection.Cut
    Columns("H:H").Select
    Selection.Insert Shift:=xlToRight
    Range("A1").Select
End Sub

Em xin cảm ơn.
 

File đính kèm

  • test.XLS
    52 KB · Đọc: 8
Xin chào anh/chị,
Em mới học excel vba có 1 đoạn Macro như bên dưới. Vấn đề của em là, nếu em đặt tên sheet là "test" thì đoạn code chạy bình thường, nhưng nếu sửa tên sheet đó khác đi thì sẽ ko chạy đc.

Mã:
Sub taobangchuan()
'
' taobangchuan Macro
'

'
    Columns("A:A").Select
    Selection.ClearContents
    Range("A1").Select
    ActiveCell.FormulaR1C1 = "STT"
    Columns("E:G").Select
    Selection.Delete Shift:=xlToLeft
    Columns("F:F").Select
    Selection.Delete Shift:=xlToLeft
    Columns("G:R").Select
    Selection.Delete Shift:=xlToLeft
    Columns("H:BB").Select
    Selection.Delete Shift:=xlToLeft
    Columns("B:B").Select
    ActiveWorkbook.Worksheets("test").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("test").Sort.SortFields.Add Key:=Range("B1"), _
        SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("test").Sort
        .SetRange Range("A2:G50")
        .Header = xlNo
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    Range("B1").Select
    ActiveCell.FormulaR1C1 = "GDV"
    Range("C1").Select
    ActiveCell.FormulaR1C1 = "Mã Khách Hàng"
    Range("D1").Select
    ActiveCell.FormulaR1C1 = "Sô H?p Ð?ng"
    Range("E1").Select
    ActiveCell.FormulaR1C1 = "Ngày Vay"
    Range("F1").Select
    ActiveCell.FormulaR1C1 = "Du N?"
    Range("G1").Select
    ActiveCell.FormulaR1C1 = "Tên Khách Hàng"
    Cells.Select
    With Selection.Font
        .Name = "Arial"
        .Size = 15
        .Strikethrough = False
        .Superscript = False
        .Subscript = False
        .OutlineFont = False
        .Shadow = False
        .Underline = xlUnderlineStyleNone
        .ColorIndex = xlAutomatic
        .TintAndShade = 0
        .ThemeFont = xlThemeFontNone
    End With
    Cells.EntireColumn.AutoFit
    Columns("F:F").Select
    Selection.Style = "Comma"
    Selection.NumberFormat = "_(* #,##0.0_);_(* (#,##0.0);_(* ""-""??_);_(@_)"
    Selection.NumberFormat = "_(* #,##0_);_(* (#,##0);_(* ""-""??_);_(@_)"
    Range("A2").Select
    ActiveCell.FormulaR1C1 = "1"
    Range("A3").Select
    ActiveCell.FormulaR1C1 = "2"
    Range("A2:A3").Select
    Selection.AutoFill Destination:=Range("A2:A50"), Type:=xlFillDefault
    Range("A2:A50").Select
    Columns("E:E").Select
    Selection.NumberFormat = "dd/mm/yyyy"
    Columns("E:E").Select
    Selection.Cut
    Columns("H:H").Select
    Selection.Insert Shift:=xlToRight
    Range("A1").Select
End Sub

Em xin cảm ơn.
Đơn giản là Code trên của bạn chỉ tác động vào Sheets("test") mà thôi.
 
"record Macro"
-------------------------------
PHP:
Sub taobangchuan()
  Dim WS As Worksheet
  Set WS = ThisWorkbook.ActiveSheet 'Worksheets("test")
  With WS
    .Columns("A:A").ClearContents
    .Range("E:BB").Delete Shift:=xlToLeft
    Dim Arr: Arr = Array("STT", "GDV", _
                   "M" & ChrW(227) & " Kh" & ChrW(225) & "ch H" & ChrW(224) & "ng", _
                   "S" & ChrW(7889) & " H" & ChrW(7907) & "p " & ChrW(273) & "" & ChrW(7891) & "ng", _
                   "D" & ChrW(432) & " N" & ChrW(7907) & "", _
                   "Tên Khách Hàng", _
                   "Ngày Vay")
    .Range("A1").Resize(1, UBound(Arr) + 1).Value = Arr
    .Sort.SortFields.Clear
    .Sort.SortFields.Add Key:=.Range("B1"), _
        SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With .Sort
        .SetRange WS.Range("A2:G50")
        .Header = xlNo
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    With .Cells.Font
        .Name = "Arial"
        .Size = 15
        .Strikethrough = False
        .Superscript = False
        .Subscript = False
        .OutlineFont = False
        .Shadow = False
        .Underline = xlUnderlineStyleNone
        .ColorIndex = xlAutomatic
        .TintAndShade = 0
        .ThemeFont = xlThemeFontNone
    End With
    .Cells.EntireColumn.AutoFit
    With .Columns("E:E")
      .Style = "Comma"
      .NumberFormat = "_(* #,##0_);_(* (#,##0);_(* ""-""??_);_(@_)"
    End With
    .Range("A2").Value = 1
    .Range("A3").Value = 2
    .Range("A2:A3").AutoFill Destination:=.Range("A2:A50"), Type:=xlFillDefault
    .Columns("G:G").NumberFormat = "dd/mm/yyyy"
  End With
End Sub
 
Web KT
Back
Top Bottom