I'm very new to this, so please bear with me.
Background info: I'm trying to create my own quickparts / textblock tool so to speak.
In order to achieve this I have created multiple "buttons" that i want to assign to macros.
The desired result is, that you only have to click the button (named "holidays" for instance) and the corresponding explanatory text is copied into your clipboard.
The problem is that the corresponding text passages are often long and I don't want them included in the code as is the case at the moment where I use this:
Sub Holidays()
Dim obj As New DataObject
Dim txt As String
txt = "Holidays must be requested minimum 2 weeks prior"
obj.SetText txt
obj.PutInClipboard
Beep
End Sub
Instead of having the explanatory text in the code I would like to have it stored in a cell in a different sheet and it to be accessed and copied into the clipboard.
What I have tried so far is putting the text in a cell and named it [Holidays].
I can also set it as a message box, but can't seem to figure out how to get it into the clipboard.
Can anyone help me out with this?
Thanks a lot!
Related
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
My excel sheet creates three proper sentences at three different locations in the worksheet based on the data in the tables but has double spaces between some of the words. I am trying to find a code where I can click a button that copies information from these different arrays in the worksheet that I can then paste into a word file without any double spaces or tab spaces and add some additional generic lines.
Currently, we are manually copying information from these arrays into a notepad to remove the table, the cells and their formating. Then manually replace all the double space and tab spaces with a single space, add some additional generic lines and then copying this information into a Word file.
Trying to write a code that will allow me to do this by clicking the button and then pasting it anywhere I want. What code can I use for this? Thanks.
Here are some ideas, untested so you will probably have to experiment to get them to work. If you get stuck, please use google to research the problem, then post your code and issue in a new question, so folk have more to go on. I'm assuming you have found the VBA editor, the alt-f11 shortcut may work.
Use the Names Manager to designate each of the three sentence locations as a "named range", e.g. first_sentence, second_sentence and third_sentence
Choose a shape and add it to your workbook. The shape will serve as the button. If you right click on the shape, at the bottom of the list of options, is one to connect a macro (which does not exist yet)
In the VBA code editor, make a new module, then create the macro by typing what is below. After creating it, you can connect it to your button, and that will allow you to test it.
Option explicit
' the macro "transfer_text" controls the export of text from the Excel sheet to the Word document
sub transfer_text()
' this is where clean-up code and everything goes
end sub
You need to tell your transfer_text macro where the cells of interest are, perhaps by pasting the following into the body of the macro:
dim range_first as range, range_second as range, range_third as range, clean_first as string, clean_second as string, clean_third as string
' set the ranges so the macro knows where the data is
with thisworkbook.worksheets("Sheet1")
set range_first = .range("first_sentence")
set range_second = .range("second_sentence")
set range_third = .range("third_sentence")
end with
' update the data (without changing the original) using the clean_text function (see below)
clean_first = clean_text(range_first)
clean_second = clean_text(range_second)
clean_third = clean_text(range_third)
' this is where we connect Word
I would use a function to clean the text in each of the ranges, like this, stored outside the main macro, though in the same module for convenience. You can add text editing operations to it later:
Function clean_text(string_text as string) as string
' the text from the range is passed in
' various operations performed on the text
' the result is sent back to where the function was called
clean_text = trim(string_text)
end function
That is rather alot already, and we have not even connected to Word. To connect to Word is really better done as a separate topic. You will need to set a specific reference to word, using the Tools-References menu in the VBA project. Scroll down the list until you find Microsoft Word, and tick the box.
That will give you access to the object library for Word, so you can place your text using bookmarks in the Word document.
You will also need to deal with the file object - opening the Word document. Another topic for you to research!
That should get you started!
Some notes on debugging. Set a breakpoint at the start of the With statement by clicking the mouse in the left hand margin of the code editor. Then click you button... the code will stop at the breakpoint. Use the mouse-over and the Immediate Window (you may need to unhide it) to check variable values. Press F8 to step forward one line of code. In the Immediate Window, type a ? followed by the variable you want to check.
Hi I am kind of new to VBA and i can't seem to find what i am looking for.
What i want to make is a macro that links to another page in my workbook that refers to data in a certain cell.
from certain datapoints i have a cell set up that as everything is filled in it gives the name of the page i want to link to (lets say "overview_Oct_2020" by filling in the month and year in other cells), and when running the macro go to that.
I seem to totally blank on how to do it. I made a =HYPERLINK() version pretty easily, but i want to change that to a button, hence the reason for a macro.
so technically i want to make a button with a macro that goes to [TEXT IN CEL A1]!A1
Put this macro in a standard module and assign it to a button on the worksheet
Sub link()
Dim textInCelA1 as String
textInCelA1 = Range("A1").value
Sheets(textInCelA1).Activate
Range("A1").Select
End Sub
I've researched this to death and feel like I'm the only person it's ever happened to.
I have some VBA that:
Creates a copy of 3 sheets to a new wb
In the new wb converts to values, deletes objects (shapes and controls) and all but 3 ranges
Opens an existing third file and sets the contents of three ranges in that to match the new wb
Closes the existing file (saved)
Closes the new wb (saved)
Gives a message box saying complete
At the end of all this, something weird happens with the state of the windows. The selected cell does not appear selected. If I try and click a control afterwards, it selects the object (hence users could drag them). It shouldn't and this is the big problem.
I've tried selecting a cell through code, it throws an error. I had limited success by forcing drawing mode off using Call CommandBars("Drawing").Controls("Select Objects").Execute and activating a specific sheet & selecting a cell. However, even then if I even click on a few cells afterwards, the next time I select a control it will select it as an object rather than click the thing.
I have no idea why and can't find anyone who's seen this before.
Any ideas on what I can do?
Thanks,
Basil
I didn't figure it out entirely, but I did find a fix. Hopefully it works for anyone else who finds this problem.
At the end of the code I added this:
ActiveSheet.Shapes.Range("ctrlExportPrices").Select
ActiveSheet.Range("B8").Select
So it forced a control on the sheet to be selected, and then a cell.
The next time I select the control manually, it clicks it rather than selecting the drawing object.
I'm a new user to the site, and new to VBA.
I'm just trying to get the contents of multiple text boxes (from many different sheets) into one sheet.
Right now, I've been stuck on getting the information from a single text box into a variable.
At this point in time, I just want it to retain the line feeds (new line, enter, return, etc).
Even if I could just copy/paste the information, at this point, I can't even access the text box via VBA.
If anyone could help out, that'd be really great! Thanks.
How is this for a start?
The following code will extract the text within the textbox and print it to the immediate view.
Sub ExtractTextFromShapes()
Dim shp As Shape
For Each shp In ActiveSheet.Shapes
If shp.Type = msoTextBox Then Debug.Print shp.TextFrame.Characters.Caption
Next
End Sub
You can easily replace
Debug.Print shp.TextFrame.Characters.Caption
With
strText = shp.TextFrame.Characters.Caption