Excel VBA Macro for Cell Formatting

EnderLexus

New Member
Joined
Nov 20, 2023
Messages
3
Office Version
  1. 2016
Platform
  1. Windows
Hi there,
I was wondering if anyone could help me with for a macro in order to format a table that is used fairly regularly.
At the moment I can paste in a list of item numbers, and with a nifty collection, it will give me some further info on if we have that/where it might be.
My issue that I have at the moment is that it is not formatted at all, which is fine with 10/20 items but when its in the 100's its a bit tedious to do manually every time. And I'm struggling to write up any code that could format it the way that I'm trying to do, have looked around google a few times but doesn't seem to quite work for what I was aiming for especially with the spacing. ( See attached for comparisons of what I have at the moment vs what I am trying to do )
If anyone is able to help out by potentially pointing me in the right direction to get started it would be really appreciated!
 

Attachments

  • Current table structure.JPG
    Current table structure.JPG
    147.9 KB · Views: 10
  • IDEAL Table structure.JPG
    IDEAL Table structure.JPG
    128.4 KB · Views: 10

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
HI
I think you can do it with VBA and finally Sort based on the column you are interested in.
A peculiar thing is the empty line between the articles.

Hi
Mario
 
Upvote 0
HI
I think you can do it with VBA and finally Sort based on the column you are interested in.
A peculiar thing is the empty line between the articles.

Hi
Mario
Okay no problems, On an old sheet we used to use which is now broken, it would do the first blank in-between the ones that couldn't be found vs what had been found but that was a singular space in the whole table. But I'm not sure how they did it
 
Upvote 0
What you're asking for is certainly doable, but could you please post your original sheet using the XL2BB add in, or alternatively share your file via Google Drive, Dropbox or similar file sharing platform? We can't copy from an image.
 
Upvote 0
Try the following on a copy of your workbook. Change the sheet name & cell colour to suit. It assumes your sheet is laid out exactly as it appears in your image - headers in row 3, Column B is the first data column.
VBA Code:
Option Explicit
Sub Fix_Format()
    Application.ScreenUpdating = False
    Dim ws As Worksheet, r As Range
    Set ws = Worksheets("Sheet1")   '<-- *** Change to actual sheet name ***
    Set r = ws.Range("B3", ws.Cells(Rows.Count, "F").End(xlUp))
    
    '1. Sort the columns
    With ws.Sort
        .SortFields.Clear
        .SortFields.Add Key:=ws.Range("D3"), Order:=xlAscending
        .SortFields.Add Key:=ws.Range("B3"), Order:=xlAscending
        .SortFields.Add Key:=ws.Range("C3"), Order:=xlAscending
        .SortFields.Add Key:=ws.Range("E3"), Order:=xlAscending
        .SortFields.Add Key:=ws.Range("F3"), Order:=xlAscending
        .SetRange r
        .Header = xlYes
        .Apply
    End With
    
    '2. Insert blank rows
    Dim a, i As Long, j As Long
    a = r
    For i = r.Rows.Count + 2 To 5 Step -1
        For j = 1 To UBound(a, 2)
            If a(i - 2, j) <> a(i - 3, j) And a(i - 2, 3) <> "Not Found" Then
                ws.Rows(i).Insert
                Exit For
            End If
        Next j
    Next i
    
    '3. Add red interior cell colour
    Dim LRow As Long
    LRow = ws.Cells(Rows.Count, "D").End(xlUp).Row
    ws.Range("B4:F" & LRow).Interior.Color = xlNone
    For i = LRow To 4 Step -1
        If ws.Cells(i, 4) = "Not Found" Then
            With ws.Range(ws.Cells(i, 2), ws.Cells(i, 6)).Interior
                .Color = 8420607            '<-- *** Change cell colour to suit ***
            End With
        End If
    Next i
    Application.ScreenUpdating = True
End Sub
 
Upvote 1
Solution
Try the following on a copy of your workbook. Change the sheet name & cell colour to suit. It assumes your sheet is laid out exactly as it appears in your image - headers in row 3, Column B is the first data column.
VBA Code:
Option Explicit
Sub Fix_Format()
    Application.ScreenUpdating = False
    Dim ws As Worksheet, r As Range
    Set ws = Worksheets("Sheet1")   '<-- *** Change to actual sheet name ***
    Set r = ws.Range("B3", ws.Cells(Rows.Count, "F").End(xlUp))
   
    '1. Sort the columns
    With ws.Sort
        .SortFields.Clear
        .SortFields.Add Key:=ws.Range("D3"), Order:=xlAscending
        .SortFields.Add Key:=ws.Range("B3"), Order:=xlAscending
        .SortFields.Add Key:=ws.Range("C3"), Order:=xlAscending
        .SortFields.Add Key:=ws.Range("E3"), Order:=xlAscending
        .SortFields.Add Key:=ws.Range("F3"), Order:=xlAscending
        .SetRange r
        .Header = xlYes
        .Apply
    End With
   
    '2. Insert blank rows
    Dim a, i As Long, j As Long
    a = r
    For i = r.Rows.Count + 2 To 5 Step -1
        For j = 1 To UBound(a, 2)
            If a(i - 2, j) <> a(i - 3, j) And a(i - 2, 3) <> "Not Found" Then
                ws.Rows(i).Insert
                Exit For
            End If
        Next j
    Next i
   
    '3. Add red interior cell colour
    Dim LRow As Long
    LRow = ws.Cells(Rows.Count, "D").End(xlUp).Row
    ws.Range("B4:F" & LRow).Interior.Color = xlNone
    For i = LRow To 4 Step -1
        If ws.Cells(i, 4) = "Not Found" Then
            With ws.Range(ws.Cells(i, 2), ws.Cells(i, 6)).Interior
                .Color = 8420607            '<-- *** Change cell colour to suit ***
            End With
        End If
    Next i
    Application.ScreenUpdating = True
End Sub
I have tried this on a copy of the workbook, and seemed to work like an absolute charm. Is very much appreciated!!
 
Upvote 0

Forum statistics

Threads
1,215,069
Messages
6,122,952
Members
449,095
Latest member
nmaske

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top