Mrrrr's Forum (VIEW ONLY)
Un forum care ofera solutii pentru unele probleme legate in general de PC. Pe langa solutii, aici puteti gasi si alte lucruri interesante // A forum that offers solutions to some PC related issues. Besides these, here you can find more interesting stuff.
Lista Forumurilor Pe Tematici
Mrrrr's Forum (VIEW ONLY) | Reguli | Inregistrare | Login

POZE MRRRR'S FORUM (VIEW ONLY)

Nu sunteti logat.
Nou pe simpatie:
Tanya321
Femeie
24 ani
Giurgiu
cauta Barbat
24 - 63 ani
Mrrrr's Forum (VIEW ONLY) / Tutoriale si Ghiduri Utile // Tutorials and useful guides / [EXCEL] Macro to Format a Table, Add Borders, Conditional Format [VBA] Moderat de TRaP, TonyTzu
Autor
Mesaj Pagini: 1
TonyTzu
Moderator

Inregistrat: acum 12 ani
Postari: 252
With this macro you format a table ranging from columns A to E, maximum rows of 279. You modify the column dimensions, conditional format columns A and B to hide any duplicate values except for their first appearance, modify the content of cell E1, make page formatting for printing including print table header on each page, add table borders for all cells with content and open the print preview dialog.

You have options to print landscape, including to increase font size and change column sizes for landscape printing. They are in comment lines.

Code:

Sub Listare()

Application.ScreenUpdating = False
'
' Macro to format page for printing ------ GO AT THE BOTTOM TO CHOOSE IF YOU WANT TO PRINT DIRECTLY OR PREVIEW FIRST

' ################################################## -- LANDSCAPE / PORTRAIT -- ##################################################

' Choose between landscape and portrait format (add ' in front of the line that you don't want to use and remove it from the other line)
'    With ActiveSheet.PageSetup
'        .Orientation = xlPortrait                                       ' portrait
'        '.Orientation = xlLandscape                                      ' landscape
'    End With

' ################################################################################################################################

' In cells E1 and D1 the values must be changed. My table will always be the same: 5 columns, starting from A1 to E1, variable
' length but never longer than about 250, always presorted ascending by column B
' Leaves 3 rows at the top (you can change this from page formatting obviously)
Range("A1").EntireRow.Insert
Range("A1").EntireRow.Insert
Range("A1").EntireRow.Insert
    Range("E4").Select
    ActiveCell.FormulaR1C1 = "Um"
    ActiveCell.WrapText = False
    Range("D4").Select
    ActiveCell.FormulaR1C1 = "Cant"
    ActiveCell.WrapText = False

' Page setup: A4, Portrait, Center horizontally, Zoom 100%.
    With ActiveSheet.PageSetup
        .Orientation = xlPortrait
        .LeftMargin = Application.CentimetersToPoints(0.196850393700787)
        .RightMargin = Application.CentimetersToPoints(0.196850393700787)
        .TopMargin = Application.CentimetersToPoints(0.393700787401575)
        .BottomMargin = Application.CentimetersToPoints(0.393700787401575)
        .HeaderMargin = Application.CentimetersToPoints(0.393700787401575)
        .FooterMargin = Application.CentimetersToPoints(0.393700787401575)
        .CenterHorizontally = True
        .PaperSize = xlPaperA4
        .Zoom = 100
        .PrintTitleRows = "$4:4"
    End With

' Replace DEDEMAN with DED
    ActiveSheet.Columns("B").Replace _
    What:="DEDEMAN", Replacement:="DED", _
    SearchOrder:=xlByColumns, MatchCase:=True

' Replace MAGAZIN with MAG
    ActiveSheet.Columns("B").Replace _
    What:="MAGAZIN", Replacement:="Mag", _
    SearchOrder:=xlByColumns, MatchCase:=True
    
' Replace PRAKTIKER with PRK
    ActiveSheet.Columns("B").Replace _
    What:="PRAKTIKER", Replacement:="PRK", _
    SearchOrder:=xlByColumns, MatchCase:=True
    
' Replace CONSTANTA with C-TA
    ActiveSheet.Columns("B").Replace _
    What:="CONSTANTA", Replacement:="C-TA", _
    SearchOrder:=xlByColumns, MatchCase:=True
    
' Replace BUCURESTI with BUC
    ActiveSheet.Columns("B").Replace _
    What:="BUCURESTI", Replacement:="BUC", _
    SearchOrder:=xlByColumns, MatchCase:=True
    
' Replace BUC with BC
    ActiveSheet.Columns("E").Replace _
    What:="BUC", Replacement:="buc", _
    SearchOrder:=xlByColumns, MatchCase:=True

'Remove grey fill from row 1
  Range("A4:E4").EntireRow.Interior.ColorIndex = 0

'Delete duplicate entries in row 5, Apply Red fill to Row of remaining entry
For rw = 279 To 5 Step -1
 If Cells(rw, 1) = Cells(rw - 1, 1) Then
  Cells(rw, 1).ClearContents
 End If
Next

'Delete duplicate entries in row 5, Apply Red fill to Row of remaining entry
For rw = 279 To 5 Step -1
 If Cells(rw, 2) = Cells(rw - 1, 2) Then
  Cells(rw, 2).ClearContents
 Else: Cells(rw, 2).EntireRow.Interior.ColorIndex = 24
 End If
Next

    If ActiveSheet.PageSetup.Orientation = xlPortrait Then
' Set column sizes for PORTRAIT pages                               PORTRAIT column sizes
    Columns("A:A").Select
    Selection.ColumnWidth = 11
    Columns("B:B").Select
    Selection.ColumnWidth = 23
    Columns("C:C").Select
    Selection.ColumnWidth = 45
    Columns("D:D").Select
    Selection.ColumnWidth = 14
    Columns("E:E").Select
    Selection.ColumnWidth = 4
    End If
'    Else
' Set column sizes for LANDSCAPE pages                                LANDSCAPE column sizes
'    Columns("A:A").Select
'    Selection.ColumnWidth = 14
'    Columns("B:B").Select
'   Selection.ColumnWidth = 42
'    Columns("C:C").Select
'    Selection.ColumnWidth = 60
'    Columns("D:D").Select
'    Selection.ColumnWidth = 20
'    Columns("E:E").Select
'    Selection.ColumnWidth = 7
'    End If
    
