Copy content of Word range to variable for later use? - vba

I have a Word document that has a checkbox. When the checkbox is clicked (unchecked) I select a range, copy it into a Range variable and then delete the original range. If the checkbox is clicked again (checked) I need to put the range stored in the variable back in its original position with the formatted text that was deleted. The idea here is that a user can decide if he wants a range of text visible or not.
The issue I have is that the range variable (where I store the original range) contains only a pointer to this original range and when I delete it the pointer is now pointing to nothing, hence the variable holds nothing.
I've tried passing the range to another variable byVal, pasting the original range to another word document and then copying it from there (text remains in the new document but the original memory location is swiped so still pointing to nothing!). Any ideas on how I can keep the original range for reuse on a later time?
Also, I am deleting the range because if I only hide the text I have empty spaces left which don't look nice.

Related

How do I save cells just pasted over?

I am working on a change management. I have good, robust code for single cell changes with old and new target values. for pasting a range, however, it is more complicated. for instance, if i copy range A1:A5 and paste to B1, how do i save the previous values in B1:B5, saving B1 is easy, the rest is beyond me.
I did something similar, the method was fairly simple, when something is pasted it is selected, save the selection.address into a range variable.
Then save each cell's contents to string variables.
then undo the paste.
then copy out the now restored contents of the range variable to wherever you want for backup purposes.
then write back the variables with the originally pasted values over the top.
Set the code to run on a worksheet change event and you are all good. Obviously you need to put in some code to detect that only certain areas are in the target so the event doesn't go bananas and don't forget to turn the events off whilst you do you stuff then back on after.

Name and values: single cells vs. range

I have a problem using named range in Excel.
If I create a named range using a cell range, I can obtain also a value.
If I select the same cell, one by one using mouse, I haven't anymore an associated value.
I have tried also to use VBA, and the result is the same.
Have you some idea to obtain values using multiple cells (without using single range)?
Result expected:
I'm expect to see cell values of the single cells.
- As you can see, if I choose every single cell (A2,A3,A4), Excel doesn't display me the value of these cells inside the name box.
- If I choose a single range (A2:A4), I can see the value of these cells.
How can I display the value of these cells inside Name instead of {...}

Index Match in excel userform

I have my userform which contain 2 textbox for the target value and the actual value.
TextBox1 will be my target value while TextBox2 will be my actual value.
When the actual value is being key in, the userform will look into the worksheet "Target" and check whether is there any amount need to top up to reach the target value or not.
Here will be my data inside my "Target" worksheet:
Cell G1:S1 will be my target value while F2:F19 will be my actual value.
If textbox2(actual value) equal to 20A and my textbox1(target value) is 5S,
When go to the next field, textbox3 to fill in others data, it will show a pop up message to alert user to add 50ml conpac in order to reach the target value.
I have been research through webpage but seem like index match does not help in this condition. Please help.
Thank you
If you create the following named ranges: G1:S1 named "Target_Value", F2:F19 named "Actual_Value" and G2:S19 named "Additions" then:
=INDEX(Additions,MATCH("20A",Actual_Value,0),MATCH("5S",Target_Value,0))
Will return 50. You can put this in a cell somewhere or modify it to work in VBA (using Application.Worksheetfunctions)

with a VBA userform, how do you populate a combobox with a different text and value?

I am trying to populate a VBA userform combobox with a named range, and I want the text value to display in the combobox for the user to select, but the value I want returned is the cell reference to the initial named range. That way it would point back to the named range, and if I change that text, it would automatically update all the references I have on the sheet that were entered by the form.
Right now I can get the named range to populate the combobox by iterating through the range, and using an .AddItem to give it the textual value, but then it just dumps that textual value into my spreadsheet, when instead I would like it to have the cell reference from the range that populated it.
The easiest way to do this is to create an Array(X,2) X being the number of items in the array. Then you put the String you want to display in Array(1,1) and the data you want in Array(1,2) then simply populate the combo box like normal. When you and use the array to reference which item they choose. They choose item 8, you know it is Array(8,2) If you need more help please post some code.
So, after more research it would seem that I am unable to directly accomplish what I want with the combobox. I ended up just looping through the range to fill the combobox with the textual values, and then when I submit the form, I looped through the same range of values, checked it if the submitted value matches one from the range of values, and if it does, then return the .Address instead of the .Value, and that gave me the cell reference I was looking for. Too bad the combobox doesn't work more like a real html select menu, it would have made my life a little easier, but in the end I was able to make it work, it just isn't as clean as I would have liked.

Excel VBA how to select all cells in a dynamic range were the colour index is not 0

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