Copy ComboBox from a Word Document to another Word Document with vba - vba

I have a combo box (combo1) in a word document and I would like to copy it to another word document. (end game will be looping through 100's of docs).
I can't for the life of me work out how to select and/or copy the combo box, although its easy enough to do outside of vba.
So far I've tried turning it into a bookmark, seems to select ok, but won't copy.
ActiveDocument.Bookmarks(combo1_bm).Select
Selection.Copy
I thought it would be able to done as an inline shape (as that is how they are added?), however again the selection appears to work, but the Copy doesn't.
ActiveDocument.InlineShapes(combo1).Select
Selection.Copy
Any ideas on what I can try next?
Cheers,
Michael

Your attempt with bookmark was quite good. You just need to extend your code a bit:
ActiveDocument.Bookmarks("combo1_bm").Range.Copy
....
Selection.Paste 'or different pasting procedure
Keep in mind that you don't need to select object before copy it. Just try doing as I show above. Moreover, don't miss quotation marks for names or use bookmark index to work with appropriate one. Copy method will copy inside range of the bookmark and keep original bookmark in place, unchanged.

This should do the trick.
Set ComboBox1Range = ActiveDocument.Range(Start:=ActiveDocument.Bookmarks("combo1_bm").Range.Start - 1, _
End:=ActiveDocument.Bookmarks("combo1_bm").Range.End)
ComboBox1Range.Expand Unit:=wdParagraph
ComboBox1Range.Copy

Related

How to find active cells range in excel VBA

I am trying to open multiple webpage tab from my selected cells. I would like to find the selected cell (based on the mouse) starting and ending row and column information for further use of vba macro.
Thanks in advance...
You can do this:
x = ActiveCell.Address
MsgBox (x)
but really, you should try to avoid using selection where possible. The reason for this is because users can (and I've found, will) click in to other spreadsheets as the code is running and so what you have intended as the selection, may no longer be the actual selection. It also affects the longevity of the code because it's much more difficult to fix if something breaks.

Make small changes to multiple functions without affecting cell reference

What is the best way to change ">" to ">=" in 100+ formulas such as:
=IF(INDEX($B$2:$B$46,MATCH(A3,$D$2:$D$46,0))**>**$A$2),"1","0")
=IF(INDEX($B$2:$B$46,MATCH(A3,$D$2:$D$46,0))**>**$B$2),"1","0")
=IF(INDEX($B$2:$B$46,MATCH(A3,$D$2:$D$46,0))**>**$C$2),"1","0")
Would I need some VBA code?
I would suggest one more idea if you are not comfortable doing the find and replace in excel.
Press the key combination Ctrl+` (BackQuote symbol)
Now you will have all the formulas visible in the cells.
Copy the range or columns in which you need to modify the formula
Paste it in a .txt file and then do the find and replace
Once it is done copy all the formula and paste it back to the same range.
Press Ctrl+` again and now you have got all the formulas changed as per your need
As long suggested, take a backup of your worksheet before you do these modifications
As Scott_Craner stated, a simple Find/Replace should work.
Save a version of your data in case you misjudged where the search subject is in your data.
Highlight the cells with the formulas you wish to change.
Press Ctrl and the F keys (or click on "Find&Select" in the Home tab in the "Editing" group and select Replace from the drop-down)
In the Find and Replace dialog box, make sure you are on the Replace tab.
In the "Find what:" box, type ">" without quotes (note, if there are other > symbols in some formulas, you can distinguish these with some repetitive text before and after)
In the "Replace with:" box, type "<" without quotes (note, if you included other text to specify certain instances of ">", make sure you include all the text that should also be replaced)
Hit the "Replace All" button.
Your spreadsheet formulas should now be as you'd like them. If they're not, thank goodness you saved another version first. Find/Replace is very powerful, so make sure to limit the scope by selecting the cells you want searched and being sure that Excel will find only those data you wish to change.
If you are interested in VBA, you could use it here. But Excel already has a powerful, user-friendly tool at your disposal.

VBA code to clear FORMATTING and leave plain text data in a chosen column in Excel

