Creating multiple buttons in Excel - excel

I'm very new to this. However, I've created a small VBA script in Excel. This script highlights the row the button is on, asks for a name and then inserts today's date and the name entered on to some cells in the row. It's basically used to book out media that is recorded on this spreadsheet.
The issue I have is that I'll need a CommandButton for every row, and I have no idea how to get the command button to change the the row values within the script to reflect the row that the button is on. Is this possible?

I think you should specify your question more clearly, but if I understand your question right, you need to write data in the next empty cell. If that is the case, you can use this:
ActiveSheet.Range("B10").Select
If ActiveCell.Offset(1, 0) <> "" Then
Selection.End(xlDown).Select
End If
ActiveCell.Offset(1, 0).Select
This will search for and select the next empty cell in column B, starting from B10. Hope this helps!

Related

Insert values in cell in VBA if previous is empty upon each click of a button

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

How can a macro ask for what open spreadsheet to use?

Im a very novice VBAer so please forgive me for asking this, but i cannot find the answers no matter how much searching and experimenting ive done.
I am hoping to create a macro that will allow the user to match values between workbooks and update the matching data from one workbook to the other. For example, in Workbook1, column1 has values and column2 has data. In Workbook2 I would have values in column1, but no data. I would like the macro to update the data from Workbook1 column2 into Workbook2 column2. I have been able to do this using a simple VLOOKUP function, but I am not able to find how to update the macro to ask which two open workbooks and sheets to run the macro on. I am hoping to get it flexible enough to display an input box asking the user to choose which workbook/sheets to reference from, and which workbook/sheets to update the info into.
I have also not been able to figure out how to get the macro to stop when it sees a blank cell. The code i have just runs until row 5000, which is ok but I am hoping to find a better solution.
Here is the code i currently have:
Sub Match_Update()
' Match_Update
' Match 1st column and update column to right
'
'
ActiveCell.Offset(0, 1).Range("A1").Select
ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-1],[Book1]Sheet1!C1:C2,2,FALSE)"
ActiveCell.Select
Selection.AutoFill Destination:=ActiveCell.Range("A1:A5000")
End Sub
I have tried to change the 'ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-1],[Book1]Sheet1!C1:C2,2,FALSE)"' to reference objects as the table_array, but have not had much luck.
Any suggestions would be much appreciated!!!

Insert Row in VBA

I am trying to create a loop in Excel to insert an entire row when there is a value change in column A. It goes through the loop once and works perfectly. It inserts the row like it is suppose to but when it loops and the value changes again it jumps straight to end sub rather than inserting a new row. I have tried to do this multiple ways.
Below is the simple loop I made to see if it works. This code works from the bottom up.
Do Until ActiveCell = ""
If ActiveCell.Value <> ActiveCell.Offset(-1, 0).Value Then
ActiveCell.EntireRow.Insert
ActiveCell.Offset(-1, 0).Select
Else
ActiveCell.Offset(-1, 0).Select
End If
Loop
It reads to me like you are attempting to circumvent the Change event. I suggest reading the documentation particularly the Worksheet_Changedocumentation. If you try and implement the Change event and are still having issues, I suggest you edit your original question to provide a bit more clarity and offer more information.
I'm assuming that this loop is part of a larger piece of code. If so, make sure that the code is returning to the bottom of the sheet (or whatever location is desired). Since you are telling the loop to stop at an empty cell, running the code again without resetting the position of activecell will automatically find and empty cell and exit the loop.
As you said, the actual code for adding the blank rows itself works well, so it makes sense to look at the code around it.
#Bradon As I can see your code,it might be going into infinite loop [assuming you are using WorkSheet_Change event to catch any change in sheet]
Any change in worksheet trigger WorkSheet_Change event; in your case inserting row into worksheet is triggering the Worksheet_Change event
I will suggest you to post your entire code if possible
or
change you code like below [still assuming you are using WorkSheet_Change event]
add new sheet say "Config" and only once manually set A1 value to "True"
In worksheet change event use if condition
if Sheets("Config").Range("A1").Value = "True" then
call MyProcedure
end if
In Procedure
sub MyProcedure()
Sheets("Config").Range("A1").Value = "False"
your Do while loop here
Sheets("Config").Range("A1").Value = "True"

Difference between an actived cell and selected cell

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.

Macro Code to delete rows untill defined column

How do I limit a Macro to function only within certain range let's say until certain column?
I have a case where macro needs to delete the rows based on condition. I also want to put some instructions on the right and a form control button for macro but it seems like every time the code is activated, it deletes everything in that row, including the instructions and button I made. So I want to define a range where macro should be active.
I have following logic active.
First flag the item to be checked.
In loop, keep checking and deleting the rows.
Delete the rows based on an extra condition.
The code is working fine, but I want to add limitation until certain column like said.
Where do i define the range? In three separate logic, or at once on top?
Any help would be appreciated.
BR,
Manny
It sounds like you have told your code to delete the entire row. If you only want the macro to delete the data in columns A to G, for example, then use code along the lines of
Range("A" & rownumber & ":G" & rownumber).Delete Shift:=xlUp
you need to replace the line of code that deletes the whole row with code that deletes only set range. So for example instead of
Rows("12:12").Delete Shift:=xlUp
you will have
Range("A12:N12").Delete Shift:=xlUp

Resources