i create some table, and make a datestamp when i enter data
in column "A" the date is shown in column "E" , so i know when data is entered.
I put checkbox in column "F", but I need help with next:
When checkbox is checked the date must be written in column "G"
The background from "A to D" (but only in this row) must be changed to red color
The data from A, B and C must be copied to another sheet also in A,B and C
I hope someone will help me because i stuck :(
I also attack a screenshot.
Thanks in advance
The procedures will help you get the macro operational, ensure you try the code on a copy of your workbook
Add a command button (ActiveX control) and paste the macro
Note: You need to ensure that you place the Command Button in the worksheet where you have your check boxes.
Step 1:
Copy the “LoopCkBoxesAddDateStampClrCelsCopy()” macro
Sub LoopCkBoxesAddDateStampClrCelsCopy()
Dim lRow As Long
Dim CkBx As OLEObject
'For/Next loop cycles through all checkboxes in worksheet
For Each CkBx In ActiveSheet.OLEObjects
lRow = Sheet2.Cells(Rows.Count, 1).End(xlUp).Row
'If the checkbox is checked then accomplish the lines of code between the If/End If
'The part after the "And" ensures that if a date is in the cell it will not be over written
If CkBx.Object.Value = True And CkBx.TopLeftCell.Offset(, 1).Value = "" Then
'Next line of code puts the date in first cell to left of the checkbox
CkBx.TopLeftCell.Offset(, 1).Value = Format(Date, "mm-dd-yyyy")
'Next line of code colors the first 4 cells in checkboxs' row
CkBx.TopLeftCell.Offset(, -5).Resize(, 4).Interior.Color = vbRed
'Next line of code copies the first 3 cell in the checkbox row to the first empty row on sheet 2
Sheet2.Cells(lRow, 1).Offset(1).Resize(, 3).Value2 = CkBx.TopLeftCell.Offset(, -5).Resize(, 3).Value2
End If
Next CkBx 'loops to the next checkbox
End Sub
Note: If you plan on pasting your cells to a different worksheet, please change both Sheet2s in the code.
Step 2:
On the Developer tab, in the Controls group, click Insert, and then under ActiveX Controls, click the Command Button.
Step 3:
Click the worksheet location where you want the command button to appear.
Step 4:
Right click the button, click View Code. This will launch the Visual Basic Editor.
Step 5:
In the VBE, highlight the Private Sub CommandButton1_Click() and the End Sub, and Paste the macro
Step 6:
Close the Visual Basic Editor, and click Design Mode Button image to ensure design mode is off.
Step 7:
Select the checkboxes in your worksheet and click the button to run the macro
Related
For a work project I'm trying to build an excel sheet that lets a user input a number in a dialogue box (or cell) and once a confirmation button is pressed, all numbers in a specific range shall be incremented by the specific value entered by the user. My range is B2:B100 and all cells are either filled with numbers or are empty.
When the user completes his work they'd enter their value in e.g. cell J3 and then press a toggled button underneath. J3 will be cleared and all values in B2:B100 would be incremented by the number entered.
Any help or direction to resources that would cover this would be appreciated. Many thanks.
1) In Excel, Press Alt + F11
2) It will open the VBA editor. copy paste the following code in the editor.
Sub addNumbers()
Dim i As Long
Dim incrementValue As Long
incrementValue = Cells(3, 10).Value
For i = 2 To 100
Cells(i, 2).Value = Cells(i, 2).Value + incrementValue
Next i
End Sub
3)Save this code
4)Go to Developer --> Insert --> Form Controls (Button)
This will open the Assign MAcro window. Select addNumbers. Click on OK.
5) Now you can enter the Desired values in Cell J3 and then click on this button. It will increment Cells B2:B100 by that particular value.
I want to delete the spin box relative to the active cell (Column A, same row).
I think the issue is in using .Value in the IF statement, .Address did not work .Value doesn't delete the active Check Boxes but it does delete all the other Check Boxes & Spin Buttons.
Spreadsheet Intended Use
In Column A there are check boxes with IF statements that when checked (true), add the date in the previous row +1 to current row Column B, a spin button in Column D (linked to column C) and a Spin Button in column F (linked to Column E). This coding is working.
When the check boxes are unchecked (False), I have code to clear the contents of the cells in that row using ActiveCell.Offset (the A column cell) and I want to delete the spin buttons so they can't be accidentally used to add values to cleared cells.
Another Submit button will simply copy the data entered by users and paste in another sheet for analysis. This button will also reset the sheet.
Below code is only trying to delete one Spin Button, I will copy, paste and update the offset for the second Spin Button when it works. The Spin Button with the red arrow should be the only one deleted.
Original code was found in the below post. I tried to adapt it to use a variable.
VBA-delete shapes
Sub RemoveSpinBoxes()
Dim sh As Shape
Dim OptionOneSpin As Range
Set OptionOneSpin = ActiveCell.Offset(0, 3)
For Each sh In ActiveSheet.Shapes
Debug.Print sh.Name
Debug.Print sh.TopLeftCell.Address
Debug.Print sh.BottomRightCell.Address
If sh.TopLeftCell.Value = OptionOneSpin And sh.BottomRightCell.Value = OptionOneSpin Then
Debug.Print sh.Name; " is deleted!"
sh.Delete
Else
End If
Next
End Sub
Found the solution. Just copy, paste the block and offset differently for more shapes.
Sort of understand how it's working. As #DecimalTurn suggested in regards to the cells implicit value, it's returning a False now and that is why it's working. Also using the intersect parameter makes it more reliable I guess?
Solution source here Adapted from Bukimis second post.
Sub RemoveSpinButtons()
Dim Shape As Shape
For Each Shape In wsSpinButtons.Shapes
If Not Intersect(Shape.TopLeftCell, ActiveCell.Offset(0, 3)) Is Nothing Then
Shape.Delete
Exit For
End If
Next Shape
End Sub
I have created a drop-down list in cell R5 containing names, lets call them Name1 Name2 Name3. I'd like when the user selects a certain name the sheet will scroll down to a specific row. For instance, if Name 1 is selected I'd like it to go to row 2, if Name2 is selected row 10, and Name3 row 18. The list is on the same worksheet as the data I'm wanting to scroll to. Is there some code I can use to do this?
You would need to use Sheet Events to handle this. Something like this:
In your Worksheet Module of the worksheet that has your input range, put this code
Private Sub Worksheet_Change(ByVal Target As Range)
Dim InputRange As Excel.Range
Set InputRange = Me.Range("R5")
'// Check if the change is happening in your dropdown cell
If Not Intersect(Target, InputRange) Is Nothing Then
Select Case InputRange.Value
Case "Name1"
Application.ActiveWindow.ScrollRow = 2
Case "Name2"
Application.ActiveWindow.ScrollRow = 10
Case "Name3"
Application.ActiveWindow.ScrollRow = 18
Case Else
'//...
End Select
End If
End Sub
Edit:
If you're having trouble getting this to work. Try adding a breakpoint by clicking in the area to the left of the code. A breakpoint will halt execution when the flow of code reaches that point. This is one way to figure out if Excel is even TRYING to run this block of code.
Debugging Excel Code
Say we put a little jump table in columns S and T like:
The row numbers are in column T. We put the drop-down in R5 and the following code in the worksheet code area:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim R5 As Range, v As String, r As Range
Set R5 = Range("R5")
If Intersect(Target, R5) Is Nothing Then Exit Sub
v = R5.Value
Set r = Range("S:S").Find(what:=v, After:=Range("S1"))
Application.Goto Range("A" & r.Offset(0, 1).Value)
End Sub
Whenever the user picks a new name in cell R5, the code will jump to the row listed in column T.
Because it is worksheet code, it is very easy to install and automatic to use:
right-click the tab name near the bottom of the Excel window
select View Code - this brings up a VBE window
paste the stuff in and close the VBE window
If you have any concerns, first try it on a trial worksheet.
If you save the workbook, the macro will be saved with it.
If you are using a version of Excel later then 2003, you must save
the file as .xlsm rather than .xlsx
To remove the macro:
bring up the VBE windows as above
clear the code out
close the VBE window
To learn more about macros in general, see:
http://www.mvps.org/dmcritchie/excel/getstarted.htm
and
http://msdn.microsoft.com/en-us/library/ee814735(v=office.14).aspx
To learn more about Event Macros (worksheet code), see:
http://www.mvps.org/dmcritchie/excel/event.htm
Macros must be enabled for this to work!
Right click on the cell. At bottom of the popped up window you find "Hyperlink". Click it. Another window opens. There select the sheet and enter the cell number where you want to go. That's all. In this cell the address given under hyperlink appears. If you give a name to it that name appears. Thereafter whenever you click on this cell/name in this sheet you go to the cell in the sheet specified under "Hyperlink". You can enter data in the new place. But you won't be able to come back to this cell on pressing enter. When you press "Enter" in the new place you go to the next cell in that sheet as usual. I used another hyperlink to come back. It is working for me. I Hope this is a facility provided by excel for jumping easily to a new location based on the hyperlink. Hope there won't be any cascading effect. I hope this is exactly what you wanted.
Press TAB on your keyboard. It might work. Just try it.
I am looking at running test on survey results. For the column headers for each question they are IMP1, IMP2, etc. What I want to be able to do is place the question in this cell so that when you click on the header you can see the question but from the overview of the file all the user can see is IMP1.
Not sure if that wording makes sense but basically I want the text in the formula section when you click on a cell. When the cell isn't selected it should just show IMP1.
This is specifically for the single cell A1, but can be expanded to process all the cells in column A. First enter this in the cell:
IMP1What is the meaning of life ??
and then place the following Event Macro in the worksheet code area:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim A1 As Range
Set A1 = Range("A1")
l = Len(A1.Text)
If Intersect(A1, ActiveCell) Is Nothing Then
A1.Characters(Start:=1, Length:=l).Font.ColorIndex = 1
A1.Characters(Start:=5, Length:=l).Font.ColorIndex = 2
Else
A1.Characters(Start:=1, Length:=l).Font.ColorIndex = 1
A1.Characters(Start:=1, Length:=4).Font.ColorIndex = 2
End If
End Sub
If you click on A1, you will see:
and if you click off the cell, you will see:
Because it is worksheet code, it is very easy to install and automatic to use:
right-click the tab name near the bottom of the Excel window
select View Code - this brings up a VBE window
paste the stuff in and close the VBE window
If you have any concerns, first try it on a trial worksheet.
If you save the workbook, the macro will be saved with it.
If you are using a version of Excel later then 2003, you must save
the file as .xlsm rather than .xlsx
To remove the macro:
bring up the VBE windows as above
clear the code out
close the VBE window
To learn more about macros in general, see:
http://www.mvps.org/dmcritchie/excel/getstarted.htm
and
http://msdn.microsoft.com/en-us/library/ee814735(v=office.14).aspx
To learn more about Event Macros (worksheet code), see:
http://www.mvps.org/dmcritchie/excel/event.htm
Macros must be enabled for this to work!
Besides using Comments you could use a VBA subroutine to do this based on the worksheet's SelectionChange event. In your VBE, double click the worksheet where this event is taking place in the VBAProject pane. In that code window place the following:
'Global Variable to hold the last column A cell that was clicked into
Private lastClicked As Range
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
'Did we click out of a column A cell?
If Not lastClicked Is Nothing Then
If Target <> lastClicked Then
'Copy the holder value from column C back to Column A
lastClicked.Value = lastClicked.Offset(, 2).Value
Set lastClicked = Nothing
End If
End If
'Detect a click into column A
If Target.Column = 1 Then
'Update the global "lastClicked" variable
Set lastClicked = Target
'Move the holder text to column C
Target.Offset(, 2) = Target.Value
'Move the question text from column B to target
Target.Value = Target.Offset(, 1).Value
End If
End Sub
This set up is assuming that your questions (the holder text like IMP1) is in Column A of the worksheet, and that you would have the entire question hidden (I assume) in Column B. Also that Column C would be empty so that we could temporarily hold the holder text (although you could stuff that into it's own global variable as well).
Example:
I have Sheet1 that looked like these:
In Sheet2 I want an output like these (Here I want to get all rows with the Item 'CAMERA'):
I have a hard time composing formula to product Sheet2, can you help me what formula to use?
This macro should do the trick:
Step 1: Do Alt+F11 to open the 'Microsoft Visual Basic for Applications'
Step 2: Hit F7 to open up a blank code sheet
Step 3: Copy the below coding onto the sheet:
Sub FilterandCopy()
Dim Equipment As String
Equipment = InputBox("Which piece of equipment?")
With ActiveSheet.Range("A:D")
.AutoFilter Field:=1, Criteria1:="" & Equipment & ""
End With
ActiveSheet.UsedRange.SpecialCells(xlCellTypeVisible).Copy _
Destination:=Worksheets("Sheet2").Range("A2")
Sheets("Sheet2").Range("A1").Value = "" & Equipment & ""
End Sub
Step 4: Hit the red x to close the visual basic.
Step 5: In your spreadsheet, go to the developer tab, and click the 'Macros' button.
Step 6: Select the macro just created (called FilterandCopy) and click Run. (Note: A pop-up will come up asking for the equipment type, type in CAMERA to get the camera items)
I like the solution you were given,
however here is another idea that might help you :
* this assumes that you selected "by hand" from the filter a SINGLE value.
Public Sub CopySheet()
' Clear prev data
Sheet2.Rows.ClearContents
Sheet2.Rows.Delete
' Copy filterd data
Sheet1.Activate
Sheet1.UsedRange.Select
Selection.Copy
' Paste the filterd data
Sheet2.Activate
Sheet2.Range("A2").Select
ActiveSheet.Paste
' Copy the filtered "word" to sheet2, for example- "Camera"
Sheet1.Range("A2").Copy
Sheet2.Range("A1").Select
ActiveSheet.Paste
' Increase the size of the word above.
Sheet2.Range("A1").Font.Size = Sheet2.Range("A1").Font.Size + 5
End Sub