I need to create a button to add to Excel (2010 currently) that will strip all the formatting in that column and just leave plain text. Our current solution is to copy the data into Notepad to strip out the formatting and then copy it back in from Notepad. This works, but is inelegant and would be far easier if I could just create a button to do this within Excel itself. I've seen a few solutions posted but none of them seem to deal with a randomly selected column. Help!
The Range.ClearFormats method seems appropriate here.
With Worksheets("Sheet1")
.Columns(1).ClearFormats 'clear formatting from column A
End With
'for a manually selected group of cells
Selection.ClearFormats 'clear formatting from the cells currently selected
fwiw, the Clear Formats command is available on the ribbon through Home ► Editing ► Clear ► Clear Formats (Alt+H+E+F). You could easily add that command to the QAT rather than create a macro that largely duplicates the command and assign it to a custom button.
Excel already has a button on the Home tab:
Just select the entire column and click Clear Formats
Just to add to Jeeped's answer:
Using this code you'll clear the whole columns formatting (select a random cell, run this code and the whole columns formatting has been cleared)
Sub ClearColumn()
i = ActiveCell.Column
ActiveSheet.Columns(i).ClearFormats
End Sub

Find/replace conditionally formatted cells

I've been using a conditional format formula =OR(B2=B1,B2=B3) to highlight consecutive duplicates. I then use format painter to copy the formula to all columns in my excel table.
I have set up a button that will, amongst other things, copy a workbook into a new workbook. I now want to include the above formula in this macro. My final objective is to replace all of the cells found with this formula with an asterisk (*).
I first tried to just pop the formula into the macro as a starting point -
For Each sh In Destwb.Worksheets
With sh.UsedRange.FormatConditions _
.Add(Type:=xlExpression, Formula1:="=OR(B2=B1,B2=B3)")
.Interior.Color = RGB(198, 239, 206)
End With
Next sh
But this just makes a mess of seemingly randomly highlighted cells. I'm not sure where I've gone wrong. Even column B highlights are all wrong. Could the header in B1 affect this? It doesn't when I use the CF normally. How can I expand the CF into all columns uniquely?
Finally, how do I go about working a replacement of formatted cells into this formula? Or is there a quicker/easier way to meet this end-goal?
I didn't realise I could add custom text in that way. That's moved me a good couple of steps forward.
Taking Balinti's suggestion into consideration I've tried a workaround. I was making a couple of assumptions that turned out to be wrong. I have been able to enter the CF into the Array of data that I have in my original workbook. It's not as simple as putting it into a table, but by manually selecting the range in each column and inputting the CF I have made it work.
I also wasn't sure if the formatting would carry through to the new worksheet as I have used the Paste Special command to convert the array formula to values in the new sheet. It does, however, carry the formatting across which is very handy.
It's not the perfect solution for me but it appears to be working so far. I still need to test what happens when I change the date and get updated data. It would still be interesting to know if I can move this formatting into my macro though. Any tips?

Excel VBA: Resetting spreadsheet count

I have a excel VBA macro that dynamically generates and deletes spreadsheets based on user input. However, when I open the VBA IDE, it seems that although I am naming my spreadsheets in the subs that create/delete them, the overall count is still increasing.
For example, depending on how far into execution my program is, under the "Microsoft Excel Objects" folder in my current project, the spreadsheets in the current workbook could look something like
Sheet101(Sheet3)
Sheet103(Sheet2)
Sheet104(Sheet1)
Or
Sheet81(Inputs)
Sheet83(Date Adjustment Interpolation)
Sheet84(Pricing)
Sheet85(Comparison)
No matter if I delete the rest of them and add one, it still picks up where the last highest one left off.
I don't know how many times this macro will be run and I'd feel a lot better about putting it out there if I could reset this annoying tally on the number of spreadsheets that have ever been generated, since I don't know for sure where excel will cut me off. Plus it's just annoying.
My Question:
I would like to know how to alter that spreadsheet number, or at least what the relevant object is for doing so.
Thanks!
Thanks to #dijkay s suggestion on code names, I've found some code to accomplish this.
ThisWorkbook.VBProject.VBComponents("Sheet1").name = "test"
Will change the code name of Sheet1 to test, so in the Excel Objects folder, it will appear as test(Sheet1) for example.
This option, however, requires messing around with some trust/security settings in each individual excel client running the macro, which is unsuitable for my purposes, unfortunately. You can also change the value manually by changing the (Name) property directly in the IDE through the properties window.
here are some ideas you can try...
Sheets(x).Name = "Sheet" & x
or (assuming in this example, 'Sheet3' doesn't already exist:
Set Sheet3 = sheets.Add
Sheet3.name = "Sheet3"
This is more cleanup than re-setting
cheers,
Micéal