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?
Related
I have a somewhat large spreadsheet with a type of summary page that follows a calender layout.
On this page I manually change the font and color of cells to make it easy for me to find certain things on it. For example, (I lecture mathematics) if I have revision on a certain lesson, I make that cell bold and green. (exact type of green I can sort out myself). I want a VBA code if possible so that if I type the word revision into a cell on that sheet only, not whole workbook, that it would automatically change it to green.
Realistically, I don't manually type in the word revision always. Some of it uses lookups of various types to find what happens on that day to display a word (for example revision) in that given cell.
I don't know if this is possible to do. I realize that if "revision" is shown due to a lookup then the contents of that cell is not equal to "revision" but a formula which simply displays "revision"
Any assistance would be appreciated. If I have a basic code I can manipulate to get it right.
Thanks
Maybe you're looking for something along the lines of:
Sub CheckRevision()
Dim CurCell As Object
For Each CurCell In ActiveWorkbook.ActiveSheet.Range("A1:AZ500")
If CurCell.Value = "Revision" Then CurCell.Interior.Color = RGB(0,204,0)
Next
End Sub
Or equivalently, you can probably use conditional formatting. Home Tab > Conditional Formatting > Highlight Cells Rules > Text that Contains. From there, type the value "Revision" into the value box and you can change the format of the cell to how you like it.
I want to change all cells in Excel spreadsheet of specific, user defined style (let's say 'Beauty' style) to value "Beast".
Sub BulkChangeValeOfStyle()
Dim TheCell As Range
For Each TheCell In ActiveSheet.UsedRange.Cells
If TheCell.Style = "Beauty" Then
TheCell.Value = "Beast"
End If
Next
End Sub
This code is too slow. I got lots of those Beauty cells scattered round the spreadsheet. Is it possible to make it like this:
ActiveSheet.AllCellsWithaStyle="Beauty".value="Beast"
Update
This is just an idea:
ActiveSheet.Cells.SpecialCells(xlCellTypeSameFormatConditions).Activate
or alternatively
ActiveSheet.Cells.SpecialCells(xlCellTypeSameValidation).Activate
but I do not know how to set up the criteria which determine xlCellTypeSameFormatConditions. Or criteria for xlCellTypeSameValidation. Anybody knows?
I don't think it is possible with a better solution than your For Each loop. But you should create a new Style for the cells you want to modify, and modify the format properties of that style when needed like this:
ThisWorkbook.Styles.Item("Good").Interior.ColorIndex=4
I have a SAP Report embedded in a worksheet, it is refreshed via a macro using variables defined in another worksheet. That all works fine, but i am having trouble selecting the data the report generates.
The headings of the report are in and always will fall in this range ("A17:K17"), but the results rows will vary making the total range I want to capture anywhere from ("A17:K18") to (A17:K1000").
The solutions I've already tried didn't work i think because there is almost no consistency in the result data, it's a mixture of text and numbers with empty cells all over the place, in both the rows and columns. Including the occasional completely empty row. This means the methods I have tried before reach a point where it thinks it's reached the end of the populated rows - but it hasn't.
The only factor that remains the same throughout the report is that the cells in the range I want to capture are all filled with a color as default and anything outside the range is unfilled.
To me the simplest solution would be to use VBA to select all the cells beneath and including the headers on ("A17:K17") where the color index is not 0 (blank?) regardless of their contents as I don't mind capturing empty cells. Except I don't know how to do this.
At this point I'd just like to select this range I haven't decided if I'm going to copy it into a new workbook or into an email yet, but that I can do. I've just hit a dead end selecting it.
Quite unsure exactly what it is you require but here's a solution. It's worth noting that both the ColorIndex and Color properties are not necessarily zero with no fill, so if you just change blankCell to a cell with the fill which you define to be blank you'll be good to go.
Sub test()
Set blankCell = Range("A1") ' change this to a cell that you define to be blank
blankIndex = blankCell.Interior.Color
Set cellsDesired = Range("A17:K17")
For Each cell In Range("A17:K1000")
If cell.Interior.Color <> blankIndex Then
Set cellsDesired = Application.Union(cellsDesired, Range(cell.Address))
End If
Next cell
cellsDesired.Select
End Sub
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
I am writting to an excel file from my vb code. The code goes as below
xlsheet3 = xlBook.Sheets.Add(After:=xlSheet)
With xlsheet3
.Columns(5).NumberFormat = "#"
.Cells(j + 1, 5) = someStringValue 'Here "j" is a row counter and this line is in a "for loop"
end with
After writing to excel, most of the cells in excel are correct. But some of the cell's text comes as ####### however if I click on the cell, formula bar shows the correct result. I have tried giving single code before adding the text still that did not help.
Please help me in resolving this.
Thank you
There is not any issue with your code. You need to increase the width of the column or have to use word wrap. In excel if your value is not fully visible it shows it is "######".
If widening and wrapping text doesn't work and the format is set to text which allows display of only 255 characters, try changing the format to general.
This just indicates that the cell is too small for showing the result: make it wider.
See https://superuser.com/questions/65556/excel-displays-for-long-text-whats-wrong for some common reasons why Excel displays "######" in cells.
Either the cell is too narrow to display the contents or the contents are over 256 characters.
Check what you're writing to the cell. If it's not too long then all you need to do is resize the column to fit the new contents.
This is simply what Excel does when the data in a column is too wide to be displayed in the current column width. Make the column slightly wider and you will see all your data.
To autosize the column so it is wide enough to display all its data, double click the column divider at the right edge of the column, in the header bar.