This is what I am currently trying to achieve in this project. The scenario, I have identified an item with zero inventory at a location color yellow, I need to search for the network of locations (data is pulled from the main data source) for the same item, category but I don’t want to see the yellow location because I already know yellow has zero inventory
enter image description here
#Raj i m not sure what to you mean but i try to create a code.
Sheet 1
Structure include the search engine (yellow area) and the results)
You just import data on the yellow fields
where the code run erase the results to bring the new ones.
Sheet 2
Structure (include the Data Base)
Try:
Option Explicit
Sub Test()
Dim LastRow2 As Long
Dim i As Long
Dim Mat_Cat_Loc As String
Dim LastRow1 As Long
With Sheet1
If .Range("B2").Value = "" Or .Range("B3").Value = "" Or .Range("B4").Value = "" Then '<= if some search part missing end sub
Exit Sub
End If
Mat_Cat_Loc = .Range("B2").Value & .Range("B3").Value & .Range("B4").Value ' <= Create Mat_Cat_Loc for the search product
LastRow1 = .Range("A" & Rows.Count).End(xlUp).Row
.Range("A8:D" & LastRow1).Clear '<= Clear range from the previous search
End With
With Sheet2
LastRow2 = .Range("A" & Rows.Count).End(xlUp).Row
For i = 3 To LastRow2 ' <= Loop in Sheet2 and if Mat_Cat_Loc (From the search) does not match Mat_Cat_Loc (From the data does not copy the line to Sheet1 Rsult Table)
If (.Range("D" & i).Value <> Mat_Cat_Loc) And (.Range("C" & i).Value) <> Sheet1.Range("B4").Value Then
With Sheet1
LastRow1 = .Range("A" & Rows.Count).End(xlUp).Row
.Range("A" & LastRow1 + 1).Value = Sheet2.Range("A" & i).Value
.Range("B" & LastRow1 + 1).Value = Sheet2.Range("B" & i).Value
.Range("C" & LastRow1 + 1).Value = Sheet2.Range("C" & i).Value
.Range("D" & LastRow1 + 1).Value = Sheet2.Range("D" & i).Value
End With
End If
Next i
End With
End Sub
Related
I need help to create one VBA for below issue -
column C should be updated based on column A and B values.
in below example if column A=Unit1 and column B=IND then Person 1 and so on.. it should loop till end.
Thank you in adv. please refer picture for sample data
enter image description here
Here you go. Just change the sheet name since I don't know what you're working with.
Sub AddPerson()
Dim i As Long, ws As Worksheet, lRow As Long
Set ws = Sheets("Sheet1") 'Change to your sheet name
lRow = ws.Range("A" & Rows.Count).End(xlUp).Row
With ws
.Range("C2:C" & lRow + 1).ClearContents
For i = 2 To lRow
If .Range("B" & i) = "IND" Then
.Range("C" & i) = "Person1"
ElseIf .Range("A" & i) = "Unit5" And .Range("B" & i) = "OTR" Then
.Range("C" & i) = "Person3"
Else
.Range("C" & i) = "Person2"
End If
Next i
End With
End Sub
I have an array of data, a screenshot of it will be linked at the bottom of this text. Row and column references are to the screenshot.
I am trying to write a macro that will output all the dates that occur within the dynamic range (Column H). And then in column I I want the column header # row i.e I4.
But if there is more than 1 count at the date, I would like the second school to output into column J. As it would for the date 26/03/18, looking like this:
h5 = 26/03/18 , i5(Event1) = Task 2 # 1, j5(Event2) = task 2 # 4
I have tried many ways today and would like some assistance.
Screenshot: https://ibb.co/cmiGSc
My Code thus far(For the more complex sheet):
Sub Events()
'How many schools there are
Dim sh As Worksheet
' This needs to change for each sheets
Set sh = ThisWorkbook.Sheets("Easter 18")
Dim k As Long
k = sh.Range("A3").End(xlDown).Row 'Counts up from bottow - Number of schools attained
Ro = Range("M52").value = k - 2 'Elimiates the two top rows as headers
'Now I need to search the Range of dates
Dim TaskDates As Range
Dim StartCell As Range 'First part of Array
Dim EndCell As Range 'End of Array
Set EndCell = Range("J" & 2 + k) 'maybe 2 or 3
Set StartCell = Range("G3")
Set TaskDates = Range(StartCell, EndCell) 'Dynamic Range
'Within the range of data print out the most left row header (school name) - and task with # in the middle - ascending
' If Column has date (true) create a table with Date (col 1), Event (col 2), Event 2 (Col3) etc etc
Dim dict As Object
Set dict = CreateObject("scripting.dictionary")
Dim varray As Variant, element As Variant
varray = TaskDates.value
'Generate unique list and count
For Each element In varray
If dict.exists(element) Then
dict.item(element) = dict.item(element) + 1
Else
dict.Add element, 1
End If
Next
'Paste report somewhere -
'First line ouptuts the dates occured
sh.Range("M55").Resize(dict.Count).value = 'Was working now saying syntax error for this line.
WorksheetFunction.Transpose (dict.keys)
' The count works if cell format is correct
CDates = sh.Range("N55").Resize(dict.Count, 1).value = _
WorksheetFunction.Transpose(dict.items)
End Sub
Please feel free to redesign it if you see fit.
you can go this way
Option Explicit
Sub Tasks()
Dim cell As Range, f As Range
With Worksheets("schools") 'change "schools" to your actual sheet name
For Each cell In .Range("C4:F" & .Cells(.Rows.Count, "B").End(xlUp).Row) 'reference its column C:F from row 4 down to column B last not empty cell
If IsDate(cell.value) Then 'if current cell value is a valid date
Set f = .Range("H3", .Cells(.Rows.Count, "H").End(xlUp)).Find(what:=cell.value, lookat:=xlWhole, LookIn:=xlValues) 'try finding the date in column H
If f Is Nothing Then Set f = .Cells(.Rows.Count, "H").End(xlUp).Offset(1) 'if date not already in column H then get its first empty cell after last not empty one
f.value = cell.value 'write the date (this is sometimes not necessary, but not to "ruin" the code)
.Cells(f.Row, .Columns.Count).End(xlToLeft).Offset(, 1).value = .Cells(3, cell.Column).value & " #" & .Cells(cell.Row, 2).value ' write the record in the first not empty cell in the "date" row
End If
Next
End With
End Sub
Took a shot at this. Just a couple nested loops testing against the dates, making sure that the date found isn't already listed under the date column. As I stated before, you never said what to do if more than 3 dates are found, so I had to add a fourth event column and assume that that's the max. Anything more than 4 dates won't be recorded anywhere, FYI.
Sub MoveDates()
Dim i As Long, j As Long, sht As Worksheet, lastrow As Long, lastrow2 As Long, refrow As Long
Set sht = ThisWorkbook.Worksheets("Sheet1")
lastrow = sht.Cells(sht.Rows.Count, "B").End(xlUp).Row
lastrow2 = sht.Cells(sht.Rows.Count, "H").End(xlUp).Row + 1
For i = 4 To lastrow
For j = 3 To 6
If Cells(i, j).Value <> "" And Cells(i, j).Value <> "n/a" Then
If Not Application.WorksheetFunction.CountIf(Range("H4:H" & lastrow), Cells(i, j)) > 0 Then
lastrow2 = sht.Cells(sht.Rows.Count, "H").End(xlUp).Row + 1
Range("H" & lastrow2).Value = Cells(i, j).Value
If Range("I" & lastrow2).Value = "" Then
Range("I" & lastrow2).Value = Cells(3, j).Value & " # " & Cells(i, 2).Value
ElseIf Range("J" & lastrow2).Value = "" Then
Range("J" & lastrow2).Value = Cells(3, j).Value & " # " & Cells(i, 2).Value
ElseIf Range("K" & lastrow2).Value = "" Then
Range("K" & lastrow2).Value = Cells(3, j).Value & " # " & Cells(i, 2).Value
ElseIf Range("L" & lastrow2).Value = "" Then
Range("L" & lastrow2).Value = Cells(3, j).Value & " # " & Cells(i, 2).Value
End If
Else
lastrow2 = sht.Cells(sht.Rows.Count, "H").End(xlUp).Row
For k = 4 To lastrow2
If Range("H" & k).Value = Cells(i, j).Value Then
refrow = k
Exit For
End If
Next k
If Range("I" & refrow).Value = "" Then
Range("I" & refrow).Value = Cells(3, j).Value & " # " & Cells(i, 2).Value
ElseIf Range("J" & refrow).Value = "" Then
Range("J" & refrow).Value = Cells(3, j).Value & " # " & Cells(i, 2).Value
ElseIf Range("K" & refrow).Value = "" Then
Range("K" & refrow).Value = Cells(3, j).Value & " # " & Cells(i, 2).Value
ElseIf Range("L" & refrow).Value = "" Then
Range("L" & refrow).Value = Cells(3, j).Value & " # " & Cells(i, 2).Value
End If
End If
End If
Next j
Next i
End Sub
Looking to up my manual mapping solution of merging worksheets, to a search-map header solution. The basics are this
Dest_Worksheet: This has the only headers that are needed post merge (up to 50 columns)
Source_Worksheet1: This has a list of items with some headers that match Dest_Worksheet (up to 100 columns - different than Source_Worksheet2)
Source_Worksheet2: This has a list of items with some headers that match Dest_Worksheet (up to 100 columns - different than Source_Worksheet1)
Total row count unknown at the time of run. Currently built out a manual mapping (see below).
ASKING: Move beyond manual mapping of each worksheet to a solution which reviews the Dest_Worksheet and references those headers, move through remaining or identified list of Source worksheets and copy all rows with only columns that match Dest_Worksheet.
See sample worksheet for working manual mapping code below
'******Manual Mapping of Source_Data1*******
Sub Source_Data1()
Dim sht As Worksheet
Dim colname As String
Dim Lastrow As Integer, rowcount As Integer
colname = 1
For Each sht In ActiveWorkbook.Worksheets
If sht.Name = "Source_Worksheet1" And sht.Range("A3").Value <> "" Then
Sheets("Source_Worksheet1").Select
Lastrow = Range("A9000").End(xlUp).Row
Sheets("Dest_Worksheet").Select
rowcount = Range("A9000").End(xlUp).Row + 1
sht.Select
Sheets("Dest_Worksheet").Range("A" & rowcount & ":A" & rowcount + Lastrow - 3).Value = sht.Range("A3:A" & Lastrow).Value
Sheets("Dest_Worksheet").Range("B" & rowcount & ":B" & rowcount + Lastrow - 3).Value = sht.Range("B3:B" & Lastrow).Value
Sheets("Dest_Worksheet").Range("C" & rowcount & ":C" & rowcount + Lastrow - 3).Value = sht.Range("C3:C" & Lastrow).Value
Sheets("Dest_Worksheet").Range("D" & rowcount & ":D" & rowcount + Lastrow - 3).Value = sht.Range("D3:D" & Lastrow).Value
End If
Next sht
Worksheets("Dest_Worksheet").Select
End Sub
'******Manual Mapping of Source_Data2*******
Sub Source_Data2()
Dim sht As Worksheet
Dim colname As String
Dim Lastrow As Integer, rowcount As Integer
colname = 1
For Each sht In ActiveWorkbook.Worksheets
If sht.Name = "Source_Worksheet2" And sht.Range("A3").Value <> "" Then
Sheets("Source_Worksheet2").Select
Lastrow = Range("A9000").End(xlUp).Row
Sheets("Dest_Worksheet").Select
rowcount = Range("A9000").End(xlUp).Row + 1
sht.Select
Sheets("Dest_Worksheet").Range("A" & rowcount & ":A" & rowcount + Lastrow - 3).Value = sht.Range("A3:A" & Lastrow).Value
Sheets("Dest_Worksheet").Range("E" & rowcount & ":E" & rowcount + Lastrow - 3).Value = sht.Range("B3:B" & Lastrow).Value
Sheets("Dest_Worksheet").Range("F" & rowcount & ":F" & rowcount + Lastrow - 3).Value = sht.Range("C3:C" & Lastrow).Value
Sheets("Dest_Worksheet").Range("C" & rowcount & ":C" & rowcount + Lastrow - 3).Value = sht.Range("E3:E" & Lastrow).Value
End If
Next sht
Worksheets("Dest_Worksheet").Select
End Sub
After lots of trial and error I got Find() working to return the column letter I'm looking for. Here's the code I ended up using and the associated function call:
Sub LookupText()
Dim DestLetter As String
DestLetter = TextLookup("Search Text")
MsgBox DestLetter
End Sub
'***********
Function TextLookup(TheText As String) As String
Set Cell = Worksheets("Destination_Worksheet").Cells.range("A1:DA1").Find(TheText, , xlValues, xlPart, , , False)
If Not Cell Is Nothing Then
ColLetter = Split(Cell.Address, "$")(1)
TextLookup = ColLetter
End If
End Function
I am trying to copy and paste data from an input sheet into an output sheet, and once the data is in the next spreadsheet, it will be filled down the next rows from a starting date to an ending date.
As is, the code gets me the following errors when debugging:
ws1.Range("A" & NextRow) = Method of Range Object_Worsheet failed; same for ws1.Range("B" & NextRow); ws1.Range("C" & NextRow) etc...
ws1.Cells(LastRow, "H") = Application-defined or object-defined error.
Also, I noticed that when I set RawDataEntries it equals 37, that is the last non-empty row, but then when I try to use the For loop and point my mouse on RawDataEntries in For n=1 To RawDataEntries, VBA returns me a value of 105, which seems to come out of nowhere.
I think the logic behind the code is correct. What might have gone wrong?
Sub AddFlight_Click()
Const RNG_END_DT As String = "N2"
Dim NextRow1 As Long, LastRow1 As Long, ws1 As Worksheet
Dim ws2 As Worksheet, RawDataEntries As Long
Set ws1 = Sheets("JetAir Flight Plan")
Set ws2 = Sheets("TUI B Flight Plan")
LastRow1 = ws1.Range("A" & Rows.Count).End(xlUp).Row
NextRow1 = LastRow1 + 1
RawDataEntries = ws2.Range("A" & Rows.Count).End(xlUp).Row
For n = 1 To RawDataEntries
'Data from an input worksheet is copied and pasted into specific cells in an output worksheet.
ws1.Range("A" & NextRow).Value = ws2.Range("A" & n).Value
ws1.Range("B" & NextRow).Value = ws2.Range("B" & n).Text
ws1.Range("D" & NextRow).Value = ws2.Range("D" & n).Text
ws1.Range("E" & NextRow).Value = ws2.Range("E" & n).Text
ws1.Range("F" & NextRow).Value = ws2.Range("F" & n).Text
ws1.Range("G" & NextRow).Value = ws2.Range("G" & n).Text
ws1.Range(RNG_END_DT).Value = ws2.Range("H" & n).Value
'A series of dates is created from a starting date
' to an ending date in column A of ws1.
ws1.Range("A" & NextRow).DataSeries Rowcol:=xlColumns, _
Type:=xlChronological, Date:=xlDay, Step:=7, _
Stop:=ws1.Range(RNG_END_DT).Value, Trend:=False
'The data filled in the last row with the userform data through
' the first part of the macro will be copied and pasted in
' the next row until there is a blank cell in column A.
LastRow1 = ws1.Range("A" & Rows.Count).End(xlUp).Row
ws1.Range(ws1.Range("B" & NextRow), ws1.Cells(LastRow, "H")).FillDown
'We repeat the process for other rows on the sheet data are pulled from
Next n
Thanks a lot.
Change NextRow to NextRow1. Right now you are calling a variable that doesn't exist.
Or you can do the opposite (NextRow1 to NextRow)
Same with LastRow and LastRow1
I have one document (two sheets) that I am trying to compare between. I have cleaned up the columns so both have our unique reference number in column A, the vendor expense in column B and the revenue in column C. I am trying to do an internal audit of sorts without going through every one individually.
One sheet contains data from two years whereas the other contains data from one year. It is not a definitive date so I didn't want to remove any.
Accountants Export
My Data
How would I go about matching the unique identifier in column A and highlighting if there is a difference in the information in column B or C?
here is the code to do it in VBA
I suppose for both Sheet1 and Sheet2 that:
ColumnA is "Pro", ColumnB Gross Rate and ColumnC "Carrier Exp"
All headers are in Row1 and the data starts in row2
Here is the code:
Sub test()
Dim wb As Workbook
Dim ws1 As Worksheet, ws2 As Worksheet
Dim Lastrow As Long, Lastrow2 As Long
Dim i As Integer, j As Integer
Set wb = ThisWorkbook
Set ws1 = wb.Sheets("Sheet1")
Set ws2 = wb.Sheets("Sheet2")
Lastrow = ws1.Range("A" & Rows.Count).End(xlUp).Row
Lastrow2 = ws2.Range("A" & Rows.Count).End(xlUp).Row
For i = 2 To Lastrow
For j = 2 To Lastrow2
If ws1.Range("A" & i).Value = ws2.Range("A" & j).Value Then
If Not ws1.Range("B" & i).Value = ws2.Range("B" & j).Value Then
ws1.Range("B" & i).Interior.Color = vbYellow
ws2.Range("B" & j).Interior.Color = vbYellow
End If
If Not ws1.Range("C" & i).Value = ws2.Range("C" & j).Value Then
ws1.Range("C" & i).Interior.Color = vbYellow
ws2.Range("C" & j).Interior.Color = vbYellow
End If
End If
Next j
Next i
End Sub