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

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

Related

Macro Paste in Excel

Currently I have macros set up in my excel that pastes a list when clicked.
However I am encountering an issue where I have to paste the copied list (from a pdf) into notepad before pasting into excel, so that it separates into cells instead of trying to excel cram the entire list into one cell when done directly.
I have tried creating a macro that would open a cell directly paste into it then cut out before pasting (Which works when done manually) as well as a number of different methods that were all dead ends.
My procedure is currently:
Open PDF, ctrl a, ctrl c
paste into notepad then ctrl a, cut
paste into excel
If I could get help removing the notepad part of the procedure, I would be incredibly happy!
If you paste the whole thing inside a cell like this:
Then you can use this script to do a text to rows operation.
Option Explicit
Sub TextToRows()
Dim RG As Range
Dim RGValue As String
Dim LinesArray
Dim LineCount As Long
Dim I As Long
Set RG = Selection
If RG.Cells.Count <> 1 Then Exit Sub
RGValue = RG.Value
LineCount = Len(RGValue) - Len(Replace(Replace(RGValue, Chr(13), ""), Chr(10), "")) + 1
If InStr(1, RGValue, Chr(10)) = 0 Then
LinesArray = Split(RGValue, Chr(13))
Else
LinesArray = Split(RGValue, Chr(10))
End If
RG.Offset(1, 0).Resize(LineCount, 1).Value = Application.Transpose(LinesArray)
End Sub
Viola!
Your aim is reduced notepad step however I suggest I would remove the pdf step since poppler or xpdf pdftotext -layout is usually good to add the needed white space to keep text tabular. That can be drag and drop pdf on a shortcut that calls open new spreadsheet with text. And here is the usual core issue with cut and paste plain text as a framework.
Most spreadsheets as far back as last century have several text import methods, most common is add commas for csv import, but space separated text is accepted too. (I still use MSeXcel 97 portable as its an old familiar) It often requires some intervention to check detection, so here in Modern Open Office I specified combine spaces. Thus, introduces just one minor error here, "Addresss" is moved left-wards.
No Problem it has a spelling mistake so it's just that one that needs managing twice. 1st spell check and then move it right.
Every case can be different but if you write a macro to cover those corrections you repeat then it pays to run for the next import to include all the steps.
The simplest is plan ahead by tidy the text (here used tabs to replace spaces), then drag and drop, the results are usually cleaner data in = cleaner the cells are aligned.
Thank you everyone for your advice, I finally found a solution. It was as simple as a paste special text.
ActiveSheet.PasteSpecial Format:="Text", Link:=False, DisplayAsIcon:= _
Hopefully this helps someone else! Found it whilst looking into ways the window operating system formats copy/paste. It can be manually done by right clicking > Paste Special > Text

Selecting a Named Range Cell Location EXCEL VBA

Basically I want to select a specific cell that my named range is referencing and not returning the Value. I want to mimic the below, but been having errors with selecting the cell address.
Range("AU8").Select
What I have been trying which hasn't been working.
ThisWorkbook.Names("Root").Select
ThisWorkbook.Names("Root").Cells(1,1).Select
Does anyone know how to fix this?
Use the .RefersToRange property of the Name Object. Like so:
ThisWorkbook.Names("Root").RefersToRange.Select
But the .Select method will fail if the sheet is not active. A better method is Application.Goto like so:
Application.Goto ThisWorkbook.Names("Root").RefersToRange
Side Note:
.Select is often a pointless and wasteful method that hinders the speed and accuracy of your macro scripts. Instead of relying on .Select and Selection, you can instead remove both, and directly reference the ranges that you want to interact with.
Comparison:
ThisWorkbook.Activate
Sheets("Sheet1").Activate
Range("B2").Select
Selection = "Input Text"
ThisWorkbook.Sheets("Sheet1").Range("B2") = "Input Text"
The last line does the same thing as the previous 4, but skips the steps of activating the workbook, sheet and range individually, which removes that screen jittering, and speeds up the execution of the script by massive amounts.

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.

copy text from one excel to another one

I would like to copy the text from a excel file in a specify range to another excel file in the same position
Here is the code that I tried
Sub OneCell()
ChDir "C:\Workfile"
Windows("simple av & cv template(Level).xls").Activate
Sheets("Table ENG SG").Select
Range("C9:C44").Select
Selection.Copy
Windows("Finalize").Activate
Sheets("sheet1").Select
ActiveSheet.Paste
End Sub
Do I need to define sth at the beginning of my program first or did I make any mistakes?
Assuming both your workbooks are open in the same instance of Excel which you can confirm by going to the View tab > Switch Windows and seeing if both workbook names are listed:
Sub ModifiedOneCell()
Workbooks("simple av & cv template(Level).xls").Sheets("Table ENG SG") _
.Range("C9:C44").Copy _
Destination:=Workbooks("Finalize").Sheets("sheet1").Range("C9:C44")
End Sub
The help for this can be found by opening the Object Browser in VBA (press F2), scrolling down to the Range object, clicking Copy from the list in the right pane then press F1.
The [space][underscore at the end of lines is the line continuation key combination in VBA allowing you to split long lines of code for readability.
If your workbooks are open in separate instances of Excel (i.e. only one is visible in Switch Windows), then copying to the clipboard and selecting Windows as you did in your code is the correct approach.
You would need to add
Range("C9:C44").PasteSpecial xlPasteAll
in place of
ActiveSheet.Paste
to get the result into the desired range
Ranges don't have a Paste method - only PasteSpecial.

Excel 2007 Display image from image path?

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)

Resources