How to delete added value in cells in Excel Worksheet - excel

From the picture on the left side, it is the input and after the user presses the button the output will appear on the right side.
My problem is that after the user presses the button and the result appears. I want the inputted data on the left side to disappear so that the user can re-input the data again and again
What do I need to add in my code so that it will give the result I want.
This is my code:
Private Sub CommandButton1_Click()
Dim i As Long, j As Long
Dim wks As Worksheet
Set wks = Worksheets("Sheet1")
Set AddNew = wks.Range("M65356").End(xlUp).Offset(5, 0)
For i = 1 To 15
For j = 1 To 7
AddNew.Cells(i, j) = wks.Range("B1").Cells(i, j)
Next j
Next i
End Sub

Just add :
wks.Range("B1").Cells(i, j) = ""
After your AddNew line

Your current code skips over the last inputline (15). But maybe consider to not use loops but to transfer the data in one go:
Private Sub CommandButton1_Click()
Dim wks As Worksheet: Set wks = Worksheets("Sheet1")
Dim AddNew As Range: Set AddNew = wks.Range("M65356").End(xlUp).Offset(5, 0)
AddNew.Resize(16, 7).Value = wks.Range("B1:H16").Value
wks.Range("B1:H16").Value = ""
End Sub

Related

How to link each items in a combobox to a specific row?

I am creating a userform that is entering data on a form. And in that userform I have a Combobox that list out all the products. For each product there a row and the data that is being inputted in would only be on that row.
Private sub cwbSave_Click()
Dim ws as Worksheet
Set ws = ActiveWorkbook.ActiveSheet
with ws
Select Case cbProduct
Case Is = "Apple"
With ActiveSheet.Range("A14:P14")
.Font.Bold = True
End With
ws.Cell(14,4) = Me.tbPrice
ws.Cell(14,5) = Me.tbColor
ws.Cell(14,6) = Me.tbSell
Case Is = "Pineapple"
With ActiveSheet.Range("A15:P15")
.Font.Bold = True
End With
ws.Cell(15,4) = Me.tbPrice
ws.Cell(15,5) = Me.tbColor
ws.Cell(15,6) = Me.tbSell
End Select
End With
End Sub
But the thing is, I got like 30 products. And it a lot of manually putting in. I was wondering if there an easier way to code this.
Thank you
There are several ways to do this.. here is one:
In the UserForm_Initialize, add the below code:
Private Sub UserForm_Initialize()
Dim aValues As Variant: aValues = WorksheetFunction.Transpose(ThisWorkbook.Worksheets("Sheet2").Range("A2:A5")) ' Change sheet name and range to where your products are
Dim iRow As Long
' Clear combobox
Me.cmbCmbBox.Clear
' Fill combobox with the values from product range
For iRow = LBound(aValues) To UBound(aValues)
Me.cmbCmbBox.AddItem aValues(iRow)
Next
End Sub
Above code uses your product range to populate the combobox. Now in cmbCmbBox_Change, add the following code:
Private Sub cmbCmbBox_Change()
Dim oWS As Worksheet: Set oWS = ThisWorkbook.Worksheets("Sheet2")
Dim rProdRange As Range: Set rProdRange = oWS.Range("A2:A5")
Dim rItemRange As Range
' Find the selected item
Set rItemRange = rProdRange.Find(Me.cmbCmbBox.Value)
' Set value in the sheet
If Not rItemRange Is Nothing Then
oWS.Cells(rItemRange.Row, 4) = Me.tbPrice
oWS.Cells(rItemRange.Row, 5) = Me.tbColor
oWS.Cells(rItemRange.Row, 6) = Me.tbSell
End If
End Sub
You can add validation for when product is not found

Save data from multiple columns in combobox to available cells

