Adjusting a Vlookup according to a combo box value - excel

I have a system which inputs a code to a cell on a spreadsheet. It does this by using a Vlookup to determine which date it is. If you look at the code below the nput is what does this Vlookup.
What I want it to do is move down a cell per amount the amount that will be in a combo box value called DayAmount. What would I need to enter for it to look at the next cell?
For example if the 5th of January is in A24 I want it to also enter the same code in the 6th and 7th of January which the Vlookup knows is A25 and A26.
Private Sub Submitplan_Click()
' This searches for the selected engineer
Dim EngineerFound As Range
Dim Front As Worksheet
Dim wb As Workbook
Dim area As Worksheet
Set wb = ThisWorkbook
Dim tabchange As String
Set Front = wb.Worksheets("Front")
x = Front.Cells(Front.Rows.Count, "F").End(xlUp).Row
With Front.Range("F8:F" & x)
Set EngineerFound = .Find(Engbox.Value, LookIn:=xlValues)
End With
EngRow = EngineerFound.Row
'This is the section which enters the data into the right date
tabchange = ("Area") & Front.Range("B8")
Set area = wb.Worksheets(tabchange)
y = WorksheetFunction.VLookup(CLng(CDate(Datebox.Value)), area.Range("A:B"), 2, 0)
nPut = WorksheetFunction.VLookup(Key, area.Range("A:B"), 2, 0) &
Hoursbox.Value
z = area.Range("C:C").Find(Engbox.Value).Row
If area.Cells(z, y).Value = " B/H" Then
area.Cells(z, y).Value = nPut & " " & "B/H"
ElseIf area.Cells(z, y).Value = " WK" Then
area.Cells(z, y).Value = nPut & " " & "WK"
Else: area.Cells(z, y).Value = nPut
End If
' If DayAmount <> "" Then
'End If
Call Update
Unload Me
End Sub

If I'm reading this correctly, you have a value in a combobox (will say DayAmount) which will be assigned until a that value is met.
Dim i as Long, j as Long, k as Long
i = ActiveCell.Row
j = DayAmount
k = 1
If j > 1 Then
Do until k = j-1
Cells(k+1,1).Value = Cells(i,1)>Value
k = i + k
Loop
End If
Or you could use a filldown, or .value match, and when you enter the line to the destination cell, you use:
Dim i as Long, j as Long
i = ActiveCell.Row
j = DayAmount
Range(Cells(i,1),Cells(i+j,1)).Value = "" 'input here
Note the arbitrary activecell and column 1 usage as i'm unsure exactly where this would be for you.
Regarding, specifically, the use of nPut, you can use offset to help, such as:
Range(nPut, nPut.Offset(DayAmount,0)) = WorksheetFunction.VLookup(Key, area.Range("A:B"), 2, 0) & Hoursbox.Value
Note that I haven't tested the latter and it's off the top of my head.

Related

VBA counter ignore duplicates

