Nhờ giúp đỡ mã code in liên tục,không trùng lặp

Liên hệ QC

Ngày mai trời lại sáng

Thành viên thường trực
Tham gia
4/7/21
Bài viết
338
Được thích
139
Chào các bạn, làm phiền các bạn giúp đỡ viết giúp câu lệnh in bằng macro để làm sao khi nhập số bản cần in vào D2 thì các mã số sẽ in ra không trùng lặp ở mỗi bản và có số thứ tự tăng dần 1 đơn vị như ảnh dưới.
Xin cảm ơn
1644329823044.png
 
PHP:
Option Explicit

Sub vidu()
    'set formulas:  '
    'I6 = E6 + 1    '
    'M6 = E6 + 2    '
    '...to M24      '
    'set print area B4:N29'
    Const sCellNumPrint = "D2"
    Const sCellRef = "E6"
    Const numStep = 12
    Dim ws As Worksheet, numPrint As Long, i As Long
    Set ws = Sheet1
    numPrint = VBA.Val(ws.Range(sCellNumPrint).Value2)
    If numPrint = 0 Then Exit Sub
    For i = 1 To numPrint
        ws.Range(sCellRef).Value = 1 + numStep*(i-1)
        'ws.PrintPreview     'preview
        ws.PrintOut         'print
    Next i
End Sub
 
PHP:
Option Explicit

Sub vidu()
    'set formulas:  '
    'I6 = E6 + 1    '
    'M6 = E6 + 2    '
    '...to M24      '
    'set print area B4:N29'
    Const sCellNumPrint = "D2"
    Const sCellRef = "E6"
    Const numStep = 12
    Dim ws As Worksheet, numPrint As Long, i As Long
    Set ws = Sheet1
    numPrint = VBA.Val(ws.Range(sCellNumPrint).Value2)
    If numPrint = 0 Then Exit Sub
    For i = 1 To numPrint
        ws.Range(sCellRef).Value = i + numStep
        'ws.PrintPreview     'preview
        ws.PrintOut         'print
    Next i
End Sub
Cảm ơn bạn ,đầu xuân chúc bạn sức khỏe và thành công.
 
PHP:
Option Explicit

Sub vidu()
    'set formulas:  '
    'I6 = E6 + 1    '
    'M6 = E6 + 2    '
    '...to M24      '
    'set print area B4:N29'
    Const sCellNumPrint = "D2"
    Const sCellRef = "E6"
    Const numStep = 12
    Dim ws As Worksheet, numPrint As Long, i As Long
    Set ws = Sheet1
    numPrint = VBA.Val(ws.Range(sCellNumPrint).Value2)
    If numPrint = 0 Then Exit Sub
    For i = 1 To numPrint
        ws.Range(sCellRef).Value = 1 + numStep*(i-1)
        'ws.PrintPreview     'preview
        ws.PrintOut         'print
    Next i
End Sub
Khi in tiếp lần nữa mà không nhập lại E6 thì sẽ lại bị trùng, có thể xử lý để sau mỗi lần in E6 tự lấy giá trị tiếp theo được không bạn?
 
Được quá đi chứ.
Bạn cần 2 ô ghi số từ đâu tới đâu.
Có 1 ô E6 thì chỉ được thế thôi.
2 ô ghi số là: ô A1 = 1000 và ô A2=2000
Số chỉ chạy trong khoảng này, có 1000 số như vậy và mình in hàng ngày mỗi ngày một ít và nhiểu lần trên ngày v.v..

Vậy có thể mỗi lần mở file lên in là số có thể tiếp tục với lần in sau cùng trước đó mà không trùng lặp với các lần in trước cứ vậy cho đến 2000 thì thông báo hết số như vậy được không bạn, nếu được bạn code giúp mình với.
Cảm ơn bạn
 
Lần chỉnh sửa cuối:
Vậy có thể mỗi lần mở file lên in là số có thể tiếp tục với lần in sau cùng trước đó mà không trùng lặp với các lần in trước cứ vậy cho đến 2000 thì thông báo hết số như vậy được không bạn, nếu được bạn code giúp mình với.
Cảm ơn bạn
Mục đích là để làm gì mà phải kiểm soát số như thế vậy bạn
 
PHP:
Option Explicit

Sub vidu()
    'set formulas:  '
    'I6 = E6 + 1    '
    'M6 = E6 + 2    '
    '...to M24      '
    'set print area B4:N29'
    Const sCellNumPrint = "D2"
    Const sCellRef = "E6"
    Const numStep = 12
    Const sCellNoStart = "A1"
    Const sCellNoEnd = "A2"
    Const sCellNoLast = "A3"
    Dim ws As Worksheet, numPrint As Long, i As Long
    Dim noStart As Long, noEnd As Long, noLast As Long
    Set ws = Sheet1
    numPrint = VBA.Val(ws.Range(sCellNumPrint).Value2)
    noStart = VBA.Val(ws.Range(sCellNoStart).Value2)
    noEnd = VBA.Val(ws.Range(sCellNoEnd).Value2)
    noLast = VBA.Val(ws.Range(sCellNoLast).Value2)
    If numPrint = 0 Then Exit Sub
    If noLast < noStart Then noLast = noStart - 1
    For i = 1 To numPrint
        noLast = noLast + 1
        ws.Range(sCellRef).Value = noLast
        'ws.PrintPreview     'preview
        ws.PrintOut         'print
        noLast = noLast + numStep
        ws.Range(sCellNoLast).Value = noLast
        If noLast > noEnd Then
            MsgBox "Het so!", vbInformation
            Exit Sub
        End If
    Next i
End Sub
 
PHP:
Option Explicit

Sub vidu()
    'set formulas:  '
    'I6 = E6 + 1    '
    'M6 = E6 + 2    '
    '...to M24      '
    'set print area B4:N29'
    Const sCellNumPrint = "D2"
    Const sCellRef = "E6"
    Const numStep = 12
    Const sCellNoStart = "A1"
    Const sCellNoEnd = "A2"
    Const sCellNoLast = "A3"
    Dim ws As Worksheet, numPrint As Long, i As Long
    Dim noStart As Long, noEnd As Long, noLast As Long
    Set ws = Sheet1
    numPrint = VBA.Val(ws.Range(sCellNumPrint).Value2)
    noStart = VBA.Val(ws.Range(sCellNoStart).Value2)
    noEnd = VBA.Val(ws.Range(sCellNoEnd).Value2)
    noLast = VBA.Val(ws.Range(sCellNoLast).Value2)
    If numPrint = 0 Then Exit Sub
    If noLast < noStart Then noLast = noStart - 1
    For i = 1 To numPrint
        noLast = noLast + 1
        ws.Range(sCellRef).Value = noLast
        'ws.PrintPreview     'preview
        ws.PrintOut         'print
        noLast = noLast + numStep
        ws.Range(sCellNoLast).Value = noLast
        If noLast > noEnd Then
            MsgBox "Het so!", vbInformation
            Exit Sub
        End If
    Next i
End Sub
Cảm ơn bạn nhiều nhiều
 
Web KT
Back
Top Bottom