table to picture macro

chobanne

Active Member
Joined
Jul 3, 2011
Messages
269
Hello all, i need a help to make this macro

I attached picture of example table which i want to copy and paste it to sheet 2 as a picture but with some rules.

The position of used cells in table could be changed when i input data, and It is important macro to apply the rules given bellow.

rule 1.

Macro wil find every single column without data and will hide it (in this example it is columns H I and J) The data in column will count only from row 8. Every column from A to S must be affected by the rule. so if some other column is without data it must be hiden too. if all columns have data there will be no hiding

rule 2.

Macro must automatically find last row with text, to make copy box. (in this example he must find row 18 because row 19 is empty and make copy box area A5:S18)

Rule 3

Macro will make thick outside borders around copy box (in example it would be around A5:S18) and then he will make copy and paste it in sheet 2 as a picture.

Rule 4

After making picture, macro will return to sheet 1 and what is the most important, table in sheet 1 must be exactly the same as it was, before starting macro. (no hiden columns, no outside borders)

I hope this is posible to make, i cant do it alone.
I do this in steps, 100 times a day, and i hope someone will help

This is how sheet1 looks before starting macro and after macro is finished.

Example.xlsx
ABCDEFGHIJKLMNOPQRSTU
1
2
3
4
5NWINDDimensionsd/bcf,0rr/bYrlAAcjYlcweWeWe'
6d (-) [d1]b (D) [d2]l
7[m][m][m][ - ][ - ][m][ - ][ - ][ - ][m²][m²][ - ][ - ][ - ][kN/m²][kN/m'][kN/m']
8op1x0.100.204.200.502.2542.001.000.861.941.8140.363
9op12y0.200.104.202.002.0070.001.000.861.721.6120.161
103y0.100.204.200.502.0042.001.000.861.721.6120.322
114x0.100.204.200.501.8021.001.000.781.401.3190.264
12op4x0.242.401.209.841.000.861.030.9690.237
13op2x0.242.401.209.841.000.861.031.2160.297
147x0.242.401.209.841.000.861.031.2160.297
158x1.002.003.002.302.001.394.500.310.942.161.1830.296
169x1.002.003.002.302.001.394.500.310.942.162.5471.1830.296
1710x0.100.204.200.501.8021.001.000.781.401.6540.331
1811x0.242.401.209.841.000.861.031.2160.297
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
Sheet1


this is how picture looks in sheet2

1650477015481.png
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
See if this macro works for you. Note that because your table starts in column A the macro temporarily inserts a new narrow column A so that the left border is visible.

VBA Code:
Public Sub Copy_Table_As_Picture_To_Sheet()

    Dim table As Range
    Dim col As Range
    Dim hiddenColumns As Collection, hiddenColumn As Variant
    Dim outsideBorders As Collection, outsideBorder As Variant
    Dim edge As Variant
 
    Set hiddenColumns = New Collection
    Set outsideBorders = New Collection
 
    With Worksheets("Sheet1")
 
        Set table = .Range("A5").CurrentRegion
     
        'Insert temporary narrow column A to move table to the right so that left border is visible
     
        .Columns(1).Insert
        .Columns(1).ColumnWidth = 0.1
     
        'Save columns without data and hide them
        
        For Each col In table.Columns
            If WorksheetFunction.CountA(col.Offset(3).Resize(col.Rows.Count - 3)) = 0 Then 'Offset(3) to start count at row 8
                hiddenColumns.Add col.Column
                col.EntireColumn.Hidden = True
            End If
        Next

        'Save current outside borders weight and change to thick outside border
     
        For Each edge In Array(xlEdgeLeft, xlEdgeTop, xlEdgeBottom, xlEdgeRight)
            With table.Borders(edge)
                outsideBorders.Add .Weight, CStr(edge)
                .Weight = xlMedium
            End With
        Next
     
        'Copy table to Sheet2 as a picture.  Offset and Resize needed to enlarge copied range to include top and left borders
     
        table.Offset(-1, -1).Resize(table.Rows.Count + 1, table.Columns.Count + 1).CopyPicture xlScreen, xlBitmap
        With Worksheets("Sheet2")
            .Pictures.Delete
            .Range("A1").PasteSpecial
        End With

        'Unhide hidden columns
     
        For Each hiddenColumn In hiddenColumns
            .Cells(1, hiddenColumn).EntireColumn.Hidden = False
        Next
     
        'Restore outside borders
     
        For Each edge In Array(xlEdgeLeft, xlEdgeTop, xlEdgeBottom, xlEdgeRight)
            table.Borders(edge).Weight = outsideBorders(CStr(edge))
        Next
     
        'Delete temporary column A
     
        .Columns(1).Delete
     
    End With
 
End Sub
 
Last edited:
Upvote 0
Yes its looks like what i want, but there are some little problems.

1st - the table has one empty row above which i dont want to be seen. I presume you try to add narrow row above the table that the upper border could be seen, That row must be narrow too.
2nd - right border can't be seen at all so i think one more narrow row must be added there too.
 
Upvote 0
Record a macro whilst inserting a narrow row to get the code for that and incorporate it into the macro.

Try adjusting the constants in the table.Offset line. The syntax is Offset(rowOffset, columnOffset) and Resize(numRows, numColumns), so Offset(-1, -1) is the row above the table and the column on the left of the table (the narrow column) and Resize(table.Rows.Count + 1, table.Columns.Count + 1) extends the table by 1 row and 1 column.
 
Upvote 0
Solution

Forum statistics

Threads
1,215,168
Messages
6,123,408
Members
449,098
Latest member
ArturS75

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