I'm not a programmer for the profession, I'm a system administrator who usually brings together the puzzle when I try something. Now I would need help simplifying an Excel form to make it useful. It will be used by me.
I have a combobox that contains 4 columns. I also have a button. When you click the button, I want to save data from the four columns in my combobox to the next available row of cells starting from row 3. I want data to be saved only to row 30. I have tested back and forth but do not get it, so I've completely deleted the code. Any ideas?
Sheet named "Data"
I use ListFillRange in my ComBobox for the data source.
Private Sub CommandButton1_Click()
Call SaveComboBoxData
End Sub
Sub SaveComboBoxData()
End sub
Using ActiveX controls
ComboBox1
CommandButton1
You can tidy this up further,
Note:
You appear to be building a database so consider the preferred option of using an access database to store this data
If you have Excel 2016 you could be using Data Entry Forms instead which are dead simple.
Assume using ActiveX combobox. You will need to alter this
Set sourceCombo = sourceSheet.OLEObjects("Combobox1").Object
if working with a form control.
Code for the type of operation you are describing:
Code pane for Order sheet:
Option Explicit
Private Sub CommandButton1_Click()
AddRecords
End Sub
Standard module
Option Explicit
Public Sub AddRecords()
Dim wb As Workbook
Dim sourceSheet As Worksheet
Dim targetSheet As Worksheet
Set wb = ThisWorkbook
Set sourceSheet = wb.Worksheets("Order")
Set targetSheet = wb.Worksheets("Data")
Dim lastRowTarget As Long
Dim sourceCombo As ComboBox
Set sourceCombo = sourceSheet.OLEObjects("Combobox1").Object 'assume activex object
Dim lRow As Long
Dim lCol As Long
Dim nextRow As Long
With sourceCombo
For lRow = 0 To .ListCount - 1
If lRow = sourceCombo.ListIndex Then
nextRow = GetNextRow(targetSheet)
If nextRow = 31 Then
MsgBox "End row of 30 reached"
Exit Sub
End If
For lCol = 0 To .ColumnCount - 1
targetSheet.Cells(nextRow, lCol + 1) = .List(lRow, lCol)
Next lCol
Exit For
End If
Next lRow
End With
End Sub
Private Function GetNextRow(targetSheet As Worksheet) As Long
With targetSheet
GetNextRow = IIf(.Cells(.Rows.Count, "A").End(xlUp).Row < 3, 3, .Cells(.Rows.Count, "A").End(xlUp).Row + 1)
End With
End Function
Code in action:
References:
How to get selected value in multicolumn listbox
How can I find the index of the selected choice in a combobox?
How to create a data entry form

Creating checkboxes in userform depending on parameters

I want an user to select on which sheets he wants to create a new line of text. But the number of sheets he is able to select may vary over time and I don't want to hardcode the sheets' name.
Here is an example ("o" represents the checkbox) of what I aim to do:
o 01.2013
o 07.2013
o 01.2014
o 07.2014
I created an userform with an empty frame to put my checkboxes, and added this bit of code to the userform:
Private Tck(10) As MSForms.CheckBox
Private Sub UserForm_Initialize()
Dim ws As Worksheet
Dim i As Long
i = 1
For Each ws In ActiveWorkbook.Worksheets
If Left(ws.Name, 3) = "T2M" Then
Set Tck(i) = Frame1.Controls.Add("Forms.Checkbox.1", "Checkbox" & i)
Tck(i).Caption = Right(ws.Name, 7)
i=i+1
End If
Next
End Sub
But it only adds one checkbox with the last sheet which validates the if test.
I tried to make an offset between the two iterations but I can't modify the position of the Tck(i) using Tck(i).top for example.
I also tried the method from the answer of this question : Adding controls to a frame in an Excel userform with VBA but it dosen't work either.
Your checkboxes are there, you just can't see them because they're overlayed on top of each other. You had the right idea with changing the 'Top' value.
Public Sub addCheckboxes()
Dim ws As Worksheet
Dim i As Integer
Dim tck As MSForms.CheckBox
Dim offset As Integer: offset = 5
For i = 1 To Worksheets.Count
Set ws = Worksheets(i)
Set tck = Frame1.Controls.Add("Forms.Checkbox.1", "Checkbox" & i, True)
tck.Top = offset
offset = offset + 15
Next i
End Sub

Copy row from another workbook when it has an exact value