' Conditional formatting font face and font size for LANDSCAPE page
' arial font by default; if you want other, add line Cell.Font.Name = "Font Name"
'    For Each Cell In ActiveSheet.UsedRange
'      If ActiveSheet.PageSetup.Orientation = xlPortrait Then
'          Cell.Font.Size = 14                                                 ' font size for PORTRAIT
'          Cell.Font.Name = "Tahoma"
'      Else
'          Cell.Font.Size = 16                                                 ' font size for LANDSCAPE
'          Cell.Font.Name = "Tahoma"
'      End If
'    Next Cell

Range("A:A").Font.Size = 13
Range("A:A").Font.Name = "Tahoma"
Range("B:B").Font.Size = 12
Range("B:B").Font.Name = "Tahoma"
Range("C:C").Font.Size = 13
Range("C:C").Font.Name = "Tahoma"
Range("D:D").Font.Size = 13
Range("D:D").Font.Name = "Arial Narrow"
Range("E:E").Font.Size = 13
Range("E:E").Font.Name = "Arial Narrow"
Range("B:B").WrapText = True

' Add table borders for all cells with content.
    With ActiveSheet.UsedRange.Borders
        .LineStyle = xlContinuous
        .ColorIndex = xlAutomatic
        .Weight = xlThin
    End With

' ######################################################### -- PRINTING -- #########################################################

' Print preview or print directly (add ' in front of the line that you don't want to use and remove it from the other line)
'        Worksheets("Sheet1").PrintPreview                          ' PRINT PREVIEW FIRST
        ActiveSheet.PrintOut                                               ' PRINT IMMEDIATELY ON DEFAULT PRINTER
        
' ###################################################################################################################################

        Range("A1").Select
        
Application.ScreenUpdating = True

End Sub



pus acum 6 ani
   
TonyTzu
Moderator

Inregistrat: acum 12 ani
Postari: 252
Updated: 08.02.2018
Tested: in office


pus acum 6 ani
   
TonyTzu
Moderator

Inregistrat: acum 12 ani
Postari: 252
Updated: 09.02.2018
Tested: on site of use


pus acum 6 ani
   
TonyTzu
Moderator

Inregistrat: acum 12 ani
Postari: 252
Modified 12.02.2018
- Switched columns D and E
- Made text smaller
- Set .CenterHorizontally = False
- Added another blank column to the right (but with lines for table)

Code:

Sub Listare()

Application.ScreenUpdating = False
'
' Macro to format page for printing ------ GO AT THE BOTTOM TO CHOOSE IF YOU WANT TO PRINT DIRECTLY OR PREVIEW FIRST

' ################################################## -- LANDSCAPE / PORTRAIT -- ##################################################

' Choose between landscape and portrait format (add ' in front of the line that you don't want to use and remove it from the other line)
'    With ActiveSheet.PageSetup
'        .Orientation = xlPortrait                                       ' portrait
'        '.Orientation = xlLandscape                                      ' landscape
'    End With

' ################################################################################################################################

' In cells E1 and D1 the values must be changed. My table will always be the same: 5 columns, starting from A1 to E1, variable
' length but never longer than about 250, always presorted ascending by column B
Range("A1").EntireRow.Insert
Range("A1").EntireRow.Insert
Range("A1").EntireRow.Insert
    Range("E4").Select
    ActiveCell.FormulaR1C1 = "Um"
    ActiveCell.WrapText = False
    Range("D4").Select
    ActiveCell.FormulaR1C1 = "Cant"
    ActiveCell.WrapText = False

' Page setup: A4, Portrait, Center horizontally, Zoom 100%.
    With ActiveSheet.PageSetup
        .Orientation = xlPortrait
        .LeftMargin = Application.CentimetersToPoints(0.196850393700787)
        .RightMargin = Application.CentimetersToPoints(0.196850393700787)
        .TopMargin = Application.CentimetersToPoints(0.393700787401575)
        .BottomMargin = Application.CentimetersToPoints(0.393700787401575)
        .HeaderMargin = Application.CentimetersToPoints(0.393700787401575)
        .FooterMargin = Application.CentimetersToPoints(0.393700787401575)
        .CenterHorizontally = False
        .PaperSize = xlPaperA4
        .Zoom = 100
        .PrintTitleRows = "$4:4"
    End With

' Replace DEDEMAN with DED
    ActiveSheet.Columns("B").Replace _
    What:="DEDEMAN", Replacement:="DED", _
    SearchOrder:=xlByColumns, MatchCase:=True

' Replace MAGAZIN with MAG
    ActiveSheet.Columns("B").Replace _
    What:="MAGAZIN", Replacement:="Mag", _
    SearchOrder:=xlByColumns, MatchCase:=True
    
' Replace PRAKTIKER with PRK
    ActiveSheet.Columns("B").Replace _
    What:="PRAKTIKER", Replacement:="PRK", _
    SearchOrder:=xlByColumns, MatchCase:=True
    
' Replace CONSTANTA with C-TA
    ActiveSheet.Columns("B").Replace _
    What:="CONSTANTA", Replacement:="C-TA", _
    SearchOrder:=xlByColumns, MatchCase:=True
    
' Replace BUCURESTI with BUC
    ActiveSheet.Columns("B").Replace _
    What:="BUCURESTI", Replacement:="BUC", _
    SearchOrder:=xlByColumns, MatchCase:=True
    
' Replace BUC with BC
    ActiveSheet.Columns("E").Replace _
    What:="BUC", Replacement:="buc", _
    SearchOrder:=xlByColumns, MatchCase:=True

'Remove grey fill from row 1
  Range("A4:E4").EntireRow.Interior.ColorIndex = 0

'Delete duplicate entries in row 5, Apply Red fill to Row of remaining entry
For rw = 279 To 5 Step -1
 If Cells(rw, 1) = Cells(rw - 1, 1) Then
  Cells(rw, 1).ClearContents
 End If
Next

