How to apply a Texture to a Range in Excel with VBA? - vba

I have a sheet in my workbook, and i'm trying to make it look better.
I Tried a google search but no luck...
i know how to apply a picture to a whole sheet (but it gets mosaique) :
Sheets("Phase Psy").SetBackgroundPicture Filename:=ThisWorkbook.Path & "\images\magie\slayers\lina_inverse_vs__voldemort.jpg"
also for comments, you can use either a picture or some nice preset textures :
Range("A1").Comment.Shape.Fill.Userpicture "c:\myPic.JPG"
or
Range("A1").Comment.Shape.Fill.PresetTextured msoTexturePapyrus
I would like something similar but not applyed to comments but to a range.
Is there a way to add a picture or texture, or pattern (but not the ugly ones you can find with format cell>Fill>Pattern Style) ?
Thanks for any advice.

Sub Test2()
ActiveSheet.Pictures("Picture 1").Width = ActiveSheet.Range("A1").MergeArea.Width
ActiveSheet.Pictures("Picture 1").Height = ActiveSheet.Range("A1").MergeArea.Height
End Sub
The above will fill the Cell with the picture, keeping the aspect ratio, until either the full width or height of the cell is acheived

Related

Change Shape in a different sheet if a cell is >95%

I'm really new to VBA and i'm trying to change the colour of multiple shapes on one sheet depending on the value of a cell in a different sheet. If the value is above or below 95% i'd like the shape to be coloured using RGB green or blue accordingly.
I am struggling to understand most other VBA scripts as they are either for the same sheet or for one shape.
Thank you so much in advance.
There are many ways to do it but probably the easiest way to explain is: lets say you have a sheet called Sheet1 and one called Sheet2. On Sheet2 you have a shape called Square. Your code might look like this:
Dim firstSheet as worksheet, secondSheet as worksheet
Set firstSheet = Sheets("Sheet1")
set secondSheet = Sheets("Sheet2")
If firstSheet.Range("A1") < 0.95 then
secondSheet.Shapes("Square").Fill.ForeColor.RGB = RGB(0, 0, 0)
End If
This is just a really basic example, as you learn more VBA you'll find there are many ways to do something like this.
FYI the reason peope have voted this post down is because you are expected on this site to show what you've tried and explain why it doesn't work. Here is more information: https://stackoverflow.com/help/mcve
Welcome to SO.

Adding a background image to a particular cell

I tried to insert the background image using following code:
Pic = "C://Picture/Logo1"
Activesheet.SetBackgroundPicture Pic
This inserts the picture to full sheet but I want to add it to particular cell or a range of cells. Please help
As far as I know excel doesn't support appending the image to a cell (with or without VBA).
The background of a single cell supports colors/gradients/fill-patters, but not the pictures.
It is possible however, to "place" the picture (shape) object with the same width and height right above the cell and make it locked and move around together with the cell if somebody attempts to resize cell widths. I personally wouldn't go that way, too much to code and too much risk of breaking the structure.

Copying an Object Across a Workbook

I have a workbook which displays a little coloured box based on some input metrics from another worksheet within the workbook. I want to display these little coloured boxes in an additional worksheet in the workbook. Is there a way to copy a shape across worksheets so that the colour will still update with the metrics rather than using the code again for a separate worksheet?
I essentially want to display this textbox with the coloured boxes/arrows in another worksheet as well.
A pretty dirty way to do something like this would be the Indirect-Picture-Copy-Solution.
Asume the art is at Sheet1 B2:D8 then just input a picture in Sheet2 (the picture doesn't matter, just pick the first you can find)
While the Picture is selected input in the formula bar =Sheet1!B2:D8.
Hope that helps ;)
EDIT
For making it dynamically put in any module:
Public Function testing() As Range
Set testing = Range(Sheet1.Shapes("Dia 1").TopLeftCell, Sheet1.Shapes("Dia 1").BottomRightCell)
End Function
(Make sure to change the names to fit your workbook/sheet/shapes....-names)
Then define a name (I'll pick TETE for this example)
Refers to: =testing()
Then the picture-formula is: =TETE
Whenever the size or position changes, your picture fits to it... still not a good way to solve your problem (to my eye)
Funny fact: making the picture-formula directly to =testing() will just pop an error

Incomplete border around range in excel

I'm using Vb.Net to automate an excel spreadsheet generation. I'm drawing a border around a range, but for some reason the text in the first columns hides the border. Is there some way of sending this text to "background", like a z-index in web, so that the border displays correctly?
This is the relevant line of code:
range = sheet.Range("B" + (sectionStart).ToString, "M" + (currentLine).ToString)
range.BorderAround(XlLineStyle.xlContinuous, XlBorderWeight.xlThin)
I would say your best bet is to wrap the text in column A, we get this problem a lot here at work and it usually solves the issue.
Try adding:
sheet.Range(“A:A″).IsWrapText = True
To your code, hope this helps!

Excel Doesn't Auto-Fit 45º Text

When I orient column headers at 45º I have to manually resize each column since Auto-Fit won't let the oriented text overlap with the neighboring cell.
Is there a way to programatically (with VBA) auto-fit the columns where they'll overlap? I'd like a solution which takes font size into account too.
AutoFit only works on non 0º oriented text when the cell with the oriented text has borders set. Odd...
What you need to do is autofit to the cell range excluding the header row:
Sub autofitToRange()
Range("B2:F5").Columns.AutoFit
End Sub
Instead of:
Sub autofitFullColumns()
Range("B:F").Columns.AutoFit
End Sub
SheetName.Range("a:c").Columns.EntireColumn.AutoFit
Will autofit columns a - c in Sheet SheetName. Is this what you were looking for?