I am struggling with changing counter in my macro. I just started my VBA adventure and spend whole day checking forums and tried many codes, but nothing is working.
I have a code which checks lots of conditions in table. In column F I have listed unique values. In column AE I have the same values, but some of them are duplicated, like in 2 or 3 lines. Now my code checks if value from column F exists in column AE and then checks other conditions like if there is "OB" in column AH and some more conditions. Then it counts how many values it found, but it counts duplicates as well. I need to change it to count only unique values from column AH. So lets say if value X is duplicated in AE2 and AE4 and both of them have "OB" in column AH, then counter shows only 1. Can somebody please explain me how to do it?
So if you look at the example, I have a list of unique values in column F. Column AE contain the same values, but in duplicated lines. 1st part of macro, for example, checks if value in column AE has "OB" in column AH and shows counter in J2. But now it shows 7, because it found 7 lines with values with "OB" in AH, but I need it to show 3, because the values are duplicated. Later macro checks if value has "OB" in column AH and if is different than 0 in column AM. Then it shows 2nd counter in K2. Right now it shows 3, because it found 3 lines with two conditions, but I need it to show 1, because it is the same value.
My code:
Dim lr1 As Long
lr1 = Cells(Rows.count, "F").End(xlUp).Row
Dim lr2 As Long
lr2 = Cells(Rows.count, "AE").End(xlUp).Row
Dim count As Long
Dim counter As Long
Dim x As Long
Dim y As Long
'••••••••••••••••• CHECK IF MATERIAL IS USED IN ACTIVE BOM •••••••••••••••••
'Loop in both ranges
For x = 3 To lr1
For y = 3 To lr2
If range("F" & x) = range("AE" & y) Then
'If material is set to OB
If UCase(range("AH" & y)) = "OB" Then
'And is used in BoM
If range("AO" & y) <> "" Then
'And BoM is not OB
If UCase(range("AP" & y)) <> "OB" Then
'Add to counter
count = count + 1
' range("F" & x).Interior.ColorIndex = 3
End If
End If
End If
End If
Next y
Next x
'Display results in J2
If count > 0 Then
range("J2") = count & " found"
range("J2").Font.Color = vbRed
Else
range("J2") = "None"
range("J2").Font.ColorIndex = 10
End If
'••••••••••••••••• CHECK IF MATERIAL IS ON STOCK •••••••••••••••••
'Loop in both ranges
For x = 3 To lr1
For y = 3 To lr2
If range("F" & x) = range("AE" & y) Then
'If material is set to OB
If UCase(range("AH" & y)) = "OB" Then
'And is on stock
If range("AM" & y) <> "0" Then
'Add to counter
counter = counter + 1
End If
End If
End If
Next y
Next x
'Display results in K2
If counter > 0 Then
range("K2") = counter & " on stock"
range("K2").Font.Color = vbRed
Else
range("K2") = "None"
range("K2").Font.ColorIndex = 10
End If
Here your are. The code first checks if the value in column AE is existing in the list of unique values and if column AH = "OB".
If this unique value has not been added to the unique collection, it will be added and the Unique count is increased, else it is ignored.
Function Condition1()
Dim ws As Worksheet: Set ws = Worksheets("Sheet1")
Dim uniqueRange As Range: Set uniqueRange = ws.Range("F2:F9")
Dim checkList As Collection
Dim i As Integer
Dim UniqueCounter As Integer
Set checkList = New Collection
For i = 2 To 15
Dim findStr As String
findStr = ws.Cells(i, "AE")
If Not uniqueRange.Find(findStr, LookIn:=xlValues) Is Nothing And ws.Cells(i, "AH") = "OB" Then 'Check if Value exists in master, if not ignore
Dim keyExists As Variant
On Error Resume Next
keyExists = Empty
keyExists = checkList(findStr)
On Error GoTo 0
If IsEmpty(keyExists) Then
UniqueCounter = UniqueCounter + 1
checkList.Add findStr, findStr
End If
End If
Next
Condition1 = UniqueCounter
End Function

My Paste function isn't working and I don't know why

I am trying to copy the entry forms I select from the master list to a different worksheet. The idea is that it goes down a condensed list of names without details. Looks at what I have selected. Copies the selected entry form and pastes it in a new place to generate a list that only contains the entry forms I need. I can't tell if the loop is working properly because the paste function at the end isn't working.
Sub BidList()
'Sets unique terms used throught this code
Dim wQuick As Worksheet, wMaster As Worksheet
Dim BlankFound As Boolean
Dim x As Long, n As Long, y As Long
Dim firstrow As Long, pasterow As Long, lastrow As Long, PasteCell As String, MyRange As String
'Turns off Screen updating to save memory
Application.ScreenUpdating = False
'Store an initial value for "x" effectively starting the macro at C4 after the +1 in the next step
x = 3
n = 0
y = 0
Set wQuick = ThisWorkbook.Sheets("Quick Reference")
Set wMaster = ThisWorkbook.Sheets("MASTER")
BlankFound = False
'Loop until x equals 600
Do While BlankFound = False
x = x + 1
n = n + 1
If Trim(wQuick.Cells(x, "B").Value) = "" Then Exit Do
'If there is a 1 in the Boolean column then ...
If Trim(wQuick.Cells(x, "C").Value) = "1" Then
'Move the y value so that the template is pasted in the appropriate place
y = y + 1
'Copy the appropriate form from wMaster
firstrow = (n * 9) + 18
lastrow = (n * 9) + 27
Let MyRange = "A" & firstrow & ":" & "K" & lastrow
wMaster.Range(MyRange).Copy
'Select the next place for the form to be pasted on wQuick
pasterow = (y * 9) - 5
Let PasteCell = "F" & "," & pasterow
wQuick.Cells(PasteCell).Paste
End If
Loop
Application.ScreenUpdating = True
End Sub
Please avoid copy-paste in Excel, better use a for-loop like the following:
# keep track of MyRange.Row and MyRange.Column
For Each cell in wMaster.Range(MyRange):
wQuick.Cells(<Starting point>).Offset(<take the cell coordinates, based on MyRange.Row and/or MyRange.Column>).Value = cell.Value
Next