'Delete duplicate entries in row 5, Apply Red fill to Row of remaining entry
For rw = 279 To 5 Step -1
 If Cells(rw, 2) = Cells(rw - 1, 2) Then
  Cells(rw, 2).ClearContents
 Else: Cells(rw, 2).EntireRow.Interior.ColorIndex = 24
 End If
Next

    If ActiveSheet.PageSetup.Orientation = xlPortrait Then
' Set column sizes for PORTRAIT pages                               PORTRAIT column sizes
    Columns("A:A").Select
    Selection.ColumnWidth = 10
    Columns("B:B").Select
    Selection.ColumnWidth = 20
    Columns("C:C").Select
    Selection.ColumnWidth = 45
    Columns("D:D").Select
    Selection.ColumnWidth = 10
    Columns("E:E").Select
    Selection.ColumnWidth = 4
    End If
    
With ActiveSheet
Range("E1").EntireColumn.Insert
Columns("F").Cut
Columns("E").Insert Shift:=xlToRight
Columns("D").Cut
Columns("F").Insert Shift:=xlToRight
End With

With ActiveSheet
    Columns("F:F").Select
    Selection.ColumnWidth = 9
End With

'    Else
' Set column sizes for LANDSCAPE pages                                LANDSCAPE column sizes
'    Columns("A:A").Select
'    Selection.ColumnWidth = 14
'    Columns("B:B").Select
'   Selection.ColumnWidth = 42
'    Columns("C:C").Select
'    Selection.ColumnWidth = 60
'    Columns("D:D").Select
'    Selection.ColumnWidth = 20
'    Columns("E:E").Select
'    Selection.ColumnWidth = 7
'    End If
    
' Conditional formatting font face and font size for LANDSCAPE page
' arial font by default; if you want other, add line Cell.Font.Name = "Font Name"
'    For Each Cell In ActiveSheet.UsedRange
'      If ActiveSheet.PageSetup.Orientation = xlPortrait Then
'          Cell.Font.Size = 14                                                 ' font size for PORTRAIT
'          Cell.Font.Name = "Tahoma"
'      Else
'          Cell.Font.Size = 16                                                 ' font size for LANDSCAPE
'          Cell.Font.Name = "Tahoma"
'      End If
'    Next Cell

Range("A:A").Font.Size = 11
Range("A:A").Font.Name = "Arial Narrow"
Range("B:B").Font.Size = 11
Range("B:B").Font.Name = "Tahoma"
Range("C:C").Font.Size = 11
Range("C:C").Font.Name = "Tahoma"
Range("D:D").Font.Size = 11
Range("D:D").Font.Name = "Arial Narrow"
Range("E:E").Font.Size = 11
Range("E:E").Font.Name = "Arial Narrow"
Range("B:B").WrapText = True

' Add table borders for all cells with content.
    With ActiveSheet.UsedRange.Borders
        .LineStyle = xlContinuous
        .ColorIndex = xlAutomatic
        .Weight = xlThin
    End With

' ######################################################### -- PRINTING -- #########################################################

' Print preview or print directly (add ' in front of the line that you don't want to use and remove it from the other line)
'        Worksheets("Sheet1").PrintPreview               ' PRINT PREVIEW FIRST
       ActiveSheet.PrintOut                             ' PRINT IMMEDIATELY ON DEFAULT PRINTER
        
' ###################################################################################################################################

        Range("A1").Select
        
Application.ScreenUpdating = True

End Sub



pus acum 6 ani
   
TonyTzu
Moderator

Inregistrat: acum 12 ani
Postari: 252
Update: 13.02.2018
Where: in office

What changed:
- removed the change where it switched columns D and E (this will be made from the exporting software - SAP)
- left the column insert part in place
- sorting has to be done from SAP layout by client then by product (columns B then C) so that if a client has the same product in multiple deliveries, they become consecutive in the list (previously, the list was sorted by client then by delivery no, by columns B then A)
- made a part where consecutive products that are the same for the same client, become bold - only problem with emboldening might be if 2 clients are next to each other and they both have the same product, eg:
..Client A - Product X
..Client A - Product Y
..Client A - Product Z
..Client B - Product Z

But as the client changes also the color of the row changes to yellow, and 2 consecutive yellow lines mean 2 consecutive clients with at least 1 product each

Code:

Sub Listare() ' Macro to format page for printing ------ GO AT THE BOTTOM TO CHOOSE IF YOU WANT TO PRINT DIRECTLY OR PREVIEW FIRST

Application.ScreenUpdating = False

' In cells E1 and D1 the values must be changed. My table will always be the same: 5 columns, starting from A1 to E1, variable
' length but never longer than about 250, always presorted ascending by column B
    Range("A1").EntireRow.Insert
    Range("A1").EntireRow.Insert
    Range("A1").EntireRow.Insert
    Range("D4").Select
    ActiveCell.FormulaR1C1 = "Um"
    ActiveCell.WrapText = False
    Range("E4").Select
    ActiveCell.FormulaR1C1 = "Cant"
    ActiveCell.WrapText = False
    Range("E4").HorizontalAlignment = xlRight

' Page setup: A4, Portrait, Center horizontally, Zoom 100%.
    With ActiveSheet.PageSetup
        .Orientation = xlPortrait
        .LeftMargin = Application.CentimetersToPoints(0.196850393700787)
        .RightMargin = Application.CentimetersToPoints(0.196850393700787)
        .TopMargin = Application.CentimetersToPoints(0.393700787401575)
        .BottomMargin = Application.CentimetersToPoints(0.393700787401575)
        .HeaderMargin = Application.CentimetersToPoints(0.393700787401575)
        .FooterMargin = Application.CentimetersToPoints(0.393700787401575)
        .CenterHorizontally = False
        .PaperSize = xlPaperA4
        .Zoom = 100
        .PrintTitleRows = "$4:4"
    End With

' Replace DEDEMAN with DED
    ActiveSheet.Columns("B").Replace _
    What:="DEDEMAN", Replacement:="DED", _
    SearchOrder:=xlByColumns, MatchCase:=True

' Replace MAGAZIN with MAG
    ActiveSheet.Columns("B").Replace _
    What:="MAGAZIN", Replacement:="Mag", _
    SearchOrder:=xlByColumns, MatchCase:=True
    
