Làm sao điều khiển Sheet1 khi đang ở Sheet2?

Liên hệ QC

thienthanbe

Thành viên mới
Tham gia
27/10/08
Bài viết
43
Được thích
2
Chào các bác!
Em có đoạn code sau
PHP:
Private Sub CommandButton1_Click()
    If TextBox1.Value <> "" And TextBox2.Value <> "" And TextBox3.Value <> "" Then
    Range("A" & i & ":E" & i).Select
    With Selection.Interior
        .ColorIndex = 42
        .Pattern = xlSolid
   End With
   Selection.Borders(xlDiagonalDown).LineStyle = xlNone
    Selection.Borders(xlDiagonalUp).LineStyle = xlNone
    With Selection.Borders(xlEdgeLeft)
       .LineStyle = xlContinuous
       .Weight = xlThin
        .ColorIndex = xlAutomatic
    End With
    With Selection.Borders(xlEdgeTop)
       .LineStyle = xlContinuous
       .Weight = xlThin
       .ColorIndex = xlAutomatic
    End With
    With Selection.Borders(xlEdgeBottom)
        .LineStyle = xlContinuous
        .Weight = xlHairline
        .ColorIndex = xlAutomatic
    End With
    With Selection.Borders(xlEdgeRight)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
    End With
    With Selection.Borders(xlInsideVertical)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
    End With
    With Selection.Font
       .Name = ".VnTime"
        .Size = 10
        .Strikethrough = False
        .Superscript = False
        .Subscript = False
        .OutlineFont = False
        .Shadow = False
        .Underline = xlUnderlineStyleNone
        .ColorIndex = xlAutomatic
    End With
    Range("A" & i) = i - 3
    Range("B" & i).FormulaR1C1 = Int(Now)
    Range("C" & i).FormulaR1C1 = UserForm1.TextBox1.Value
    Range("D" & i).FormulaR1C1 = UserForm1.TextBox2.Value
    Range("E" & i).FormulaR1C1 = UserForm1.TextBox3.Value
    Range("E" & i).NumberFormat = "#,##0"
    ActiveWorkbook.Save    
    End If
   End Sub
Cái này chỉ làm việc với sheet1 khi em đang ở Sheet1, Các bác chỉ hộ em cách nào khi đang ở sheet2 vẫn thực hiện nó trên sheet1 mà không cần phải select sheet1?
Cám ơn các bác.
 
Bạn cứ chỉ dẫn tường minh sheet nhận tác động bằng SheetName hay NameCode thì dù bạn ở đâu (Sheet khác, trên Form, Code runtime...) bạn vẫn thực hiện ý muốn của mình, không nhất thiết sheet đó phải Active. Trong code của bạn phải lưu ý dẫn chiếu đến sheet cụ thể nếu không phải là sheet hiện hành phần gạch chân (Mình kiểm tra chưa hết không biết còn đoạn nào không).

