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
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
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
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 a table as shown below.
In column C I would like to Sum values from column A if they have the same index (column B). I would like to put sum result for all the rows if they have same index (as shown in column D).
Unfortunately the range of values with same index is variable and my macro can sum values just with 2 indexes. Can anyone help with it, please? Thanks!
Sub example()
Dim ws As Worksheet
Dim LastRow As Long
Dim n, i As Integer
Set ws = ActiveWorkbook.Sheets("Sheet2")
ws.Select
LastRow = Sheets("Sheet2").Range("A" & Sheets("Sheet2").Rows.Count).End(xlUp).Row
Range("C3:C" & LastRow).Select
Selection.ClearContents
For i = 3 To LastRow
If Range("B" & i + 1) - Range("B" & i) = 0 Then
Range("C" & i) = Range("A" & i + 1) + Range("A" & i)
Else
Range("C" & i) = Range("C" & i - 1)
End If
Next i
End Sub
Here's one way:
Sub example()
Dim ws As Worksheet
Dim LastRow As Long
Set ws = ActiveWorkbook.Sheets("Sheet2")
LastRow = ws.Range("A" & ws.Rows.Count).End(xlUp).Row
With ws.Range("C3:C" & LastRow)
.ClearContents
.Value = ws.Evaluate("INDEX(SUMIF(B3:B" & LastRow & ",B3:B" & LastRow & ",A3:A" & LastRow & "),)")
End With
End Sub
I have a tricky situation. I have a column A with only headers and column B contains text. Now I would like to get the text from column B to start in column A. If there's text in column A, B will always be empty.
A B
Title 1
Text 1
Text 2
Title 2
Text 1
Text 2
How could I get it so the text in column B is put in column A. Range is set until a complete empty row is found. (A1 to S1 no values in the cells = empty row)
I was thinking about merging cells, but that's perhaps not neat.
Like this? This uses merging and also takes into account where A and B are both filled up.
Sub Sample()
Dim ws As Worksheet
Dim LastRow As Long
Set ws = Sheets("Sheet5")
With ws
LastRow = .Range("B" & .Rows.Count).End(xlUp).Row
For i = 1 To LastRow
If Application.WorksheetFunction.CountA(.Range("A" & i & ":" & "B" & i)) = 1 And _
Len(Trim(.Range("A" & i).Value)) = 0 Then
With .Range("A" & i & ":" & "B" & i)
.Merge
End With
End If
Next i
End With
End Sub
FOLLOW UP
If you don't want merging and A will always remain empty when there is a value in B then we can move the value from Col B into Col A like this
Sub Sample()
Dim ws As Worksheet
Dim LastRow As Long
Set ws = Sheets("Sheet5")
With ws
LastRow = .Range("B" & .Rows.Count).End(xlUp).Row
For i = 1 To LastRow
If Len(Trim(.Range("A" & i).Value)) = 0 Then
.Range("A" & i).Value = .Range("B" & i).Value
.Range("B" & i).ClearContents
End If
Next i
End With
End Sub