' Replace PRAKTIKER with PRK
    ActiveSheet.Columns("B").Replace _
    What:="PRAKTIKER", Replacement:="PRK", _
    SearchOrder:=xlByColumns, MatchCase:=True
    
' Replace CONSTANTA with C-TA
    ActiveSheet.Columns("B").Replace _
    What:="CONSTANTA", Replacement:="C-TA", _
    SearchOrder:=xlByColumns, MatchCase:=True
    
' Replace BUCURESTI with BUC
    ActiveSheet.Columns("B").Replace _
    What:="BUCURESTI", Replacement:="BUC", _
    SearchOrder:=xlByColumns, MatchCase:=True
    
' Replace BUC with BC
    ActiveSheet.Columns("D").Replace _
    What:="BUC", Replacement:="buc", _
    SearchOrder:=xlByColumns, MatchCase:=True

'Remove grey fill from row 1
  Range("A4:E4").EntireRow.Interior.ColorIndex = 0

'Delete duplicate entries in row 5, Apply Red fill to Row of remaining entry
For rw = 279 To 5 Step -1
 If Cells(rw, 1) = Cells(rw - 1, 1) Then
  Cells(rw, 1).ClearContents
 End If
Next

'Delete duplicate entries in row 5, Apply Red fill to Row of remaining entry
For rw = 279 To 5 Step -1
 If Cells(rw, 2) = Cells(rw - 1, 2) Then
  Cells(rw, 2).ClearContents
 Else: Cells(rw, 2).EntireRow.Interior.ColorIndex = 6
 End If
Next

    If ActiveSheet.PageSetup.Orientation = xlPortrait Then
' Set column sizes for PORTRAIT pages                               PORTRAIT column sizes
    Columns("A:A").Select
    Selection.ColumnWidth = 10
    Columns("B:B").Select
    Selection.ColumnWidth = 20
    Columns("C:C").Select
    Selection.ColumnWidth = 45
    Columns("D:D").Select
    Selection.ColumnWidth = 4
    Columns("E:E").Select
    Selection.ColumnWidth = 10
    End If
    
With ActiveSheet
'Range("E1").EntireColumn.Insert
'Columns("F").Cut
'Columns("E").Insert Shift:=xlToRight
'Columns("D").Cut
Columns("F").Insert Shift:=xlToRight
End With

With ActiveSheet
    Columns("F:F").Select
    Selection.ColumnWidth = 9
End With

'    Else
' Set column sizes for LANDSCAPE pages                                LANDSCAPE column sizes
'    Columns("A:A").Select
'    Selection.ColumnWidth = 14
'    Columns("B:B").Select
'    Selection.ColumnWidth = 42
'    Columns("C:C").Select
'    Selection.ColumnWidth = 60
'    Columns("D:D").Select
'    Selection.ColumnWidth = 20
'    Columns("E:E").Select
'    Selection.ColumnWidth = 7
'    End If
    
' Conditional formatting font face and font size for LANDSCAPE page
' arial font by default; if you want other, add line Cell.Font.Name = "Font Name"
'    For Each Cell In ActiveSheet.UsedRange
'      If ActiveSheet.PageSetup.Orientation = xlPortrait Then
'          Cell.Font.Size = 14                                                 ' font size for PORTRAIT
'          Cell.Font.Name = "Tahoma"
'      Else
'          Cell.Font.Size = 16                                                 ' font size for LANDSCAPE
'          Cell.Font.Name = "Tahoma"
'      End If
'    Next Cell

Range("A:A").Font.Size = 11 '13
Range("A:A").Font.Name = "Arial Narrow" 'Tahoma
Range("B:B").Font.Size = 11 '12
Range("B:B").Font.Name = "Arial Narrow" 'Tahoma
Range("C:C").Font.Size = 11 '13
Range("C:C").Font.Name = "Tahoma"
Range("D:D").Font.Size = 11 '13
Range("D:D").Font.Name = "Arial Narrow"
Range("E:E").Font.Size = 11 '13
Range("E:E").Font.Name = "Arial Narrow"
Range("B:B").WrapText = True

' Add table borders for all cells with content.
    With ActiveSheet.UsedRange.Borders
        .LineStyle = xlContinuous
        .ColorIndex = xlAutomatic
        .Weight = xlThin
    End With

'If 2 consecutive entries are the same in column 3, bold them
For rw = 279 To 5 Step -1
 If Cells(rw, 3) = Cells(rw - 1, 3) Then
  Cells(rw, 3).Font.Bold = True
  Cells(rw - 1, 3).Font.Bold = True
 End If
Next

' ######################################################### -- PRINTING -- #########################################################

' Print preview or print directly (add ' in front of the line that you don't want to use and remove it from the other line)
'        Worksheets("Sheet1").PrintPreview               ' PRINT PREVIEW FIRST
'        ActiveSheet.PrintOut                            ' PRINT IMMEDIATELY ON DEFAULT PRINTER
        
' ###################################################################################################################################
        
        Range("A1").Select
        
Application.ScreenUpdating = True

End Sub



pus acum 6 ani
   
TonyTzu
Moderator

Inregistrat: acum 12 ani
Postari: 252
COMMA

Code:

Sub Listare() ' Macro to format page for printing ------ GO AT THE BOTTOM TO CHOOSE IF YOU WANT TO PRINT DIRECTLY OR PREVIEW FIRST

Application.ScreenUpdating = False

Dim LastRow As Long

With ActiveSheet
LastRow = .Cells(Rows.Count, "C").End(xlUp).Row

' In cells E1 and D1 the values must be changed. My table will always be the same: 5 columns, starting from A1 to E1, variable
' length but never longer than about 250, always presorted ascending by column B
    Range("A1").EntireRow.Insert
    Range("A1").EntireRow.Insert
    Range("A1").EntireRow.Insert
    Range("D4").Select
    ActiveCell.FormulaR1C1 = "Um"
    ActiveCell.WrapText = False
    Range("E4").Select
    ActiveCell.FormulaR1C1 = "Cant"
    ActiveCell.WrapText = False
    Range("E4").HorizontalAlignment = xlRight
    Columns("F").Insert shift:=xlToRight
    Columns("F").ColumnWidth = 9
  