Mã:
[COLOR=#000000][COLOR=#007700]Private [/COLOR][COLOR=#0000bb]Sub CommandButton1_Click[/COLOR][COLOR=#007700]()
    If [/COLOR][COLOR=#0000bb]TextBox1[/COLOR][COLOR=#007700].[/COLOR][COLOR=#0000bb]Value [/COLOR][COLOR=#007700]<> [/COLOR][COLOR=#dd0000]"" [/COLOR][COLOR=#007700]And [/COLOR][COLOR=#0000bb]TextBox2[/COLOR][COLOR=#007700].[/COLOR][COLOR=#0000bb]Value [/COLOR][COLOR=#007700]<> [/COLOR][COLOR=#dd0000]"" [/COLOR][COLOR=#007700]And [/COLOR][COLOR=#0000bb]TextBox3[/COLOR][COLOR=#007700].[/COLOR][COLOR=#0000bb]Value [/COLOR][COLOR=#007700]<> [/COLOR][COLOR=#dd0000]"" [/COLOR][COLOR=#0000bb]Then
    [U]Range[/U][/COLOR][U][COLOR=#007700]([/COLOR][COLOR=#dd0000]"A" [/COLOR][COLOR=#007700]& [/COLOR][COLOR=#0000bb]i [/COLOR][COLOR=#007700]& [/COLOR][COLOR=#dd0000]":E" [/COLOR][COLOR=#007700]& [/COLOR][COLOR=#0000bb]i[/COLOR][COLOR=#007700]).[/COLOR][/U][COLOR=#0000bb][U]Select[/U]
    With Selection[/COLOR][COLOR=#007700].[/COLOR][COLOR=#0000bb]Interior
        [/COLOR][COLOR=#007700].[/COLOR][COLOR=#0000bb]ColorIndex [/COLOR][COLOR=#007700]= [/COLOR][COLOR=#0000bb]42
        [/COLOR][COLOR=#007700].[/COLOR][COLOR=#0000bb]Pattern [/COLOR][COLOR=#007700]= [/COLOR][COLOR=#0000bb]xlSolid
   End With
   Selection[/COLOR][COLOR=#007700].[/COLOR][COLOR=#0000bb]Borders[/COLOR][COLOR=#007700]([/COLOR][COLOR=#0000bb]xlDiagonalDown[/COLOR][COLOR=#007700]).[/COLOR][COLOR=#0000bb]LineStyle [/COLOR][COLOR=#007700]= [/COLOR][COLOR=#0000bb]xlNone
    Selection[/COLOR][COLOR=#007700].[/COLOR][COLOR=#0000bb]Borders[/COLOR][COLOR=#007700]([/COLOR][COLOR=#0000bb]xlDiagonalUp[/COLOR][COLOR=#007700]).[/COLOR][COLOR=#0000bb]LineStyle [/COLOR][COLOR=#007700]= [/COLOR][COLOR=#0000bb]xlNone
    With Selection[/COLOR][COLOR=#007700].[/COLOR][COLOR=#0000bb]Borders[/COLOR][COLOR=#007700]([/COLOR][COLOR=#0000bb]xlEdgeLeft[/COLOR][COLOR=#007700])
       .[/COLOR][COLOR=#0000bb]LineStyle [/COLOR][COLOR=#007700]= [/COLOR][COLOR=#0000bb]xlContinuous
       [/COLOR][COLOR=#007700].[/COLOR][COLOR=#0000bb]Weight [/COLOR][COLOR=#007700]= [/COLOR][COLOR=#0000bb]xlThin
        [/COLOR][COLOR=#007700].[/COLOR][COLOR=#0000bb]ColorIndex [/COLOR][COLOR=#007700]= [/COLOR][COLOR=#0000bb]xlAutomatic
    End With
    With Selection[/COLOR][COLOR=#007700].[/COLOR][COLOR=#0000bb]Borders[/COLOR][COLOR=#007700]([/COLOR][COLOR=#0000bb]xlEdgeTop[/COLOR][COLOR=#007700])
       .[/COLOR][COLOR=#0000bb]LineStyle [/COLOR][COLOR=#007700]= [/COLOR][COLOR=#0000bb]xlContinuous
       [/COLOR][COLOR=#007700].[/COLOR][COLOR=#0000bb]Weight [/COLOR][COLOR=#007700]= [/COLOR][COLOR=#0000bb]xlThin
       [/COLOR][COLOR=#007700].[/COLOR][COLOR=#0000bb]ColorIndex [/COLOR][COLOR=#007700]= [/COLOR][COLOR=#0000bb]xlAutomatic
    End With
    With Selection[/COLOR][COLOR=#007700].[/COLOR][COLOR=#0000bb]Borders[/COLOR][COLOR=#007700]([/COLOR][COLOR=#0000bb]xlEdgeBottom[/COLOR][COLOR=#007700])
        .[/COLOR][COLOR=#0000bb]LineStyle [/COLOR][COLOR=#007700]= [/COLOR][COLOR=#0000bb]xlContinuous
        [/COLOR][COLOR=#007700].[/COLOR][COLOR=#0000bb]Weight [/COLOR][COLOR=#007700]= [/COLOR][COLOR=#0000bb]xlHairline
        [/COLOR][COLOR=#007700].[/COLOR][COLOR=#0000bb]ColorIndex [/COLOR][COLOR=#007700]= [/COLOR][COLOR=#0000bb]xlAutomatic
    End With
    With Selection[/COLOR][COLOR=#007700].[/COLOR][COLOR=#0000bb]Borders[/COLOR][COLOR=#007700]([/COLOR][COLOR=#0000bb]xlEdgeRight[/COLOR][COLOR=#007700])
        .[/COLOR][COLOR=#0000bb]LineStyle [/COLOR][COLOR=#007700]= [/COLOR][COLOR=#0000bb]xlContinuous
        [/COLOR][COLOR=#007700].[/COLOR][COLOR=#0000bb]Weight [/COLOR][COLOR=#007700]= [/COLOR][COLOR=#0000bb]xlThin
        [/COLOR][COLOR=#007700].[/COLOR][COLOR=#0000bb]ColorIndex [/COLOR][COLOR=#007700]= [/COLOR][COLOR=#0000bb]xlAutomatic
    End With
    With Selection[/COLOR][COLOR=#007700].[/COLOR][COLOR=#0000bb]Borders[/COLOR][COLOR=#007700]([/COLOR][COLOR=#0000bb]xlInsideVertical[/COLOR][COLOR=#007700])
        .[/COLOR][COLOR=#0000bb]LineStyle [/COLOR][COLOR=#007700]= [/COLOR][COLOR=#0000bb]xlContinuous
        [/COLOR][COLOR=#007700].[/COLOR][COLOR=#0000bb]Weight [/COLOR][COLOR=#007700]= [/COLOR][COLOR=#0000bb]xlThin
        [/COLOR][COLOR=#007700].[/COLOR][COLOR=#0000bb]ColorIndex [/COLOR][COLOR=#007700]= [/COLOR][COLOR=#0000bb]xlAutomatic
    End With
    With Selection[/COLOR][COLOR=#007700].[/COLOR][COLOR=#0000bb]Font
       [/COLOR][COLOR=#007700].[/COLOR][COLOR=#0000bb]Name [/COLOR][COLOR=#007700]= [/COLOR][COLOR=#dd0000]".VnTime"
        [/COLOR][COLOR=#007700].[/COLOR][COLOR=#0000bb]Size [/COLOR][COLOR=#007700]= [/COLOR][COLOR=#0000bb]10
        [/COLOR][COLOR=#007700].[/COLOR][COLOR=#0000bb]Strikethrough [/COLOR][COLOR=#007700]= [/COLOR][COLOR=#0000bb]False
        [/COLOR][COLOR=#007700].[/COLOR][COLOR=#0000bb]Superscript [/COLOR][COLOR=#007700]= [/COLOR][COLOR=#0000bb]False
        [/COLOR][COLOR=#007700].[/COLOR][COLOR=#0000bb]Subscript [/COLOR][COLOR=#007700]= [/COLOR][COLOR=#0000bb]False
        [/COLOR][COLOR=#007700].[/COLOR][COLOR=#0000bb]OutlineFont [/COLOR][COLOR=#007700]= [/COLOR][COLOR=#0000bb]False
        [/COLOR][COLOR=#007700].[/COLOR][COLOR=#0000bb]Shadow [/COLOR][COLOR=#007700]= [/COLOR][COLOR=#0000bb]False
        [/COLOR][COLOR=#007700].[/COLOR][COLOR=#0000bb]Underline [/COLOR][COLOR=#007700]= [/COLOR][COLOR=#0000bb]xlUnderlineStyleNone
        [/COLOR][COLOR=#007700].[/COLOR][COLOR=#0000bb]ColorIndex [/COLOR][COLOR=#007700]= [/COLOR][COLOR=#0000bb]xlAutomatic
    End With
    [U]Range[/U][/COLOR][U][COLOR=#007700]([/COLOR][COLOR=#dd0000]"A" [/COLOR][COLOR=#007700]& [/COLOR][COLOR=#0000bb]i[/COLOR][COLOR=#007700]) = [/COLOR][COLOR=#0000bb]i [/COLOR][COLOR=#007700]- [/COLOR][COLOR=#0000bb]3
    Range[/COLOR][COLOR=#007700]([/COLOR][COLOR=#dd0000]"B" [/COLOR][COLOR=#007700]& [/COLOR][COLOR=#0000bb]i[/COLOR][COLOR=#007700]).[/COLOR][COLOR=#0000bb]FormulaR1C1 [/COLOR][COLOR=#007700]= [/COLOR][COLOR=#0000bb]Int[/COLOR][COLOR=#007700]([/COLOR][COLOR=#0000bb]Now[/COLOR][COLOR=#007700])
    [/COLOR][COLOR=#0000bb]Range[/COLOR][COLOR=#007700]([/COLOR][COLOR=#dd0000]"C" [/COLOR][COLOR=#007700]& [/COLOR][COLOR=#0000bb]i[/COLOR][COLOR=#007700]).[/COLOR][COLOR=#0000bb]FormulaR1C1 [/COLOR][COLOR=#007700]= [/COLOR][COLOR=#0000bb]UserForm1[/COLOR][COLOR=#007700].[/COLOR][COLOR=#0000bb]TextBox1[/COLOR][COLOR=#007700].[/COLOR][COLOR=#0000bb]Value
    Range[/COLOR][COLOR=#007700]([/COLOR][COLOR=#dd0000]"D" [/COLOR][COLOR=#007700]& [/COLOR][COLOR=#0000bb]i[/COLOR][COLOR=#007700]).[/COLOR][COLOR=#0000bb]FormulaR1C1 [/COLOR][COLOR=#007700]= [/COLOR][COLOR=#0000bb]UserForm1[/COLOR][COLOR=#007700].[/COLOR][COLOR=#0000bb]TextBox2[/COLOR][COLOR=#007700].[/COLOR][COLOR=#0000bb]Value
    Range[/COLOR][COLOR=#007700]([/COLOR][COLOR=#dd0000]"E" [/COLOR][COLOR=#007700]& [/COLOR][COLOR=#0000bb]i[/COLOR][COLOR=#007700]).[/COLOR][COLOR=#0000bb]FormulaR1C1 [/COLOR][COLOR=#007700]= [/COLOR][COLOR=#0000bb]UserForm1[/COLOR][COLOR=#007700].[/COLOR][COLOR=#0000bb]TextBox3[/COLOR][COLOR=#007700].[/COLOR][COLOR=#0000bb]Value
    Range[/COLOR][COLOR=#007700]([/COLOR][COLOR=#dd0000]"E" [/COLOR][COLOR=#007700]& [/COLOR][COLOR=#0000bb]i[/COLOR][COLOR=#007700]).[/COLOR][COLOR=#0000bb]NumberFormat [/COLOR][COLOR=#007700]= [/COLOR][/U][COLOR=#dd0000][U]"#,##0"[/U]
    [/COLOR][COLOR=#0000bb]ActiveWorkbook[/COLOR][COLOR=#007700].[/COLOR][COLOR=#0000bb]Save    
    End [/COLOR][COLOR=#007700]If
   [/COLOR][COLOR=#0000bb]End Sub  [/COLOR][/COLOR]
Ví dụ đoạn gạch chân 1 (Nhớ bỏ select đi):

Mã:
[COLOR=#000000][COLOR=#0000bb]Dim rng as range
set rng= sheet3.Range[/COLOR][COLOR=#007700]([/COLOR][COLOR=#dd0000]"A" [/COLOR][COLOR=#007700]& [/COLOR][COLOR=#0000bb]i [/COLOR][COLOR=#007700]& [/COLOR][COLOR=#dd0000]":E" [/COLOR][COLOR=#007700]& [/COLOR][COLOR=#0000bb]i[/COLOR][COLOR=#007700])[/COLOR][/COLOR]

thay toàn bôj selection bằng rng
Đoạn Code dưới sử lý như sau:
Thay:
Mã:
[/COLOR][/COLOR][COLOR=#000000][COLOR=#0000bb]Range[/COLOR][COLOR=#007700]([/COLOR][COLOR=#dd0000]"A" [/COLOR][COLOR=#007700]& [/COLOR][COLOR=#0000bb]i[/COLOR][COLOR=#007700]) = [/COLOR][COLOR=#0000bb]i [/COLOR][COLOR=#007700]- [/COLOR][COLOR=#0000bb]3
Bằng:
Mã:
[/COLOR][/COLOR][COLOR=#000000][COLOR=#0000bb]sheet3.Range[/COLOR][COLOR=#007700]([/COLOR][COLOR=#dd0000]"A" [/COLOR][COLOR=#007700]& [/COLOR][COLOR=#0000bb]i[/COLOR][COLOR=#007700]) = [/COLOR][COLOR=#0000bb]i [/COLOR][COLOR=#007700]- [/COLOR][COLOR=#0000bb]3
.....
.....
 
Lần chỉnh sửa cuối:
Upvote 0
Bạn cứ chỉ dẫn tường minh sheet nhận tác động bằng SheetName hay NameCode thì dù bạn ở đâu (Sheet khác, trên Form, Code runtime...) bạn vẫn thực hiện ý muốn của mình, không nhất thiết sheet đó phải Active.

Không được bác ạ. câu lệnh
PHP:
Sheet1.Range("A" & i & ":E" & i).Select
bị báo lỗi. em không biết khắc phục như thế nào
 
Upvote 0
Web KT

Bài viết mới nhất

Back
Top Bottom