How to populate combobox dropdown in userform from Excel data? - excel

How do I populate a combobox in a userform with the values in an Excel sheet?
Say sheet name as "Reg ALL - current".
I need to populate the value from cell AI (which is a date column).
Also I need to increment the date one day from AJ to cell BF.
Example: if AI holds value (19/06/2019) then AJ should hold (20/06/2019) and so on until BF.

There are different ways. If you have one set range of cells (that's what I assume reading your question) that won't change, you could just set the RowSource property of your combobox.
For example:
Apply to your situation:
Cell AI1 holds your date
Cell AJ1 holds formula =AI1+1
Drag formula to cell BF1 (assuming you always want to add to the value in AI1, the formula will keep doing this for you)
Use RowSource property and fill in =Sheet1!AI1:BF1
Conclusion, no VBA needed at all! If I understood your question well enough that is.

Here is simple solution
Just add a button, paste this.
Dim i As Long
'Clear existing items
ComboBox1.Clear
'36 (AJ) column to 58 (BF) column
For i = 36 To 58
ComboBox1.AddItem ActiveSheet.Cells(1, i).Value
Next i

Related

vba offset with reference to cell

I need to copy a column of data to another column on a different worksheet, pasting values only. The appropriate paste column is identified in a single cell. This cell will be changed manually each time the macro is applied. So one time I might want to copy/paste in the first column, so my identifier cell is 1. The next time I might input 5 in this cell so that I offset 5 columns to the right to copy/paste data. Thank you.
You can reference the columns in a worksheet using the Columns property. You can achieve what I think you're trying to do with code like this.
Dim col As Integer
col = SomeSheet.Cells(1,1).Value
FromSheet.Columns(col).copy
ToSheet.Columns(col).PasteSpecial xlPasteValues

Enter specific text into textbox and add +1 to another cell

I'm new to excel but now i'm stuck at something.
The one thing i try to achieve is that if i add a specific word into a textbox, another cell gets +1 (so if 0 and text has been entered in the textbox, it changes to 1 etc.)
so for example:
Cell B2 = Apple
Cell H2 : value of B2
I'd like to get, if possible, one or two textboxes where i could put the type of product and another box for the amount.
Thanks in advance.
OK. Here's a solution.
Set up a named range. I entered 5 different fruit in one column and 5 different quantities in the adjacent column (doesn't matter where but must be adjacent). I named the range "Products" but any other name will do just as well.
I set up a data validation list. I used cell G3 but any other will be equally suitable. I pointed the data validation list to =INDEX(Products,0,1), meaning the first column of the Products range.
Now I added code to the worksheet. This code must be in the code sheet of the worksheet on which G3 is located. That code sheet will have a name like Sheet1 (Sheet1). Don't use a standard code module with a name like Module1. Here is the code.
Private Sub Worksheet_Change(ByVal Target As Range)
' 018
Const Trigger = "G3" ' change to suit
Dim Qty As Long
With Target
If .Address = Range(Trigger).Address Then
On Error Resume Next ' in case not found
Qty = Application.VLookup(.Value, Range("Products"), 2, False)
.Offset(0, 1).Value = Qty + 1
End If
End With
End Sub
Note that the Trigger cell is named as "G3". This must be the cell where you have the data validation drop-down.
This code will run whenever Trigger is changed. You make a selection there and the VLOOKUP function will find the quantity in column 2 of the Products range. If the item isn't found in the list it will return 0 but you can set the cell validation to prevent the entry of an item that isn't in the list. The code will add one to the quantity found and issue the result in .Offset(0, 1), meaning one column to the right of the Trigger cell.
You might want to do other things with your idea. I think the system I suggest can be adapted to whatever you have in mind, including changing the quantity in the Products list.

Is there a way to substitute a value in a formula using a UserForm?

I have a very long formula in columns I2:HM13 that contains values that need to change depending on what department uses the worksheet and these values may change over the course of the year. This formula repeats in all cells within the stated range with only the reference column/row changing. I would like to create a UserForm were the end user can input the values specific to their department in textboxes and click a commandbutton and the values in the textboxes get inserted into the formula.
I have tried some find and replace codes, but nothing I have tried has work at all. Below is the formula I am trying to modify.
=IF($H2="No","",IF(($B2-I$1)<100,"",IF(((($B2-I$1)*$C2)*1000000)<260000000000,"",IF(((I$1*$C2)*1000000)<330000000000,"",IF(AND(($B2-I$1)>=200,($B2-I$1)<=800,((($B2-I$1)*$C2)*1000000)>=620000000000,((($B2-I$1)*$C2)*1000000)<=920000000000,I$1>=375,I$1<=420,((I$1*$C2)*1000000)>=680000000000,((I$1*$C2)*1000000)<=790000000000,$C2>=1300,$C2<=2100),1)))))
The above is a small portion of the formula of interest. The values I am trying to changed based on the textbox inputs are all the values after the less than, less than or equal to, greater than, or greater than or equal to symbols.
For example in textbox 1 the user inputs 150, 150 would then replace all "100" in the formula. If the user inputs 270000000000 in textbox 2, all the "260000000000" would be replaced with 270000000000.
Your best approach would be to create sheet-scoped names which you then use in your formulas: your userform can then change the "refersto" values for those names to the user-supplied values.
Eg:
With ActiveSheet
.Names("FOO").RefersTo = txtName1.Value
.Names("BAR").RefersTo = txtName2.Value
End With
On your sheet:
=FOO/BAR
Found this solution to my question:
Private Sub CommandButton1_Click()
Dim cell As Range
For Each cell In Range("I2:HM2")
cell.Formula = Replace(cell.Formula, "100,", TextBox4.Value + ",")
cell.Formula = Replace(cell.Formula, "260000000000,", TextBox5.Value + ",")
Next cell
End Sub
I would need to repeat this format for all the values I would want to change per department.

Differences between A1 fixed referencing and formulaR1C1 relative positioning VBA

I have a macro that populates cells on a blank worksheet with values from another form worksheet using R1C1. The issue is that the position of the source data on the form worksheet changes as users add rows to input their data. The A1 solution to this would be to use a fix Cell column reference and allow the reference for the row to change 9ex. Cell=Sheet1!$A25, which could then change to Cell=Sheet1!$A26 as a row is added above. How can I make my FormulaR1C1 change in the same way as my A1?
Correct situation:
Cell A1=Sheet1!$F13
Therefore, ActiveCell.FormulaR1C1 = "=Sheet1!R[12]C6"
But when I add a row above F13:
Cell A1=Sheet1!$F14
But, ActiveCell.FormulaR1C1 = "=Sheet1!R[12]C6", Does not respond to the shift row down

EXCEL - dropdown and fill cells

Good night.
I'm having some troubles to get what i need done.
I have some cells in a sheet that needs to be filled every day, manually.
I have also a dropdown with all the months, and another one with the days.
Is it possible to save data in specific cells for the selected dropdown values?
Something like for each day mantain different data in the same cells.
Thanks.
I'm not exactly sure what you are requesting. What I think you are trying to do is:
Enter something into a cell
Select the Month and Day from the dropdown lists
Record the value of the dropdowns into cells near the value you entered.
Is this correct? If so, you would need to need to write a VBA macro that would take the values of the dropdown and write them into the cells where you needed them to be (presume next to your entered text). I think this would do that for you.
Sub writeDropdowns()
'This will take the values from cells B1 and C1
'and record this in the two cells next to the selected cell
Selection.Offset(0, 1).Value = ActiveSheet.Range("B1")
Selection.Offset(0, 2).Value = ActiveSheet.Range("C1")
End Sub
It assumes that the dropdown values are in cell B1 and C1 of the same sheet.You could then link this macro to a Form button. This is very simple and doesn't check for any errors, like if there is no cell selected. It should be a good starting point though.

Resources