' Replace BUC with buc
    ActiveSheet.Columns("D").Replace _
    What:="BUC", Replacement:="buc", _
    SearchOrder:=xlByColumns, MatchCase:=True

'Remove grey fill from row 1
  'Range("A4:F4").Interior.ColorIndex = 0

'Delete duplicate entries in column 1
For rw = 279 To 5 Step -1
 If Cells(rw, 1) = Cells(rw - 1, 1) Then
  Cells(rw, 1).ClearContents
 End If
Next

    Columns("A").ColumnWidth = 10
'    Columns("B").ColumnWidth = 20
'    Columns("C").ColumnWidth = 43
    Columns("D").ColumnWidth = 4
    Columns("E").ColumnWidth = 10

Range("A4:A" & LastRow).Font.Size = 11 '13
Range("A4:A" & LastRow).Font.Name = "Arial Narrow" 'Tahoma
Range("B4:B" & LastRow).Font.Size = 11 '12
Range("B4:B" & LastRow).Font.Name = "Arial Narrow" 'Tahoma
Range("C:C").Font.Size = 11 '13
Range("C:C").Font.Name = "Tahoma"
Range("D4:D" & LastRow).Font.Size = 11 '13
Range("D4:D" & LastRow).Font.Name = "Arial Narrow"
Range("E4:E" & LastRow).Font.Size = 11 '13
Range("E4:E" & LastRow).Font.Name = "Arial Narrow"

End With

With ActiveSheet

' FIND EAN CODE FOR MATERIALS ------------------------- TONY VERSION
' Below code is to find EAN code into another excel file (export of ZNOMENCLATOR tcode from SAP), then merge it to
' column C next to the product name

'LastRow = ActiveSheet.Cells(ActiveSheet.Rows.Count, "G").End(xlUp).Row
'    Range("H5").Formula = "=INDEX([znomenclator.XLSX]Sheet1!$G:$G,MATCH(G5,[znomenclator.XLSX]Sheet1!$A:$A,0))"
'    Range("H5").AutoFill Destination:=Range("H5:H" & LastRow)
'    Range("I5").Formula = "=IF(ISNA(H5),C5,IF(H5="""",C5,C5&"" --- ""&H5))"
'    Range("I5").AutoFill Destination:=Range("I5:I" & LastRow)

'Range("C5:C" & LastRow).Formula = Range("I5:I" & LastRow).Value
'Columns("G:M").EntireColumn.Delete

' FIND EAN CODE FOR MATERIALS ------------------------- DERBYDAD VERSION
' Below code is to find EAN code into another excel file (export of ZNOMENCLATOR tcode from SAP), then merge it to
' column C next to the product name

