Excel 2007 Display image from image path? - excel

I would like to insert an image into a cell.
The code below inserts it into a sheet:
With wb.Sheets(1).Pictures.Insert("\\bk01fil0001\salesdb$\ImageUpload\NoImage.gif")
.Left = wb.Sheets(1).Range("B2").Left
.Top = wb.Sheets(1).Range("B2").Top
.Width = wb.Sheets(1).Range("B2").Width
End With

You can't insert a picture inside a cell: they always sit "on top" of the worksheet. Best you can do is position it over the required cell/range as you are already doing.

You can make things a little simpler by selecting a cell then inserting the picture...
Sheets(1).Select
Range("B2").Select
ActiveSheet.Pictures.Insert("\\bk01fil0001\salesdb$\ImageUpload\NoImage.gif")
I don't think this would ever be a less efficient method as you would only ever wish to insert a picture on a visible sheet. I do not see you would need to select the sheet more than once, if at all
(As an aside, it sounds as though you are trying to auto-insert images in one column of a table in which case I would strongly recommend using Cells notation as described in the text accompanying this Excel Visual Basic video)

Related

Excel VBA Centering contents in a Word Document table

I am creating a macro in Excel that inserts a table in Word at a specific bookmark. The table insertion process and the process that updates the table is working like I expect it to.
Now, I'm trying to align the data in the table. I'm using the following code to align the data in the table cells:
objTbl.Cell(i, 1).Range.ParagraphFormat.Alignment = wdAlignParagraphCenter
objTbl.Cell(i, 1).Range.ParagraphFormat.Alignment = wdAlignParagraphRight
This works great and is exactly how I want the data to appear.
The only issue I'm having is the data appears at the top of each cell and I would like it in the middle of each cell. Is this possible? I tried to adjust the row height with the following code:
WrdApp.ActiveDocument.Tables(1).Rows.Height = 15
I've tried both 15 and 20 and it didn't make a difference.
My VBA experience has been overwhelmingly within Excel. So this is a bit of a learning curve to program Word from Excel. Any suggestions would be appreciated. Thanks in advance for your help.......
Perhaps you should spend part of your 60 minutes of research in recording a macro. Whilst you wouldn't want to use the recorded code it will point you to the objects and properties you need to use. Of course you do need to know how to use the UI to achieve what you want first.
Sub CellAlignment()
'
' CellAligment Macro
'
'
Selection.SelectCell
Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
Selection.Cells.VerticalAlignment = wdCellAlignVerticalCenter
End Sub
Having recorded the code you can then use the online help or the object explorer to get a pointer to more usable code, e.g.
ActiveDocument.Tables(1).Range.Cells.VerticalAlignment = wdCellAlignVerticalCenter

VBA Excel cannot edit the text in textbox after cut & pastespecial

I found a serious problem in my situation. Since I had the textboxes I decided to move them in the different parts of the sheet using cut & pastespecial option.
Unfortunately I noticed, that my shape is not a textbox anymore. Instead of it it has been turned into the image, where I cannot edit the text inside.
My code looks like this:
ActiveSheet.Shapes("TextboxLocation1").Cut
ActiveSheet.Range("AA70").PasteSpecial
Selection.Name = "TextboxLocation1"
ActiveSheet.Shapes("Textbox_Location2").Cut
ActiveSheet.Range("AA71").PasteSpecial
Selection.Name = "Textbox_Location2"
Is it possible to prevent the initial features of my shape before moving (cut & pastespecial) across the sheet?
I guess, that I shall avoid the "Selection" option and use "dim" instead...
How to avoid using Select in Excel VBA

Excel: How can I hide the Selection Box flying around?

