Excel If Else Statement For Copying Data with Checkboxes - excel

I would like to set up some code for copying some cells with check boxes
I have 30 checkboxes
I have copied the code below and modified it 30times
This is no doubt redundant
Each check box is on a row, the data it will copy is on the same row
When the checkbox is clicked the row data in the next cell will be copied and moved somewhere else
This data will be dumped somewhere below in the same worksheet
I tried creating the elseif statements, unfortunately they did not work
If ThisWorkbook.Worksheets(1).Shapes("Check Box 2").OLEFormat.Object.Value = 1 Then
Range("f2").Select
Selection.Cut
Sheets("Sheet1").Select
Range("f15").Select
ActiveSheet.Paste
Range("f15").Select
Selection.Insert Shift:=xlDown
End If
End Sub
If ThisWorkbook.Worksheets(1).Shapes("Check Box 3").OLEFormat.Object.Value = 1 Then
Range("f3").Select
Selection.Cut
Sheets("Sheet1").Select
Range("f15").Select
ActiveSheet.Paste
Range("f15").Select
Selection.Insert Shift:=xlDown
End If
End Sub
If ThisWorkbook.Worksheets(1).Shapes("Check Box 4").OLEFormat.Object.Value = 1 Then
Range("f4").Select
Selection.Cut
Sheets("Sheet1").Select
Range("f15").Select
ActiveSheet.Paste
Range("f15").Select
Selection.Insert Shift:=xlDown
End If
End Sub
It is very repetitive as you can see
Any advice on how I can write this code so it will be like a nested if statement
if checkbox 1 is true do this
if checkbox 2 is true do this
etc etc
[IMG]http://i44.tinypic.com/2db78dj.jpg[/IMG]
please advise thank you

Without knowing a little more about the workbook structure, this is the best I can come up with. THere is likely some sort of "relationship" between the CheckBoxes and the cell(s) that need to be operated on, which could allow you to use a formula or some other logic to determine the cells to cut/paste, rather than relying on If/Then or Case logic.
Sub Test()
Dim cb As Shape
Dim cutRange As Range
'## The destination doesn't change, so we put this outside the loop
' also make it a constant value:
Cosnt destRange As String = "F15"
'## Now, iterate over each checkbox control in the sheet:
For Each cb In ActiveSheet.Shapes
'## Make sure the shape is an xlCheckBox AND value = True/checked
If cb.FormControlType = xlCheckBox And cb.OLEFormat.Object.Value = 1 Then
'## Assign the cutRange based on the CheckBox name
Select Case cb.Name
Case "Check Box 2"
Set cutRange = Range("F3")
Case "Check Box 3"
Set cutRange = Range("F4")
Case "Check Box 4"
Set cutRange = Range("F5")
'etc.
'## You can add as many Case values as you need
End Select
'## One statement cuts & inserts:
cutRange.Cut Range(destRange)
Range(destRange).Insert Shift:=xlDown
End If
Next
End Sub

Related

Create a list with values from multiple Excel sheets

I am working on VBA code that will run through a dropdown menu in Sheet "voorblad".
For every value in the dropdown menu I want to copy the value "Voorblad".range"K9" and "Calculation".range"G35" and paste it in a sheet called "LIST Sheet".
The VBA is meant to create a list of all the values.
Sub CreateList()
Dim Answer As VbMsgBoxResult
Answer = MsgBox("Validation message!", vbYesNoCancel, "CreateList")
If Answer = vbYes Then
Application.ScreenUpdating = False
With Sheets("Voorblad").Range("K9").Validation
For Each rCell In Range(.Formula1)
.Parent.Value = rCell.Value
Sheets("Voorblad").Select
Range("K9").Select
ActiveCell.Copy
Sheets("LIST Sheet").Select
Range("B2").Select
Selection.PasteSpecial Paste:=xlPasteValues
Sheets("LIST Sheet").Select
ActiveCell.Offset(1, 0).Select
Sheets("Calculation").Select
Range("G35").Select
ActiveCell.Copy
Sheets("LIST Sheet").Select
Range("G2").Select
Selection.PasteSpecial Paste:=xlPasteValues
Sheets("LIST Sheet").Select
ActiveCell.Offset(1, 0).Select
Next rCell
.Parent.Value = ""
End With
Application.ScreenUpdating = True
MsgBox "Export geslaagd! Het PDF is opgeslagen in jouw Documenten"
End If
End Sub
The VBA code is not creating a list. Every time the macro runs through the code it will reselect the pre-selected cells at Range("B2") and Range("G2"). I want it to paste the values one row down. Eventually this must create a list.
To create the list properly, you need to ensure you are moving to the next empty cell on the target worksheet (List Sheet). Try something like
Sheets("LIST Sheet").Range("G2").End(XLdown).Offset(1,0).PasteSpecial Paste:=xlPastValues
This should put the value in the next blank cell. Same with column G
HTH