LastRow = ActiveSheet.Cells(ActiveSheet.Rows.Count, "G").End(xlUp).Row
    Range("H5:H" & LastRow).Formula = _
        "=IFERROR(INDEX([znomenclator.XLSX]Sheet1!$G:$G,MATCH(G5,[znomenclator.XLSX]Sheet1!$A:$A,0)),"""")"
    Range("I5:I" & LastRow).Formula = "=IF(H5=0,C5,C5&"" --- ""&H5)"

Range("C5:C" & LastRow) = Range("I5:I" & LastRow).Value
Range("G:I").EntireColumn.Delete

' CHANGE COLUMN C TITLE
    Range("C4").Select
    ActiveCell.FormulaR1C1 = "Denumire produs --- EAN"
    ActiveCell.WrapText = False
    
End With

' INSERT 1 ROW ABOVE EACH COMPANY NAME IN COLUMN B
With ActiveSheet
    Dim i, itotalrows As Integer
    Dim strRange As String
    itotalrows = ActiveSheet.Range("A300").End(xlUp).Offset(1, 0).Row
    Do While i <= itotalrows
        i = i + 1
        strRange = "B" & i
        strRange2 = "B" & i + 1
        If Range(strRange).Text <> Range(strRange2).Text Then
            Rows(i + 1).Insert
            itotalrows = ActiveSheet.Range("A300").End(xlUp).Offset(1, 0).Row
            i = i + 1
        End If
    Loop
Rows(1).EntireRow.Delete
End With

With ActiveSheet
' CLEAR DUPLICATES IN COLUMN B, EXCEPT FOR THEIR FIRST OCCURRENCE
For rw = 279 To 6 Step -1
 If Cells(rw, 2) = Cells(rw - 1, 2) Then
  Cells(rw, 2).ClearContents
 End If
Next

' COPY COMPANY NAME AS TITLE FOR EACH SET OF ROWS FOLLOWING IT
    Columns("B").Insert shift:=xlToRight
'    Columns("B").ColumnWidth = 1

LastRow = ActiveSheet.Cells(ActiveSheet.Rows.Count, "D").End(xlUp).Row
    Range("H5").FormulaLocal = "=IF(C6<>"""",C6,"""")"
    Range("H5").AutoFill Destination:=Range("H5:H" & LastRow)
    Range("I5").FormulaLocal = "=IF(A5<>"""","""",H5)"
    Range("I5").AutoFill Destination:=Range("I5:I" & LastRow)
Range("B5:B" & LastRow) = Range("I5:I" & LastRow).Value
Columns("C:C").EntireColumn.Delete
Columns("G:H").EntireColumn.Delete

' MAKE COLUMN C AND COMPANY NAMES IN COLUMN B MORE VISIBLE
Range("B:B").Font.Size = 14 '13
Range("B:B").Font.Name = "Tahoma"
Range("B:B").Font.Bold = True
    Columns("B").ColumnWidth = 0.05
    Columns("C").ColumnWidth = 62
    Range("C:C").WrapText = True
    
End With

' Add table borders for all cells with content.
    With ActiveSheet.UsedRange.Borders
        .LineStyle = xlContinuous
        .ColorIndex = xlAutomatic
        .Weight = xlThin
    End With

' Page setup: A4, Portrait, Center horizontally, Zoom 100%.
    With ActiveSheet.PageSetup
        .Orientation = xlPortrait
        .LeftMargin = Application.CentimetersToPoints(0.296850393700787)
        .RightMargin = Application.CentimetersToPoints(0.296850393700787)
        .TopMargin = Application.CentimetersToPoints(0.393700787401575)
        .BottomMargin = Application.CentimetersToPoints(0.393700787401575)
        .HeaderMargin = Application.CentimetersToPoints(0.393700787401575)
        .FooterMargin = Application.CentimetersToPoints(0.393700787401575)
        .CenterHorizontally = True
        .PaperSize = xlPaperA4
        .Zoom = 100
        .PrintTitleRows = "$1:1"
         Range("A5:F5").Interior.ColorIndex = 0
    End With

' ######################################################### -- PRINTING -- #########################################################

' Print preview or print directly (add ' in front of the line that you don't want to use and remove it from the other line)
'        Worksheets("Sheet1").PrintPreview               ' PRINT PREVIEW FIRST
        ActiveSheet.PrintOut                            ' PRINT IMMEDIATELY ON DEFAULT PRINTER
        
' ###################################################################################################################################
               
        Range("A1").Select
        
Application.ScreenUpdating = True

End Sub



pus acum 6 ani
   
TonyTzu
Moderator

Inregistrat: acum 12 ani
Postari: 252
SEMICOLON

Code:

Sub Listare() ' Macro to format page for printing ------ GO AT THE BOTTOM TO CHOOSE IF YOU WANT TO PRINT DIRECTLY OR PREVIEW FIRST

Application.ScreenUpdating = False

Dim LastRow As Long

With ActiveSheet
LastRow = .Cells(Rows.Count, "C").End(xlUp).Row

' In cells E1 and D1 the values must be changed. My table will always be the same: 5 columns, starting from A1 to E1, variable
' length but never longer than about 250, always presorted ascending by column B
    Range("A1").EntireRow.Insert
    Range("A1").EntireRow.Insert
    Range("A1").EntireRow.Insert
    Range("D4").Select
    ActiveCell.FormulaR1C1 = "Um"
    ActiveCell.WrapText = False
    Range("E4").Select
    ActiveCell.FormulaR1C1 = "Cant"
    ActiveCell.WrapText = False
    Range("E4").HorizontalAlignment = xlRight
    Columns("F").Insert shift:=xlToRight
    Columns("F").ColumnWidth = 9
  
' Replace BUC with buc
    ActiveSheet.Columns("D").Replace _
    What:="BUC", Replacement:="buc", _
    SearchOrder:=xlByColumns, MatchCase:=True

'Remove grey fill from row 1
  'Range("A4:F4").Interior.ColorIndex = 0

'Delete duplicate entries in column 1
For rw = 279 To 5 Step -1
 If Cells(rw, 1) = Cells(rw - 1, 1) Then
  Cells(rw, 1).ClearContents
 End If
Next

    Columns("A").ColumnWidth = 10
'    Columns("B").ColumnWidth = 20
'    Columns("C").ColumnWidth = 43
    Columns("D").ColumnWidth = 4
    Columns("E").ColumnWidth = 10

Range("A4:A" & LastRow).Font.Size = 11 '13
Range("A4:A" & LastRow).Font.Name = "Arial Narrow" 'Tahoma
Range("B4:B" & LastRow).Font.Size = 11 '12
Range("B4:B" & LastRow).Font.Name = "Arial Narrow" 'Tahoma
Range("C:C").Font.Size = 11 '13
Range("C:C").Font.Name = "Tahoma"
Range("D4:D" & LastRow).Font.Size = 11 '13
Range("D4:D" & LastRow).Font.Name = "Arial Narrow"
Range("E4:E" & LastRow).Font.Size = 11 '13
Range("E4:E" & LastRow).Font.Name = "Arial Narrow"

End With

With ActiveSheet

' FIND EAN CODE FOR MATERIALS ------------------------- TONY VERSION
' Below code is to find EAN code into another excel file (export of ZNOMENCLATOR tcode from SAP), then merge it to
' column C next to the product name

'LastRow = ActiveSheet.Cells(ActiveSheet.Rows.Count, "G").End(xlUp).Row
'    Range("H5").Formula = "=INDEX([znomenclator.XLSX]Sheet1!$G:$G,MATCH(G5,[znomenclator.XLSX]Sheet1!$A:$A,0))"
'    Range("H5").AutoFill Destination:=Range("H5:H" & LastRow)
'    Range("I5").Formula = "=IF(ISNA(H5),C5,IF(H5="""",C5,C5&"" --- ""&H5))"
'    Range("I5").AutoFill Destination:=Range("I5:I" & LastRow)

'Range("C5:C" & LastRow).Formula = Range("I5:I" & LastRow).Value
'Columns("G:M").EntireColumn.Delete

' FIND EAN CODE FOR MATERIALS ------------------------- DERBYDAD VERSION
' Below code is to find EAN code into another excel file (export of ZNOMENCLATOR tcode from SAP), then merge it to
' column C next to the product name

