VBA Sort with Spinners - vba

I am working on an excel spreadsheet that contains several rows worth of data that I need to sort based on the values in Column A. Column B contains values that are controlled by Spinners also located in Column B.
What I am trying to do is create buttons that when pressed will sort all of the rows based on Column A but without breaking the spinners.
What I have tried so far:
When I just use a simple sort function, it moves the rows as required along with the spinners but it doesn't update the Cell Links on the spinners so they remain linked to the cells in the old questions.
I tried adding in a line in the VBA for the spinners that would update the spinners Cell Link when the spinner was pressed. The problem here is that excel would first run the increment portion of the spinner before updating the link, resulting in it incrementing/decrementing the old cell before updating the link.
I tried adding in a line in the VBA for the SORT Macros that would run a For loop to update all of the spinner Cell Links based on their new topleftcell value. This works for the Cell Link but it also updates the Cell Value based on what seems to be the Cell Value of the last spinner moved. Needless to say this is also a problem.
I'm not sure what else to try as I am still quite inexperienced with VBA.
If anyone has any suggestions for sorting cells that contain spinners without breaking the spinner's values or links I would be very appreciative!
Thanks in advance and please let me know if further information is required. I can include snippets of the code I've used so far but I wasn't sure if it owuld help much.

Try something like this.
Sub SORT()
Range("A2:B4").SORT. Key1:=Range("A2"), Order:=xlAscending
Call SpinnerControl
End Sub
Sub SpinnerControl()
Dim spn As Spinner
Dim rng As Range
For Each spn In ActiveSheet.Spinners
Set rng = spn.TopLeftCell
spn.LinkedCell = rng.Address(external:=False)
Next spn
End Sub

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.

Loop and Add Range in a place holder

Hello,
First of all, thanks for everyone who's helped me with my previous concern. I have another loop issue though. What I'm trying to do is store a certain range on a place holder and use for Charts. My code loops through column A to check cells that contain Product/URL and if found, will offset to the next column and will store the range highlighted on a place holder.
What I was able to do so far is to just select the cell with content in-line with cell with Product/URL. Here's my code so far.
Dim ProdCell as Range
For Each ProdCell in [A:A].SpecialCells(xlCellTypeConstants)
If Not IsEmpty(Prodcell) and Prodcell.Cells.Value = [A1] Then
ProdCell.Offset(0,1).Cells.End(xlToRight).Select
End if
Next ProdCell
I believe that if I can find a way to highlight the range using VBA coding, I will be able to loop through it. Please help.

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

Excel, VB form with Text boxes. On button click. Create new row in worksheet with data from text boxes

Ive been looking around EVERYWHERE! And nothing works. ever.
I would like to be able to have a form that you fill in ( Lets say with two textboxes2 )
When you click the button it should then add a new row below all other populated rows and put the data of the text boxes into the cells of that row.
Nothing I do works :(
Does anyone have any ideas?
My form has:
Textbox1
Textbox2
Worksheet is called Sheet1
( Ive left everything default while testing )
Any help would be epic..
Once I have this bit sorted, i can then modify to do what i want to achieve in the long run. but this basic " Add a row with some data" is just killing me.. It cant be that hard right?
Thanks
G
I believe you could follow a logic similar to that : (After the click)
Find the last populated row
Insert an empty row under it
Grab the data from the textboxes and copy them to their proper column
Does that help? If not, what part are you having trouble? I'll be happy to provide more help.
Something like this could get you started. This assumes your first text box will fill in column A and second text box fills in column B.
Sub InsertText()
Range("A65000").End(xlUp).Offset(1, 0).Select
ActiveCell.Value = TextBox1.Value
ActiveCell.Offset(0, 1).Value = TextBox2.Value
End Sub
I don't think you need to add another row if you're just filling in below the last populated cell. If so
ActiveCell.EntireRow.Insert

Copy data from one part of a userform to another automatically

I am trying to work with an Excel form using VBA that I have customized (I found the original on the web).
I have two tabs: one is called Planning-Deleted, the other is called Planning-Deleted Data. All the data that I enter in the first tab is copied to the second tab when I press a command button called Add to Database.
Is it possible to skip the command button altogether and record the data entered in cell D5 and D6 directly in the second tab and clear these cells for the next records? I have no knowledge of programming and I would appreciate as much clarity and specificity as possible.
If more details are needed, please let me know.
You'll need some trigger to cause the copy operation to happen, for example:
Using the 'Change' event, you can trigger some action any time some value is entered into a sheet:
Private Sub Worksheet_Change(ByVal Target As Range)
Sheet2.Range(Target.Address).Value = Target.Value
End Sub
This code will copy any data entered into the first sheet into the same cell on the second sheet. 'Target' in this case is the cell where data was entered.
I'm not sure if this what you want though, since you also want to clear the cells after the copy. Can you describe when the copy-and-erase should occur, if not from clicking a button?
Also, your question doesn't seem to match your question's title. Can you clarify a bit what you are trying to accomplish?