I have the following code to simulate some cells behave like buttons
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
'Application.ScreenUpdating = False
If Target.Cells.Count = 1 Then
'~~~~~~ pseudocode ~~~~~~
If {select cell is one of the chose ones}
{do some stuff}
End If
'~~~~~~ pseudocode ~~~~~~
End If
Range("A1").Select
'Application.ScreenUpdating = True
End Sub
It doesn't matter whereas I use the ScreenUpdating code, the "Selection box" is flying around from A1 to the selected cell and back and it make the sheet much slower.
Can this flying (animation) selection box be stoped?
So far I have found this, not possible to hide, but noting about the flying efect:
Hide the cell selection box in Excel
Edit:
I need (I think so) edit capabilities on the sheet, therefore the option of not changing selection cell is not applicable. Due to:
most of the sheet is informative, and should be available for copy (not edited)
some cells are input forms (free text thing), selection as usual
some cells should behave like buttons (plus/minus for a numeric value, sclaes, simple stuff, but thousand of then, so much easier do/maintain by code), and user must not edit them
grouping should be available, (so that's complicate protecting the sheet)
I am not closed to the option : Range("A1").Select after each (most) of user interaction, but no other method comes into mind to me now.
An example:
I know some would say: "you should make this out from excel", and I agree with you, but this is a mandatory thing, I do not have the power to raise this question
As you can see, I got the "flying selection" that I try to get rid off
cell A1 is already hodden, that will do most of the trick
final version sure will go with hidden gridlines and headlines
rows groups exist, and are important, so no protection possible
all the functionality, I can do easy with vba, just problem with the animation
Maybe this is not the answer that you have been waiting for, but as Mathieu mentioned in his comment, please try to avoid using Selection.
It does make things slower and often causes errors (in example try selecting cell from hidden sheet). Instead just do something with the range that you define with your if statements directly. Every property of Cell or Range can be accessed directly.
Hope it helps.
Not sure how you can achieve you "flying select box" problem, but at least you could add this code, so opening/closing groups are available on protected sheets:
'Password Protect Current Sheet
ActiveSheet.Protect Password:="Add_here_your_password", UserInterfaceOnly:=True
'Enable Group Collapse/Expand Capabilities
ActiveSheet.EnableOutlining = True
How about trying to remove the "back to A1" as much as possible?
Maybe do it only on absolutely necessary, or move back to the changed value (the 33 in your example), or to the question title (in your multi-option example)

Copy paste shapes from cells

I have made a code that creates labels and barcodes for printing.
Due to other reasons (for simplicity) I have placed some labels on a separate sheet that I then need to transfer to the real sheet.
I found some mention about the CopyObjectsWithCells but it's not working for me.
Application.CopyObjectsWithCells = True
Sheets("Robot").Range("A1:L" & Lastrow).Copy
Sheets("Etikett").Range("A" & intRad).PasteSpecial Paste:=xlPasteAll
I get the same result Range().Select then Selection.Copy.
Sheets("Robot").Shapes.SelectAll
Selection.Copy
Works. But it makes the pasted image one large image, not x small images/shapes. and gives a white background overlaying the other text on the sheet.
If I select the range in Excel and press Ctrl + C / V it copies the images as I want.
But with VBA it just won't work.
Example image
Whenever you have a problem, which is can be described with:
I have managed to do it manually in Excel, but I cannot find the correct VBA code.
a good solution is to use the macro-recorder option in VBA and see the code it generates, while you do the manual work in Excel. In your case, this is the code it makes, when it simply copies two shapes to another worksheet:
Sub Makro2()
ActiveSheet.Shapes.Range(Array("Rectangle 1")).Select
ActiveSheet.Shapes.Range(Array("Rectangle 1", "Rectangle 2")).Select
Sheets("Tabelle2").Select
Range("B4").Select
ActiveSheet.Paste
Range("M25").Select
End Sub
This code is really bad, but it works and it may give you good insights to work further.
After selecting and copying the range that contains the shapes, select the destination cell and then use ActiveSheet.Paste.
The option you are using PasteSpecial Paste:=xlPasteAll won't paste Shapes.

Excel VBA Combo box clear

I am using excel 2010.
I want to clear the content of a combo box in my sheet(clear to blank like when it's not selected), but I don't know how to select and clear. I tried to select a combo box like this:
Sheet1.ComboBox1.Clear
But no 'ComboBox' is under the Sheet1 object. The only to select my combo box is use this:
Sheet1.Shapes("Drop Down 24")
I don't know how to select and clear the content, can anyone help me?
What about
ActiveSheet.Shapes.Range(Array("Drop Down 24")).Select
With Selection
.ListFillRange = ""
End With
I assume you actually want to make the displayed value of your control blank. In that case, for a Drop Down Object, as you indicated you would do this:
Sheet1.Shapes("Drop Down 2").OLEFormat.Object.Value = 0
Where 0 indicates which element from the list is selected I.E. none.
If that doesn't work, then you're probably actually dealing with a ComboBox in which case you want to use this:
Sheet1.Shapes("Drop Down 2").OLEFormat.Object.Object.Value = ""
Note this code was created and tested in Excel 2003 (what I have on this machine) so the path to reaching the actual object might vary slightly on Excel 2010.)
As an autentic programmer I would rather prefer this way. That don't depends on excel "range memory selection" on the sheet.
Set oCombo = Sheets("SheetName").Shapes("cmbComboName").ControlFormat
For I = oCombo.ListCount To 1 Step -1
oCombo.RemoveItem (I)
Next
To reset a Drop Down List to a blank cell but still maintaining the list for future use.
Create a Macro to clear the cells. During recording simply select the cell and select "clear contents". This will set the selection to a blank cell but still keeps the drop down list in place.
It would look like this for example.
Range("H3").Select
Selection.ClearContents

Resources