Excel VBA to loop and find specific range and concatenate 2 cell values and delete empty cell

I am trying to identify a specific range in column-A and concatenate two cells within the specific range and delete the empty cell. I have been successful in putting a code together and it does the job very well. But, I don't know how to loop it to identify next range. Any help would be appreciated.
As per below image and code, First, I am finding and selecting a range between two (MCS) in column-A with a condition that, if the rows are more than 8 between two MCS. Then I am concatenating first 2 cells immediately after MCS and delete the empty row.
The below code works well for first range but I am unable to loop to identify next range from row 22 to 32 and perform concatenations.
I dont know how to loop in column-A and select ranges and concatenate. Any help would be much appreciated. Thanks
Sub MergeStem()
Dim findMCS1 As Long
Dim findMCS2 As Long
Dim myCount As Integer
Dim myStems As Long
Dim mySelect As Range
Dim c As Range
findMCS1 = Range("A:A").Find("MCS", Range("A1")).Row
findMCS2 = Range("A:A").Find("MCS", Range("A" & findMCS1)).Row
myCount = Range("A" & findMCS1 + 1 & ":A" & findMCS2 - 1).Cells.Count
Range("B1").Value = myCount
MsgBox "Number of rows =" & myCount
Set mySelect = Selection
If myCount > 8 Then
myStems = Range("A" & findMCS1 + 2 & ":A" & findMCS2 - 9).Select
Set mySelect = Selection
For Each c In mySelect.Cells
If firstcell = "" Then firstcell = c.Address(bRow, bCol)
sArgs = sArgs + c.Text + " "
c.Value = ""
Next
Range(firstcell).Value = sArgs
End If
Columns("A").SpecialCells(xlCellTypeBlanks).EntireRow.Delete
Application.ScreenUpdating = True
End Sub
Can you try this? Ordinarily, Find would be the way to go but because you are deleting rows it's hard to keep track of which cells you've found.
Sub x()
Dim r As Long, n1 As Long, n2 As Long
With Range("A1", Range("A" & Rows.Count).End(xlUp))
For r = .Count To 1 Step -1
If .Cells(r).Value = "MCS" Then
If n1 = 0 Then
n1 = .Cells(r).Row
Else
n2 = .Cells(r).Row
End If
If n1 > 0 And n2 > 0 Then
If n1 - n2 > 9 Then
.Cells(r + 1).Value = .Cells(r + 1).Value & .Cells(r + 2).Value
'.Cells(r + 2).EntireRow.Delete
'Call procedure to delete row
End If
n1 = n2
n2 = 0
End If
End If
Next r
End With
End Sub

VBA: How to describe row with specific string