LastRow = ActiveSheet.Cells(ActiveSheet.Rows.Count, "G").End(xlUp).Row
    Range("H5:H" & LastRow).FormulaLocal = _
        "=IFERROR(INDEX([znomenclator.XLSX]Sheet1!$G:$G;MATCH(G5;[znomenclator.XLSX]Sheet1!$A:$A;0));"""")"
    Range("I5:I" & LastRow).FormulaLocal = "=IF(H5=0;C5;C5&"" --- ""&H5)"

Range("C5:C" & LastRow) = Range("I5:I" & LastRow).Value
Range("G:I").EntireColumn.Delete

' CHANGE COLUMN C TITLE
    Range("C4").Select
    ActiveCell.FormulaR1C1 = "Denumire produs --- EAN"
    ActiveCell.WrapText = False
    
End With

' INSERT 1 ROW ABOVE EACH COMPANY NAME IN COLUMN B
With ActiveSheet
    Dim i, itotalrows As Integer
    Dim strRange As String
    itotalrows = ActiveSheet.Range("A300").End(xlUp).Offset(1, 0).Row
    Do While i <= itotalrows
        i = i + 1
        strRange = "B" & i
        strRange2 = "B" & i + 1
        If Range(strRange).Text <> Range(strRange2).Text Then
            Rows(i + 1).Insert
            itotalrows = ActiveSheet.Range("A300").End(xlUp).Offset(1, 0).Row
            i = i + 1
        End If
    Loop
Rows(1).EntireRow.Delete
End With

With ActiveSheet
' CLEAR DUPLICATES IN COLUMN B, EXCEPT FOR THEIR FIRST OCCURRENCE
For rw = 279 To 6 Step -1
 If Cells(rw, 2) = Cells(rw - 1, 2) Then
  Cells(rw, 2).ClearContents
 End If
Next

' COPY COMPANY NAME AS TITLE FOR EACH SET OF ROWS FOLLOWING IT
    Columns("B").Insert shift:=xlToRight
'    Columns("B").ColumnWidth = 1

LastRow = ActiveSheet.Cells(ActiveSheet.Rows.Count, "D").End(xlUp).Row
    Range("H5").FormulaLocal = "=IF(C6<>"""";C6;"""")"
    Range("H5").AutoFill Destination:=Range("H5:H" & LastRow)
    Range("I5").FormulaLocal = "=IF(A5<>"""";"""";H5)"
    Range("I5").AutoFill Destination:=Range("I5:I" & LastRow)
Range("B5:B" & LastRow) = Range("I5:I" & LastRow).Value
Columns("C:C").EntireColumn.Delete
Columns("G:H").EntireColumn.Delete

' MAKE COLUMN C AND COMPANY NAMES IN COLUMN B MORE VISIBLE
Range("B:B").Font.Size = 14 '13
Range("B:B").Font.Name = "Tahoma"
Range("B:B").Font.Bold = True
    Columns("B").ColumnWidth = 0.05
    Columns("C").ColumnWidth = 62
    Range("C:C").WrapText = True
    
End With

' Add table borders for all cells with content.
    With ActiveSheet.UsedRange.Borders
        .LineStyle = xlContinuous
        .ColorIndex = xlAutomatic
        .Weight = xlThin
    End With

' Page setup: A4, Portrait, Center horizontally, Zoom 100%.
    With ActiveSheet.PageSetup
        .Orientation = xlPortrait
        .LeftMargin = Application.CentimetersToPoints(0.296850393700787)
        .RightMargin = Application.CentimetersToPoints(0.296850393700787)
        .TopMargin = Application.CentimetersToPoints(0.393700787401575)
        .BottomMargin = Application.CentimetersToPoints(0.393700787401575)
        .HeaderMargin = Application.CentimetersToPoints(0.393700787401575)
        .FooterMargin = Application.CentimetersToPoints(0.393700787401575)
        .CenterHorizontally = True
        .PaperSize = xlPaperA4
        .Zoom = 100
        .PrintTitleRows = "$1:1"
         Range("A5:F5").Interior.ColorIndex = 0
    End With

' ######################################################### -- PRINTING -- #########################################################

' Print preview or print directly (add ' in front of the line that you don't want to use and remove it from the other line)
'        Worksheets("Sheet1").PrintPreview               ' PRINT PREVIEW FIRST
        ActiveSheet.PrintOut                            ' PRINT IMMEDIATELY ON DEFAULT PRINTER
        
' ###################################################################################################################################
               
        Range("A1").Select
        
Application.ScreenUpdating = True

End Sub



pus acum 6 ani
   
TRaP
Moderator

Inregistrat: acum 6 ani
Postari: 748
Add the code below to insert print date and time below the table on the printout.

Insert the line of code right before the line with ActiveSheet.PrintOut:


    Cells(LastRow + 3, 3).Value = "Date and time of printing: " & Date & " " & Time


pus acum 5 ani
   
TRaP
Moderator

Inregistrat: acum 6 ani
Postari: 748
Modified to keep material code too in last column.


Sub Listare() ' Macro to format page for printing ------ GO AT THE BOTTOM TO CHOOSE IF YOU WANT TO PRINT DIRECTLY OR PREVIEW FIRST

Application.ScreenUpdating = False

Dim Lastrow As Long

With ActiveSheet
Lastrow = .Cells(Rows.Count, "C").End(xlUp).Row

' In cells E1 and D1 the values must be changed. My table will always be the same: 5 columns, starting from A1 to E1, variable
' length but never longer than about 250, always presorted ascending by column B
    Range("A1").EntireRow.Insert
    Range("A1").EntireRow.Insert
    Range("A1").EntireRow.Insert
    Range("D4").Select
    ActiveCell.FormulaR1C1 = "Um"
    ActiveCell.WrapText = False
    Range("E4").Select
    ActiveCell.FormulaR1C1 = "Cant"
    ActiveCell.WrapText = False
    Range("E4").HorizontalAlignment = xlRight
    Columns("F").Insert Shift:=xlToRight
    Columns("F").ColumnWidth = 5
 
' Replace BUC with buc
    ActiveSheet.Columns("D").replace _
    what:="BUC", Replacement:="buc", _
    SearchOrder:=xlByColumns, MatchCase:=True

'Remove grey fill from row 1
  'Range("A4:F4").Interior.ColorIndex = 0

'Delete duplicate entries in column 1
For Rw = 279 To 5 Step -1
If Cells(Rw, 1) = Cells(Rw - 1, 1) Then
  Cells(Rw, 1).ClearContents
End If
Next

    Columns("A").ColumnWidth = 8.5
'    Columns("B").ColumnWidth = 20
'    Columns("C").ColumnWidth = 43
    Columns("D").ColumnWidth = 3
    Columns("E").ColumnWidth = 10

Range("A4:A" & Lastrow).Font.Size = 11 '13
Range("A4:A" & Lastrow).Font.Name = "Arial Narrow" 'Tahoma
Range("B4:B" & Lastrow).Font.Size = 11 '12
Range("B4:B" & Lastrow).Font.Name = "Arial Narrow" 'Tahoma
Range("C:C").Font.Size = 11 '13
Range("C:C").Font.Name = "Arial Narrow"
Range("D4:D" & Lastrow).Font.Size = 11 '13
Range("D4:D" & Lastrow).Font.Name = "Arial Narrow"
Range("E4:E" & Lastrow).Font.Size = 11 '13
Range("E4:E" & Lastrow).Font.Name = "Arial Narrow"

End With

With ActiveSheet

' FIND EAN CODE FOR MATERIALS ------------------------- TONY VERSION
' Below code is to find EAN code into another excel file (export of ZNOMENCLATOR tcode from SAP), then merge it to
' column C next to the product name

'LastRow = ActiveSheet.Cells(ActiveSheet.Rows.Count, "G").End(xlUp).Row
'    Range("H5").Formula = "=INDEX([znomenclator.XLSX]Sheet1!$G:$G,MATCH(G5,[znomenclator.XLSX]Sheet1!$A:$A,0))"
'    Range("H5").AutoFill Destination:=Range("H5:H" & LastRow)
'    Range("I5").Formula = "=IF(ISNA(H5),C5,IF(H5="""",C5,C5&"" --- ""&H5))"
'    Range("I5").AutoFill Destination:=Range("I5:I" & LastRow)

