Giúp gộp code giãn chiều cao dòng vào nút Spin button và vào Form in (2 người xem)

Liên hệ QC

Người dùng đang xem chủ đề này

Bùi Thúy Thúy

Thành viên thường trực
Tham gia
2/7/18
Bài viết
289
Được thích
38
Xin chào các anh/chị em có đoạn code giãn dòng chỉ các tác dụng trong từng sheet:
Ở ví dụ em muốn dữ liệu vùng gộp D20:L20 trong sheet(NT A_B) tự động giãn chiều cao dòng khi dữ liệu được lấy từ ô B8 trong sheet (List) sang khi dùng nút cuộn Spin và khi in bằng Form "In_BB_All"
Dữ liệu được lấy sang cột phụ, cột phụ này có chiều rộng bằng tổng chiều rộng cột từ D20:L20.
Đoạn code giãn dòng trong sheet(NT A_B) như sau:
Mã:
Private Sub Worksheet_Calculate()
    Application.ScreenUpdating = False
            'Rows(FitRows).EntireRow.AutoFit
    Range("A20").EntireRow.AutoFit
            Application.ScreenUpdating = True
End Sub
> Giờ em in ấn sheet trên bằng Form (như hình 1), Code của Form như sau:
Mã:
'Company Name: Nguyên Quôc Tuyên' - 0989 839 198
'General E-mail Address: nguyentuyen.tvgt.tq@gmail.com
'Company Home Page: TP.Tuyên Quang - Facebook: https://www.facebook.com/VoVa.tq.86

'CODE IN BIEN BAN NGHIÊM THU
Private Sub OkInBB_All_Click()
    Dim inStart, inFinish As Long 'Dùng Inputbox dôi? Integer
    Dim I As Long
    Dim ws As Worksheet
    ActiveSheet.DisplayPageBreaks = False
  
    On Error GoTo Thoat
    Set ws = ActiveSheet
    inStart = TextBox1.Value 'Nêu'ko dùng form > inStart = application.Inputbox("In Tu:")
    inFinish = TextBox2.Value 'Nêu'ko dùng form > inFinish = application.Inputbox("In Dên':")
        For I = inStart To inFinish
            With ws
            ActiveSheet.DisplayPageBreaks = False
            .Range("L10").Value = I
            .PrintOut 'Vùng in Set
            End With
        Next I
Thoat:
    Unload Me
    ActiveSheet.DisplayPageBreaks = False
End Sub

Private Sub ThoatInBB_All_Click()
    Unload Me
    ActiveSheet.DisplayPageBreaks = False
End Sub