Excel VBA application caller copy 2 ranges

Friends, I'm very poor in programming but maybe someone is willing to help.
My spreadsheet contains 18 ranges and 2 different headers in protected area. I need to copy and combine 1 header and 1 range to another, unprotected area. User should press a button and macro brings data to new position where it can be pasted.
For button operation I have an application.caller for hiding and showing rows. I think this is a good start. I also have a copy macro for 1 set of ranges. I'd like to combine these 2 in to a new macro.
Sub Macro_copy_RIVA1()
Range("RHEAD").Copy
Range("RIVA1").Copy
Application.Goto Reference:="R1120C2"
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Selection.Cut
End Sub
My application caller for hide/show rows is (thanks to Stackoverflow)
Sub ShowHideRows()
Dim arr
'split the calling button name into an array
' (array will be zero-based)
arr = Split(Application.Caller, "_")
'**EDIT** check array is expected size...
If UBound(arr) <> 3 Then Exit Sub
If IsNumeric(arr(1)) And IsNumeric(arr(2)) Then
With ActiveSheet ' "Me" if the code is in the sheet module, else "ActiveSheet"
.Unprotect Password:=""
'arr(1) determines start row
'arr(2) determines # of rows
'arr(3) determines if rows are hidden or not
.Cells(arr(1), 1).Resize(arr(2), 1).EntireRow.Hidden = (arr(3) = "H")
.Protect Password:=""
End With
End If
End Sub`
My ranges are called :
Header:
RHEAD1
RHEAD2
Ranges:
RIVA1
RIVA2
RIVA3
.....
RIVA6
RMVA1
RMVA2
.....
RMVA12
Proposed name of button : btn_RHEAD1_RIVA1 or btn_RHEAD2_RMVA12
How can I run a macro from an application caller that performs the copying task ?
Thanks

VBA move selected range up / down (with offset?)

I am building a tool where user-selected cell contents is moved around with arrow shapes.
The code below works great to move 1 or more group of adjacent cells down.
However, reversing the code seems tricky (+1 in offset does not work :-?)
Any idea?
Thank you,
Augustin
Sub Move_Up()
Selection.Cut
Selection.Offset(-1, 0).Select
Selection.Insert Shift:=xlDown
End Sub
supposing cells are to be moved around and overwritten ones are just shifted where moved ones once were, the code could be the following:
Sub MoveUp()
Selection.Rows(Selection.Rows.count + 1).Insert Shift:=xlDown
Selection.Rows(1).Offset(-1).Cut Selection.Rows(Selection.Rows.count + 1)
Selection.Rows(1).Offset(-1).Delete Shift:=xlUp
Selection.Offset(-1).Select
End Sub
Sub MoveDown()
Selection.Rows(1).Insert Shift:=xlDown
Selection.Rows(Selection.Rows.count).Offset(2).Cut Selection.Rows(1)
Selection.Rows(Selection.Rows.count).Offset(2).Delete Shift:=xlUp
Selection.Offset(1).Select
End Sub
If you want to move a Selected block of cells up by one row then:
Sub ShiftBlockUp()
Dim r As Range
Set r = Selection
Intersect(r(1).EntireRow, r).Offset(-1, 0).Delete Shift:=xlUp
End Sub
If you want to move a Selected block of cells down by one row then:
Sub ShiftBlockDown()
Dim r As Range
Set r = Selection
Intersect(r(1).EntireRow, r).Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
End Sub

Excel Macro Repetitive IF and Else

I am currently working on an Excel VBA Macro script where in it will do a simple TRUE or False test to the active cell. My problem is, i cannot make this working until the end of the list. It only run once and ends the program. I need this VB script to perform the IF & ELSE test up to the bottom of the list.
Description of the problem:
Let's say i have a list of dates in A1 to A9999 and beside it (F1:F9999) there's also a list that has a text on it. the F1:F9999 list contains two values only. (a)SAME DATE and (b) NOT THE SAME.
Perform a True or False test in the List F1:F9999.
If the active cell value is equal to the text "SAME DATE" (TRUE), it will ignore and move to the next item in the list then perform again number 1.
If the active cell value is equal to the text "SAME DATE" (FALSE), it will insert a row above it and then move to the next item in the list then perform again number 1
The TRUE or FALSE test will run until the end of the list.
The TRUE or FALSE test will stop running if it reached the bottom of the list.
by the way, the number of items in the list is not consistent. I just put there F1:F9999 for example purposes.
here's my code!
Sub IFandElseTest()
If ActiveCell.Value = "Same Date" Then
Range(Selection, Cells(ActiveCell.Row, 1)).Select
Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
ActiveCell.Offset(1, 0).Select
Else:
ActiveCell.Offset(1, 0).Select
Range(Selection, Cells(ActiveCell.Row, 1)).Select
Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
End If
End Sub
Appreaciate if you could help me on this.
Give this a try.
Explanation:
You should avoid using .Select/ActiveCell etc. You might want to see this LINK
When working with the last row, it's better not to hard code values but dynamically find the last row. You might want to see this LINK
Work with Objects, what if the current sheet is not the sheet with which you want to work with?
The below FOR loop will traverse the row from below and move up.
Code:
Sub Sample()
Dim ws As Worksheet
Dim LRow As Long, i As Long
Dim insertRange As Range
'~~> Chnage this to the relevant sheet
Set ws = ThisWorkbook.Sheets("Sheet1")
'~~> Work with the relevant sheet
With ws
'~~> Get the last row of the desired column
LRow = .Range("E" & .Rows.Count).End(xlUp).Row
'~~> Loop from last row up
For i = LRow To 1 Step -1
'~~> Check for the condition
'~~> UCASE changes to Upper case
'~~> TRIM removes unwanted space from before and after
If UCase(Trim(.Range("E" & i).Value)) = "SAME DATE" Then
'~~> Insert the rows
.Rows(i).Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
End If
Next i
End With
End Sub
Screenshot:
Followup From Comments
It really worked! BUT, one final modification. in your code: Set ws = ThisWorkbook.Sheets("Sheet1") Is it possible is you can set the WS as the Active worksheet. The reason of this is because the name of the worksheet unique and not consistent also.
Like I mentioned, in the first link above as well in the comment, do not use Activesheet. Use CodeNames of the sheet which do not change. See the screenshot below.
Blah Blah is the name of the sheet which you see in the worksheet tab but Sheet1 is the CodeName which will not change. i.e. you can change the name of the sheet from Blah Blah to say Kareen but in the VBA editor, you will notice that the Codename doesn't change :)
Change the code
Set ws = ThisWorkbook.Sheets("Sheet1")
to
'~~> Replace Sheet1 with the relevant Code Name
Set ws = [Sheet1]
Edit:
If you leave out the r.copy line it does more or less exactly what Siddharth Rout's solution does
Sub insrow()
Dim v, r As Range
Set r = [d1:e1]
v = r.Columns(1).Value
Do
' r.copy
If v = "Same Date" Then r.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
Set r = r.Offset(1)
v = r.Columns(1).Value
Loop Until v = ""
End Sub
This does not yet include the end condition if row exceeds line 9999 but that should be easy to add ...

Convert numbers stored as text to numbers?

How can I convert numbers stored as text to numbers?
I have tried setting:
ActiveSheet.Range("H154").NumberFormat = "General"
But it doesn't work!
The only things I've found that work are using "Text to columns" or clicking the cell to edit it and then clicking Enter.
But I would really like to find a way to turn number cells in a sheet stored as text into numbers using VBA.
A general technique is to Copy PasteSpecial, Multiply by 1
In code, something like this:
Sub ConvertToNumber()
Dim rng As Range
Dim cl As Range
Dim rConst As Range
' pick an unused cell
Set rConst = Cells(1, 4)
rConst = 1
Set rng = Cells.SpecialCells(xlCellTypeConstants)
rng.NumberFormat = "General"
rConst.Copy
rng.PasteSpecial xlPasteValues, xlPasteSpecialOperationMultiply
rConst.Clear
End Sub
Just use CDbl():
ActiveSheet.Range("H154") = CDbl(ActiveSheet.Range("H154"))
I'm not a coding expert and the "Number Stored as Text" error plagued me for a long time.
I finally found this:
Delimited Text-to-Columns in a Macro
Which got me to this:
Sub ConvertTextToNumber()
Sheets("Worksheet_Name").Select
Range("A1").Select
Selection.TextToColumns _
Destination:=Range("A:A"), _
DataType:=xlDelimited
End Sub
I use this in a macro to copy & reorder columns in a new sheet:
Sub ColumnReorder()
'**********************************************************
'Paste this macro into the Workbook of each new "Employee_List_Weekly_Update"
'Functionality:
'1. Column order in the "Employee_List_Weekly_Update" worksheet changes fairly often.
' The macro will find each column by header name,
' select that column and copy it to the new sheet.
'2. The macro also converts "Employee ID#" to a number,
' removing the "Number saved as Text" error.
'**********************************************************
'Create new sheet
Sheets.Add.Name = "Roster_Columns_Reordered"
'Repeat for each column or range
'Find Column in "Employee_List_Weekly_Update" - Copy it - Paste it in "Roster_Columns_Reordered" - Employee ID#
Dim a As Integer
Sheets("Employee_List_Weekly_Update").Select
Set rngData = Range("A1").CurrentRegion
a = Application.WorksheetFunction.Match("Employee ID#", Range("A1:BB1"), 0)
Columns(a).Select
Selection.Copy
Sheets("Roster_Columns_Reordered").Select
Range("A1").Select
ActiveSheet.Paste
'Use TextToColumns to convert "Number Stored as Text "
Selection.TextToColumns _
Destination:=Range("A:A"), _
DataType:=xlDelimited
'Find Column in "Employee_List_Weekly_Update" - Copy it - Paste it in "Roster_Columns_Reordered" - Name
Dim b As Integer
Sheets("Employee_List_Weekly_Update").Select
Set rngData = Range("A1").CurrentRegion
b = Application.WorksheetFunction.Match("Name", Range("A1:BB1"), 0)
Columns(b).Select
Selection.Copy
Sheets("Roster_Columns_Reordered").Select
Range("B1").Select
ActiveSheet.Paste
'Go to "Roster_Columns_Reordered" - Add AutoFilter - Freeze Top Row
Rows("1:1").Select
Selection.AutoFilter
With ActiveWindow
.SplitColumn = 2
.SplitRow = 1
End With
Rows("2:2").Select
ActiveWindow.FreezePanes = True
Range("A1").Select
End Sub
if you want to convert a selection (even with text in it!), you can use the code by firefiend (http://www.ozgrid.com/forum/showthread.php?t=64027&p=331498#post331498)
I think the magic is in .Value = .Value
vba
Sub macro()
Range("F:F").Select 'specify the range which suits your purpose
With Selection
.NumberFormat = "General"
.Value = .Value
End With
End Sub

Resources