In a spreadsheet, I have five columns with the following headings
"Product Code" in cell B3
"Product Description" in cell C3
"Dozens per case" in cell D3
"Cases per pallet" in cell E3
"UOM" in cell F3 (which means Unit of Measure)
On my user form, I have a combo box that the user will select the product from, a command button and a text box. The values of that combo box are populated from the spreadsheet Product Code column.
Once the user selects the product from the combo box and enters a value in the text box (called "txtbxdz") and clicks the command button, a formula will be performed.
Currently the values are hard coded into the program as shown in the formula below.
Private Sub cmdbtnPrint_Click()
Dim textValUp As Long
Dim textValDown As Long
Dim txtUOM As String
Dim txtCs As Long
Dim txtDz As Long
Case Is = "4120-5-01 (ALLERGY 180MG 5CT)"
txtDz = 2
txtCs = 200
txtUOM = "DZ"
End Select
textValUp = ((txtbxdz.Value) / txtDz / txtCs) + 0.5 - 1E-16
textValDown = ((txtbxdz.Value) / txtDz / txtCs) - 0.5 + 1E-16
End Sub
Yes, if I understand your answer correctly. If I recall correctly what a VLookup does similar to that. Let me give an example. If the user selects the 4120-5-01 (Allergy 180mg 5ct) from the combo box , an unknown line of code at this point, will loop through the product description until the code finds the product, then I guess offsets to the column that holds the values that corresponds to that product and assigns that value to the appropriate variable.
So if Product name is located in cell C3
The code will offset 1 to the right get
the value of "Dozens per case" in cell D3
and assign it to txtDz and offset it again by 1 and assigns the value in E3 to txtCs and so on.
Related
I'm trying to make a simple user form in Excel to enter the date that a staff member did a certain task. I have a spreadsheet that lists all of my staff down column B and I have a list of all the tasks along row 5.
I've created a form with a couple of combo boxes so I can select the staff member and also select the task. There is also a text box which I will type a date into.
What I need is when I click the "submit" button it will enter the date contained in the text box into the cell defined by the 2 selections in the combo boxes. If either of the combo boxes have nothing selected then no data should be entered into the spreadsheet when the button is clicked.
Dim nameRow As Integer
Dim job1col As Integer
nameRow = cbxStaffName.ListIndex + 6
job1col = cbxJob1.ListIndex + 3
ThisWorkbook.Sheets("Job Rotation").Cells(nameRow, job1col).Value = tbxDate.Value
The combo box containing the list of staff names is called cbxStaffName. The combobox containg the jobs is cbxJob1. The textbox containing the date I want to enter is named tbxDate.
The combo boxes have been populated with data that exists on the spreadsheet, simply names down the left and jobs along the top. If for example Jim did the job welding I want to select Jim from one combo box, select welding from the other box and when I click the button the date will go into that cell on the spreadsheet.
You would just need an If statement to check if both checkboxes have a value:
If cbxStaffName.Value <> vbNullString And cbxJob1.Value <> vbNullString Then
'write into cell
Else
MsgBox "Please select staff and job first"
End If
Alternatively you can check the ListIndex which should be -1 if nothing was selected.
Note that I recommend to always use Long instead of Integer especially when dealing with row counts. Excel has more rows than Integer can handle.
SITUATION
I have a sequence of date serial numbers, for example:
42519, 42526, 42533, 42540, 42547, 42554
in cells
B2, C2, D2, E2, F2, G2
respectively.
Eventually there will be 52 date serial numbers, each one representing a Weekly Invoice Date.
You will notice that each one has been incremented by 7 in a previous macro, which presented no problems.
OBJECTIVE
I need to convert these date serial numbers into a format "dd-mmm-yy", using VBA mentioned in Method 2 below (as opposed to copying formulas manually).
So let's say the first date number is 42519 in cell B2.
Method 1.
This method which converts Date Serial Number 42519 to "dd-mmm-yy" format presents no problem to me but is long winded and involves formula copying manually:
Using expression:
Range("B3")="=TEXT(B2,""dd-mmm-yy"")" ' returns 29-May-16 in cell B3
I can even use the expression:
Range("C3")="=TEXT(B2+7,""dd-mmm-yy"")" 'returns 05-Jun-16 in cell C3
Method 2.
This has me stumped and I'm coming up against a brick wall. It was my idea to do something like the following and if I can get this to work I can go ahead and use a loop to generate 52 dates in a row of 52 cells (C3, D3, etc in the format "dd-mmm-yy", each one incremented by 7 days over the period of a year):
Sub sbNumToText_01()
Dim intAdd7 As Integer
intAdd7 = 0
Dim lngSerialDate As Long
lngSerialDate = Range("C2").Value
MsgBox lngSerialDate + intAdd7 'returns 42526, as expected
Range("C3") = "=TEXT(lngSerialDate + intAdd7,""dd-mmm-yy"")"
End Sub
Instead of cell C3 displaying "05-Jun-16", cell C3 displays #Name? and the mini drop down error menu on the left of cell C3 says "The formula contains unrecognised text".
Can any of you out there please explain how to use the TEXT function incorporating variables? Or any other solution along these lines.
Your problem is with the line
Range("C3") = "=TEXT(lngSerialDate + intAdd7,""dd-mmm-yy"")"
If you look at the formula in cell C3 after running the macro, you'll see it contains
=TEXT(lngSerialDate + intAdd7,"dd-mmm-yy")
But lngSerialDate and intAdd7 are VBA variables, not Excel names, so they are meaningless in a worksheet formula and that's why you see the error. What you want to do is convert the sum of those variables to a number before placing it in the worksheet function:
Range("C3") = "=TEXT(" & lngSerialDate + intAdd7 & ",""dd-mmm-yy"")"
I've got an Excel sheet with my variables listed in column E and their values listed in column G
I would like to test if E contains the word "text" (my variable). If so then I want to replace the corresponding cell in column G with "This is my successful if statement text".
If not -- I want the cell to either be left alone (impossible in excel) or keep the value it originally had (I think the issue is its populated with text not numbers).
So far ive tried
=if(e2="text", "Replace with this", G2)
as well as
=if(e2="text", "replace with this", "")
The top returns a number while the bottom returns an empty cell which deletes the contents I had there.
Any suggestions? I think this can be done with VB but that's out of my league.
The proper way to solve this is as so.
In column H (or any that doesn't contain any information) place the formula
=IF(E2 = "text", "This is the true part", G2) and drag down.
This will test E2 for the word "text" and then replace with "this is true.." If the conditions are not met, the original text from G2 is pulled into the new column.
Once this is complete, the desired results should have taken effect. You can then copy the row and use "Paste Special" and select "Values" from the pop up menu to paste in your new data. Selecting Values allows the user to paste the actual field data, not the formula that generated it!
Try this.
Sub g()
Dim ws As Worksheet
Dim lastRow As Long
Set ws = ThisWorkbook.Worksheets("Sheet1") 'change sheet name as applicable
lastRow = ws.Cells(ws.Rows.Count, "E").End(xlUp).Row
For i = 1 To lastRow
With ws
If .Cells(i, 5) = "text" Then
.Cells(i, 7) = "The text you want"
End If
End With
Next
End Sub
It seems like you are trying to get four values from column E that you want to parse (cut up) and place in Column G.
By creating four parses { =mid(e2,16,10), =mid(e3, 9, 15), =mid(e4,5,3), =mid(e5,10,22) } in cells G2, G3, G4, and G5, respectively, you can select the block of four G cells (G2:G5), select the block at the bottom right, and drag it down throughout the file.
Optionally, you can use modulo math and case statements to loop through the file and perform the required function at each point:
myCount = 0
myLoop = 0
endMyLoop = false
range("G2").activate
do
myLoop = myCount mod 4
select case myLoop
case 0
code for description_tag
case 1
code for title_tag
case 2
code for headline
case 3
code for text
end select
if activecell.value = "" then endMyLoop = true
loop until (endMyLoop = true)
You stated that every fourth row the value in E is text. So, it should just be a matter of copying the formula every fourth row or performing your function every fourth iteration (modulo returns the remainder) in the G column.
One other option would be to nest your if loops (=if(e2="text","Its text",if(e2="title_tag","Its a title",if(e2="headline","Its headline","Its description")))) to account for the four different options. Of course you would fill the text with the function that you actually want to perform.
I have two sheets in excel, one is an order form the other is a production sheet based off the order form.
I am using VLOOKUP to query the order form for the quantity of a given item ordered.
However, sometimes this quantity is highlighted on the order form, indicating that the item in question actually gets produced 2 extra amounts (for free samples).
So for example, in the production form I have:
ITEM|QUANTITY TO PRODUCE
In the order form I have:
ITEM|QUANTITY TO ORDER
I use VLOOKUP to get the match, and this works, but if a cell in QUANTITY TO ORDER is highlighted yellow, then I need the VLOOKUP value to be added by two.
How can I do this? Is there a way to do this automatically, without a macro? My client doesn't want to be manually activating things, they just expect the sheet to work.
Thank you.
VLOOKUP can't do that. What you need to do, is treat a cell's background color as data... and a cell's background color isn't data.
But... this link explains how to do that and what the implications are.
Create a workbook-scoped name (Ctrl+F3) called BackColor referring to =GET.CELL(63,OFFSET(INDIRECT("RC",FALSE),0,-1)), and then add a column immediately to the right of the column where the user highlights cells, and make that column have a formula such as =BackColor<>0 so that it contains TRUE for any highlighted cell in the column immediately to its left.
Hard-coding the extra 2 units into your formula isn't going to be maintenance-friendly, so enter that 2 in a cell somewhere and define a name called ExtraUnits for it.
Then modify your formula to
=[the original VLOOKUP]+IF([lookup the BackColor Boolean], ExtraUnits, 0)
This will add ExtraUnits to the looked up units, for all highlighted cells.
The only drawback is that, as I said above, a cell's background color isn't data as far as Excel is concerned, so your user must trigger a recalculation - just changing cells' background color will not do that, but pressing F9 will.
The below code was found at http://www.mrexcel.com/forum/excel-questions/215415-formula-check-if-cell-highlighted.html
Function CellColorIndex(InRange As Range, Optional _
OfText As Boolean = False) As Integer
'
' This function returns the ColorIndex value of a the Interior
' (background) of a cell, or, if OfText is true, of the Font in the cell.
'
Application.Volatile True
If OfText = True Then
CellColorIndex = InRange(1,1).Font.ColorIndex
Else
CellColorIndex = InRange(1,1).Interior.ColorIndex
End If
End Function
To use the function:
=IF(CELLCORINDEX(A1,FALSE)>0,1,0)
This lets you check the color of the cell , or the text. But you will need to use the Index-match code found here http://www.mrexcel.com/forum/excel-questions/447723-vlookup-returns-cell-address.html in order to match it up.
Also, like the above answer states, highlighting a cell doesn't count as a data change, so even though you can get this info without a macro, if someone updates the cell's highlight status, it will not update the cells using this formula unless automatically.
Sounds like you may need to rethink the Highlighting being the trigger of the +2 samples. I'm with the above answer that recommends adding a column maybe True/False or Yes/No that is checked to see if they get samples.
What I did is this:
I created a user defined function:
Function getRGB3(rcell As Range, Optional opt As Integer) As Long
Dim C As Long
Dim R As Long
Dim G As Long
Dim B As Long
C = rcell.Interior.Color
R = C Mod 256
G = C \ 256 Mod 256
B = C \ 65536 Mod 256
If opt = 1 Then
getRGB3 = R
ElseIf opt = 2 Then
getRGB3 = G
ElseIf opt = 3 Then
If B <> 0 Then
B = -2
End If
getRGB3 = B + 2
Else
getRGB3 = C
End If
End Function
This made it so all the highlighted cells (in yellow) got a value of 2 when referred to, so on the order form it goes like ITEM|QUANTITY TO ORDER|CUSTOM FUNCTION VALUE| and the third column (custom function) is 2 for each corresponding yellow cell next to it, if not, it is just zero.
Then I do a second VLOOKUP to add the CUSTOM FUNCTION VALUE to the original, and then I have added two. :)
I have updated the document Google Drive
The problem, with a clearer explanation, is; On my C-D-E-F tab I have , in E12:E14, The VIP numbers. Next cell, F12:F14, labeled "Bulk" are empty. In these cells I want the value from the Data tab, cells T58 in F12, T61 in F13, and T64 in F14.
The bad news is this column will change every day with updated values, the good news is that the order will stay the same. So the lowest VIP tag 16001669, will be the first VIP total, middle is next, and last is last.
Then, I need the number of "Caged Tote's", under the number copied to the C-D-E-F tab, copied to the next cell.
Right now I have:
E11 F11 G11
VIP Bulk Totes
16001669
16001670
16001671
The output I am looking for is:
E11 F11 G11
VIP Bulk Totes
16001669 4 1
16001670 1 1
16001671 4 1
Can you not just use a VLookup formula?
=vlookup(value_looking for, range_table_to_look_in, column_to_return, false)
Failing that, if you're trying to achive in code (and can't just use application worksheetfunction vlookup() ) use the find method of range object.
Let rgSearch = the cell with the value you're looking for= 1234, 4567 etc, rgSearchList = the first column of that table you've listed, intCol = the column you want to return, i.e. three
Function GetThingy(rgSearch as range, rgSearchList as range, intCol as integer) as range
set rgMatch = rgSearchList.find(rgSearch.value, rgSearchList.cells(1,1), xlValues, xlWhole)
if not rgMatch is nothing then 'i.e. "if found"
GetThingy = rgMatch.offset(0, intCol - 1) ' have to remove one because this is an offset from rgMatch, not an absolute from left edge
else
GetThingy = nothing
end if
end function