'Range("C5:C" & LastRow).Formula = Range("I5:I" & LastRow).Value
'Columns("G:M").EntireColumn.Delete

' FIND EAN CODE FOR MATERIALS ------------------------- DERBYDAD VERSION
' Below code is to find EAN code into another excel file (export of ZNOMENCLATOR tcode from SAP), then merge it to
' column C next to the product name

Lastrow = ActiveSheet.Cells(ActiveSheet.Rows.Count, "G").End(xlUp).Row
    Range("H5:H" & Lastrow).FormulaLocal = _
        "=IFERROR(INDEX([znomenclator.XLSX]Sheet1!$G:$G;MATCH(G5;[znomenclator.XLSX]Sheet1!$A:$A;0));"""")"
    Range("I5:I" & Lastrow).FormulaLocal = "=IF(H5=0;C5;C5&"" --- ""&H5)"

Range("C5:C" & Lastrow) = Range("I5:I" & Lastrow).Value
Range("H:I").EntireColumn.Delete

' CHANGE COLUMN C TITLE
    Range("C4").Select
    ActiveCell.FormulaR1C1 = "Denumire produs --- EAN"
    ActiveCell.WrapText = False
   
End With

' INSERT 1 ROW ABOVE EACH COMPANY NAME IN COLUMN B
With ActiveSheet
    Dim i, itotalrows As Integer
    Dim strRange As String
    itotalrows = ActiveSheet.Range("A300").End(xlUp).Offset(1, 0).Row
    Do While i <= itotalrows
        i = i + 1
        strRange = "B" & i
        strRange2 = "B" & i + 1
        If Range(strRange).Text <> Range(strRange2).Text Then
            Rows(i + 1).Insert
            itotalrows = ActiveSheet.Range("A300").End(xlUp).Offset(1, 0).Row
            i = i + 1
        End If
    Loop
Rows(1).EntireRow.Delete
End With

With ActiveSheet
' CLEAR DUPLICATES IN COLUMN B, EXCEPT FOR THEIR FIRST OCCURRENCE
For Rw = 279 To 6 Step -1
If Cells(Rw, 2) = Cells(Rw - 1, 2) Then
  Cells(Rw, 2).ClearContents
End If
Next

' COPY COMPANY NAME AS TITLE FOR EACH SET OF ROWS FOLLOWING IT
    Columns("B").Insert Shift:=xlToRight
'    Columns("B").ColumnWidth = 1

Lastrow = ActiveSheet.Cells(ActiveSheet.Rows.Count, "D").End(xlUp).Row
    Range("I5").FormulaLocal = "=IF(C6<>"""";C6;"""")"
    Range("I5").AutoFill destination:=Range("I5:I" & Lastrow)
    Range("J5").FormulaLocal = "=IF(A5<>"""";"""";H5)"
    Range("J5").AutoFill destination:=Range("J5:J" & Lastrow)
Range("B5:B" & Lastrow) = Range("I5:I" & Lastrow).Value
Columns("C:C").EntireColumn.Delete
Columns("H:I").EntireColumn.Delete

' MAKE COLUMN C AND COMPANY NAMES IN COLUMN B MORE VISIBLE
Range("B:B").Font.Size = 14 '13
Range("B:B").Font.Name = "Tahoma"
Range("B:B").Font.Bold = True
    Columns("B").ColumnWidth = 0.05
    Columns("C").ColumnWidth = 50
    Range("C:C").WrapText = True
   
End With

' Add table borders for all cells with content.
    With ActiveSheet.UsedRange.Borders
        .LineStyle = xlContinuous
        .ColorIndex = xlAutomatic
        .Weight = xlThin
    End With

' Page setup: A4, Portrait, Center horizontally, Zoom 100%.
    With ActiveSheet.PageSetup
        .Orientation = xlPortrait 'xlLandscape
        .LeftMargin = Application.CentimetersToPoints(0.296850393700787)
        .RightMargin = Application.CentimetersToPoints(0.296850393700787)
        .TopMargin = Application.CentimetersToPoints(0.393700787401575)
        .BottomMargin = Application.CentimetersToPoints(0.393700787401575)
        .HeaderMargin = Application.CentimetersToPoints(0.393700787401575)
        .FooterMargin = Application.CentimetersToPoints(0.393700787401575)
        .CenterHorizontally = True
        .PaperSize = xlPaperA4
        .Zoom = 100
        .PrintTitleRows = "$1:1"
         Range("A5:G5").Interior.ColorIndex = 0
    End With

'    Range("C4:C" & LastRow).Select
'    Cells(0, 0).Value = Date & " " & Time
    Cells(Lastrow + 3, 3).Value = "Data si ora listarii: " & Date & " " & Time

' ######################################################### -- PRINTING -- #########################################################

' Print preview or print directly (add ' in front of the line that you don't want to use and remove it from the other line)
'        Worksheets("Sheet1").PrintPreview               ' PRINT PREVIEW FIRST
'        ActiveSheet.PrintOut                            ' PRINT IMMEDIATELY ON DEFAULT PRINTER
       
' ###################################################################################################################################
               
        Range("A1").Select
       
Application.ScreenUpdating = True

End Sub


Modificat de TRaP (acum 5 ani)


pus acum 5 ani
   
Pagini: 1  

Mergi la