'***********************************************************************************************************************
'LUA CHON MÁY IN
Private Sub Printer_Properties_Click()
    Call Shell("rundll32 printui.dll,PrintUIEntry /p /n """ & ComboBox1.Value & """", vbNormalFocus)
End Sub

Sub List_Printer()
    Dim aPrinters As Object
    Dim I As Long, N As Long
    With CreateObject("WScript.Network")
        Set aPrinters = .EnumPrinterConnections
        For I = 1 To aPrinters.Count Step 2
            ComboBox1.AddItem aPrinters.Item(I)
        Next
    End With
End Sub

Sub Get_Default_Printer()
    Dim WSHshell As Object, RegKey As String, RegKeySplit As Variant
    Dim RegDefault As String, MyPrinter As String
    RegKey = _
            "HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Windows\Device"
    Set WSHshell = CreateObject("WScript.Shell")
    RegDefault = WSHshell.RegRead(RegKey)
    Set WSHshell = Nothing
    RegKeySplit = Split(RegDefault, ",")
    MyPrinter = RegKeySplit(0)
    ComboBox1.Text = MyPrinter
End Sub

Private Sub UserForm_Initialize()
    Call List_Printer
    Call Get_Default_Printer
End Sub
'***********************************************************************************************************************
Em nhờ các anh, chị giúp em gộp code ở sheet(NT A_B) vào Form để khi in trên Form thì code co giãn ở sheet có tác dụng luôn.
(Các anh, chị giúp em gộp code mới nhé!)
3333.png
 

File đính kèm

Lần chỉnh sửa cuối:
Xin chào các anh/chị em có đoạn code giãn dòng chỉ các tác dụng trong từng sheet:
Ở ví dụ em muốn dữ liệu vùng gộp D20:L20 trong sheet(NT A_B) tự động giãn chiều cao dòng khi dữ liệu được lấy từ ô B8 trong sheet (List) sang khi dùng nút cuộn Spin và khi in bằng Form "In_BB_All"
Dữ liệu được lấy sang cột phụ, cột phụ này có chiều rộng bằng tổng chiều rộng cột từ D20:L20.
Đoạn code giãn dòng trong sheet(NT A_B) như sau:
Mã:
Private Sub Worksheet_Calculate()
    Application.ScreenUpdating = False
            'Rows(FitRows).EntireRow.AutoFit
    Range("A20").EntireRow.AutoFit
            Application.ScreenUpdating = True
End Sub
> Giờ em in ấn sheet trên bằng Form (như hình 1), Code của Form như sau:
Mã:
'Company Name: Nguyên Quôc Tuyên' - 0989 839 198
'General E-mail Address: nguyentuyen.tvgt.tq@gmail.com
'Company Home Page: TP.Tuyên Quang - Facebook: https://www.facebook.com/VoVa.tq.86

'CODE IN BIEN BAN NGHIÊM THU
Private Sub OkInBB_All_Click()
    Dim inStart, inFinish As Long 'Dùng Inputbox dôi? Integer
    Dim I As Long
    Dim ws As Worksheet
    ActiveSheet.DisplayPageBreaks = False
 
    On Error GoTo Thoat
    Set ws = ActiveSheet
    inStart = TextBox1.Value 'Nêu'ko dùng form > inStart = application.Inputbox("In Tu:")
    inFinish = TextBox2.Value 'Nêu'ko dùng form > inFinish = application.Inputbox("In Dên':")
        For I = inStart To inFinish
            With ws
            ActiveSheet.DisplayPageBreaks = False
            .Range("L10").Value = I
            .PrintOut 'Vùng in Set
            End With
        Next I
Thoat:
    Unload Me
    ActiveSheet.DisplayPageBreaks = False
End Sub

Private Sub ThoatInBB_All_Click()
    Unload Me
    ActiveSheet.DisplayPageBreaks = False
End Sub



'***********************************************************************************************************************
'LUA CHON MÁY IN
Private Sub Printer_Properties_Click()
    Call Shell("rundll32 printui.dll,PrintUIEntry /p /n """ & ComboBox1.Value & """", vbNormalFocus)
End Sub

Sub List_Printer()
    Dim aPrinters As Object
    Dim I As Long, N As Long
    With CreateObject("WScript.Network")
        Set aPrinters = .EnumPrinterConnections
        For I = 1 To aPrinters.Count Step 2
            ComboBox1.AddItem aPrinters.Item(I)
        Next
    End With
End Sub

Sub Get_Default_Printer()
    Dim WSHshell As Object, RegKey As String, RegKeySplit As Variant
    Dim RegDefault As String, MyPrinter As String
    RegKey = _
            "HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Windows\Device"
    Set WSHshell = CreateObject("WScript.Shell")
    RegDefault = WSHshell.RegRead(RegKey)
    Set WSHshell = Nothing
    RegKeySplit = Split(RegDefault, ",")
    MyPrinter = RegKeySplit(0)
    ComboBox1.Text = MyPrinter
End Sub

Private Sub UserForm_Initialize()
    Call List_Printer
    Call Get_Default_Printer
End Sub
'***********************************************************************************************************************
Em nhờ các anh, chị giúp em gộp code ở sheet(NT A_B) vào Form để khi in trên Form thì code co giãn ở sheet có tác dụng luôn.
(Các anh, chị giúp em gộp code mới nhé!)
View attachment 229883
bạn đã làm được chưa chỉ cho mình với ạ
 
Web KT

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

Back
Top Bottom