Hi I'm trying to create a loop that identifies a specific string in column "B", and SUMs up the values in column "D" of the same row. So far I've been able to identify the "cash" but now I don't know how to describe column "D" of the SAME row and sum it up. Please help!
Below is what I've got so far for this loop.
Dim CD As Long
Dim text As String
Dim Z As Long
CD = 0
For Z = 1 To Range("B" & Rows.Count).End(xlUp).Row
text = Range("B" & Z).Value
If Left(text, 4) = "Cash" Then
Sum....
You could certainly do something like:
For Z = 1 To Range("B" & Rows.Count).End(xlUp).Row
text = cells(Z,2).Value
If Left(text, 4) = "Cash" Then
Sum.... Zum = Zum + cells(Z,4).value
However, the computation could be done with a simple worksheet formula
=SUMIF(B:B,"cash*",D:D)
Following the code you have provided, this would be what you are looking for:
Sub calculateSum()
Dim CD As Long
Dim text As String
Dim Z As Long
'Create a variable to store the sum and set its value to 0.
Dim sum As Double
sum = 0
CD = 0
For Z = 1 To Range("B" & Rows.Count).End(xlUp).Row
text = Range("B" & Z).Value
If Left(text, 4) = "Cash" Then
'Increase the sum by the value stored in column D on the same row.
sum = sum + Range("D" & Z).Value
End If
Next Z
'Display the final result (sum).
MsgBox "Sum: " & sum
End Sub

Need code to be able to Check through current column and another column, and Match to use the same order number

if you see below, I have 3 columns, all i basically want is to check column A and Column B, if the carrier (Column A) and Date (Column B) are equal then it will have the same Order Number.
For example: In this case, A3 = A6 and B3 = B6 , so it should have the same order number as one above (160) not 163. I hope this makes it clear.
Thanks for the help. I appreciate it :)
This was quite interesting, so I went ahead and wrote some code. Copy this into a new module and change the sheetname etc. to fit to your workbook. You may also need to redefine fr (firstrow, currently set to 2). The code also currently marks all the changed order-numbers red with the line .Range("C" & r).Font.ColorIndex = 3. Delete / comment it, if you don't want that.
Sub matching()
Dim wb As Workbook
Dim tws As Worksheet
Dim keys() As String
Dim tmpKey As String
Dim pos As Integer
Dim fr, lr As Integer 'first row, last row of data
Set wb = ThisWorkbook
Set tws = wb.Worksheets("Vigmo")
fr = 2
lr = tws.Range("A1000000").End(xlUp).Row
ReDim keys(1 To lr - 1)
With tws
keys(1) = .Range("A" & fr).Value & "_" & .Range("B" & fr).Value
End With
For r = fr + 1 To lr
With tws
tmpKey = .Range("A" & r).Value & "_" & .Range("B" & r).Value
If UBound(Filter(keys, tmpKey)) >= 0 And tmpKey <> "_" Then
'found in array -> replace orderNumber
'On Error resume next
pos = Application.Match(tmpKey, keys, 0)
'On Error goto 0
.Range("C" & r).Value = .Range("C" & pos + 1).Value
.Range("C" & r).Font.ColorIndex = 3
Else
'not found -> next
End If
keys(r - 1) = tmpKey
End With
Next r
End Sub
Let me know if you have any questions as to how this code works!
Below is some code that I came up with that does what your looking for. I dont know how you are generating your order numbers but I assumed they are already present. Hope this helps you :)
Sub OrderNumber()
Dim SearchTerm As String
Dim DateTerm As Date
Dim NumberOfEntries As Long
Dim wks As Excel.Worksheet
Set wks = Worksheets("Sheet1") '<==== Sets the workbook. change it to what yours is called
NumberOfEntries = Application.WorksheetFunction.CountA(wks.Range("A:A")) '<=== Find the number of entries
For x = 2 To NumberOfEntries '<==== Goes through all the entries
SearchTerm = wks.Cells(x, 1) '<===== The Search term (Carrier)
DateTerm = CDate(wks.Cells(x, 2)) '<==== The search Date
For y = x To NumberOfEntries '<===== goes through everything below the search term to speed things up
If wks.Cells(y, 1) = SearchTerm And CDate(wks.Cells(y, 2)) = DateTerm Then '<=== If the name and the date match then
wks.Cells(y, 3) = wks.Cells(x, 3) '<==== Copy the order number
End If
Next y
Next x
End Sub
Just put this in a module or wherever you want but I made it in a module.
G

Resources