I'm new programming at vba and I can't understand what's the difference between an actived cell and a selected cell.
I'm making a very simple example I found on internet about press a button and add the values of certain lines.
This is my example:
When I press the button that says "Rellenar" I want the information about countries goes to dropdown menu.
I made this:
Private Sub CommandButton4_Click()
Range("k6").Activate
Do While Not IsEmpty(ActiveCell)
micombo.AddItem ActiveCell.Value
ActiveCell.Offset(1, 0).Activate
Loop
End Sub
Where "k6" is the 1st cell where you can find information, but I tried to change the activate value to select value and the programm works exactly the same with both.
Can someone explain me the difference between both one?
From my understanding an Active cell is the cell the user is currently on. E.G the highlighted cell if you are in a spreadsheet.
A Selected cell is a cell that has been clicked. You can have more than one selected cell. (Usually by holding down CTRL and selecting multiple cells) However the only active cell is the one the user is currently on.
Another example for this is when you are selecting multiple images on your computer. If you click one then that's your active cell. Whereas if you click multiple then the clicked cells are "Selected" but only your most recent click is an active cell.
Related
I have this simple vba,
Private Sub CommandButton7_Click()
Range ("F1:F100").Value=Time
End Sub
As you already know, this will insert the current time in cells from F1 to F100(all at once) whenever I click the button once.
However, I want to insert time in each cells for each click. If I click the button first time, only f1 should be filled in. The second click should fill in f2 only because the previous cell is empty and so on and so forth.
I am not sure if it should be done in loop but I was unable to find references. I am trying to find a simpler code to achieve this.
Thanks commentators for your assistance.I just put the answer below in case any one needs it. The code below served my objective.
Once a cell is filled in upon clicking, you can directly go to the next cell and fill in the cell value upon clicking the button again,
Dim timeCell as Range
Set timeCell = Range("F" & rows.count).End(xlUp).Offset(1)
timeCell.value = time
I have a userform with a combobox. The combobox selections come from a defined name list. I am trying to figure out how to have the code search for the combobox selection in a range of cells on the active sheet and select the cell to the right of the combobox selection in that range. I am fairly new to VBA and learning as I do it. Esentially, if "Car" is chosen in the combobox, find "car" in a range of the active sheet and select the cell to its right. Thanks in advance for any assistance you maybe able to provide.
This should work:
ActiveSheet.Cells.Find(UserForm1.ComboBox1.Value).Offset(0, 1).Select
Use your UserForm name in place of UserForm1, same goes for CombBox1. Keep in mind that it will fail if you do not have combobox value (the one you are looking for) on the sheet.
Also include vbModeless next to your UserForm1.Show to allow cell selection.
I need a formula in MS Excel to get the last updated timestamp everytime any cell in a particular row is updated.I am using =if($2:$2="","",NOW()) but when one cell is updated, every cell even of other row having similar formula is updated with the latest time-stamp. What can be the problem?
I need only formula not a macro/excel program.
You will need to create a VBA macro in the worksheet change event. You can easily input a timestamp as a string into your row depending on the row of the changed cell. There is no formula that can do what you're asking.
To get to the worksheet change event, go to File, and then Options. Then go to Customize Ribbon. Check the box next to Developer in the column on the right. Under the newly added Developer tab in the Ribbon, select Visual Basic on the far left. Find you worksheet in the Project window on the left hand side of the Editor that opens up. You may need to expand your Workbook and a folder called Excel Objects. Double click on the sheet to get to its code. A window will open with two drop downs at the top. On the left, go to Worksheet. On the Right, select Change. In between Private Sub Worksheet_Change(ByVal Target As Range) and End Sub, you can add the code that will run whenever the worksheet is changed.
This is the code you would use, replacing "5" in Cells() with the column you want to put a timestamp in.
Dim i As Integer
i = Target.Row
Dim t As String
t = VBA.Now
Cells(i, 5) = t
If the cell to contain the timestamp is B3 and the cell to watch is C3 the formula is:
=IF(C3<>"",IF(B3="",NOW(),B3),"")
I am doing a large amount of data entry but down a list which is already populated i.e. I am changing slightly the entries if they meet certain requirements.
I was wondering if anyone knew of a way to get a cell to change colour once it has been selected?
The work flow I want is:
Select cell at the top --> make alteration if necessary --> press enter to go down to next cell --> the cell changes from red to green.
The idea is that when I take a break or check different values in the same excel sheet I can very easily find where I left off without having write down the row number.
It's much easier to change colour of the cell when you select it (rather than after deselecting). To do this:
Right-click the sheet tab
Click 'view code'
Paste the following into the window that pops up:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Target.Interior.ColorIndex = 33
End Sub
Try it.
So I'm trying to create an excel sheet that handles a user input/output task (I know that this is a kludge at best and offensive at worst). So I want the user to be able to edit the input cells (and only the input cells), and select (but not edit) the outputs.
Example: Grid A1:C3. A1:A3 are completely locked - unselectable. B1:B3 are selectable and editable. C1:C3 are selectable and uneditable.
Is this possible? My gut is telling me no, but I thought I'd ask anyway.
It is possible using worksheet protection:
In the Format Cells dialog of the cells B1:B3, uncheck the "Protected" checkbox in the last tab (Protection). Then protect the worksheet (right click the worksheet tab at the bottom->Protect Sheet). Once the sheet is protected, the user can only edit those cells that are unprotected.
Regarding the selection of C1:C3 and non-selection of A1:A3 - you can allow/prevent the user from selecting protect cells in the same sheet protection dialog. However, this is a sheet wide setting, so be default you can only fully prevent selecting all protect cells or allow selection of all cells.
If you only want prevent selection of A1:A3 because you don't want the user to see the formula, just check "Hidden" in the Format cells dialog - this way the user does not see the formula.
If you really need to separate between selectable and unselectable, either split across two worksheets - or use a little VBA macro. To do so, open the VBA editor (Alt+F11) and double click your worksheet in the top-left list. In the code window, enter the following code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Me.ProtectContents And Intersect(Target, Range("B1:C3")) Is Nothing Then Range("B1").Select
End Sub
This way, every time a cell outside your required range is selected, B1 is selected instead