I have kind of a basic question. I want to copy rows from workbook "WB1" to workbook "WB" if a cell (i,4) has an exact known value. The code I have tried to write is not working, what can I do do make it work? Hope someone can help me :)
Private Sub CommandButton1_Click()
Dim i As Integer
For i = 8 To 300
If Workbooks("WB1").Worksheets("Commodity Action Plan").Cell(i,4).Value = "Zamet" Then Workbooks("WB1").Worksheets("Commodity Action Plan").EntireRow.Copy
Workbooks("WB2").Worksheets("Action plan").EntireRow.Paste
End If
Next i
End Sub
I copied and checked your code and it wouldn't compile due to a few errors..
Your If statement was on one line, when it should be
If ValueToEvaluate = true Then
'code to execute goes here
End If
or
If ValueToEvaluate = True Then 'code to execute goes here
If you have the full statement on one line then you don't need the End If.
2nd problem is that you are trying to get the entirerow property of a sheet,
Workbooks("WB1").Worksheets("Commodity Action Plan").EntireRow.Copy
this exists on a range object, so you probably wanted something like
Workbooks("WB1").Worksheets("Commodity Action Plan").Rows(i).EntireRow.Copy
Rather than using Paste you can specify a destination (range) as the second argument for the Copy function, which is easier and less prone to errors than the copy & paste 2 stage method.
Try something like:
Private Sub CommandButton1_Click()
Dim i As Long 'Change to long so we don't get an error past row 32767
Dim outRow as Long
Dim sourceWs As Worksheet, destWs As Worksheet
Set sourceWs = Workbooks("WB1").Worksheets("Commodity Action Plan")
Set destWs = Workbooks("WB2").Worksheets("Action plan")
outRow = 1
'For testing
'Set sourceWs = Sheet1
'Set destWs = Sheet2
For i = 8 To 300
If sourceWs.Cells(i, 4).Value = "Zamet" Then
sourceWs.Rows(i).EntireRow.Copy destWs.Rows(outRow)
outRow = outRow + 1
Application.CutCopyMode = False
End If
Next i
End Sub

How to get a name of control by name?

I have a simple function where there's a combo box. If combo box's value is equal to "Disable", I'll disable textbox B. There are many combo boxes with their corresponding textbox B, arranged in rows and named by hand. If combo box A is named Product1, textbox B will be named Product1_status
I was thinking something like:
If value_of_a = "disable" Then
Dim name_of_b as String
name_of_b = Me.Combo.Name + "_status"
get_object_by_name(name_of_b).Enabled = False
End If
How do I do this?
I'm not sure how you are calling this, but here's a self-contained procedure that should help:
Sub test()
Dim ws As Excel.Worksheet
Dim ProductCombo As OLEObject
Dim ProductText As OLEObject
Set ws = ThisWorkbook.Sheets(1)
With ws
Set ProductCombo = .OLEObjects("Product1")
Set ProductText = .OLEObjects(ProductCombo.Name & "_status")
ProductText.Enabled = ProductCombo.Object.Text <> "Disabled"
End With
End Sub
EDIT: I really hate worksheet controls - I start from scratch every time I program them! Nonetheless, I thought I'd add this subroutine which resets every textbox whose name fits the patter Product#_status, according to its paired combobox. The logic does assume the names start with Product1, Product2, etc., without a gap in the numbering:
Sub test2()
Dim ws As Excel.Worksheet
Dim ctl As OLEObject
Dim i As Long
Dim ProductComboboxesCount
Dim ProductCombo As OLEObject
Dim ProductText As OLEObject
Const ControlPrefix As String = "Product"
Set ws = ThisWorkbook.Sheets(1)
With ws
For Each ctl In .OLEObjects
If TypeOf ctl.Object Is MSForms.ComboBox And Left(ctl.Name, Len(ControlPrefix)) = ControlPrefix Then
ProductComboboxesCount = ProductComboboxesCount + 1
End If
Next ctl
For i = 1 To ProductComboboxesCount
Set ProductCombo = .OLEObjects(ControlPrefix & i)
Set ProductText = .OLEObjects(ControlPrefix & i & "_status")
ProductText.Enabled = ProductCombo.Object.Text <> "Disabled"
Next i
End With
End Sub
VBA
Edit: (Change for an actual VBA macro)
Sub Macro1()
'
' GetControl By Name
'
If value_of_a = "disable" Then
GetControl(ComboBox1.Name + "_status").Enabled = False
End If
End Sub
Function GetControl(nameOfControl As String) As OLEObject
Dim ctrl As OLEObject
For Each ctrl In ActiveSheet.OLEObjects
If ctrl.Name = nameOfControl Then
Set GetControl = ctrl
End If
Next ctrl
End Function
VB.Net
Code for VB.Net if anyone wants it for that reason:
Sub Main()
If value_of_a = "disable" Then
GetControl(ComboBox_1.Name + "_status").Enabled = False
End If
End Sub
Function GetControl(nameOfControl As String) As Control
For Each ctrl In Me.Controls
If ctrl.Name = nameOfControl Then
Return ctrl
End If
Next ctrl
Return Nothing
End Function

Resources