Subtraction in the Excel - excel

I would like to create an excel template for warehouse. In this template are integrated warehouse, sellings and invoices.
I already wrote a code that transfers data to sellings every time I create an invoice and click a button. I would like to integrate in my code function that can do corrections in my warehouse i.e. when I fill an invoice I put items ID, description, amount and a price. After I click a button I want the items amount in the warehouse sheet to decreased by the same amount as in invoice.
The biggest issue for me was how to write a code that takes information from invoice sheet (ID and items amount), look for exact matches in the warehouse sheet and in the warehouse sheet column "I"(stock) decrease stock with the same amount.
sheet1 - name sheet "warehouse". ID in the column "C"; stock - column "I"; data starts from row "4"
sheet3 - name sheet "invoice". ID in the merged column "A""B"(column 1); amount that I sell in the merged column "S""T""U" (column 4); data starts from row 15 as you can see in the code below.
My code that transfers data to sellings (sheet2). I need to integrate function into this code without creating another button.
Sub Button4_Click()
Dim x As Long
Dim erow As Long
'Calculate starting rows
x = 15
With Worksheets("Sellings")
erow = .Cells(.Rows.Count, 1).End(xlUp).Offset(1, 0).Row
End With
With Worksheets("Invoice")
Do While .Cells(x, 1) <> ""
'The next line copies values to Sheet2
Worksheets("Sellings").Range("A" & erow & ":Z" & erow).Value = .Range("A" & x & ":Z" & x).Value
'increment row counters
x = x + 1
erow = erow + 1
Loop
End With
End Sub

Related

Is it possible to restructure multi header one drive table using automate in one drive?

I am trying to restructure inside one drive using automate a function, but not sure if multi header table can be restructured. Thank you in advance for your help.
I want to restructure it in this format -
Customer
You can loop through the dates in 1st row and create a nested loop which goes through the customers one by one and get the values according to the actual column and row. You should save the result on a new sheet.
Sub Format_Table()
lastRowOnNewFormatSheet = 2 'last empty row on newSheet
'Loop through the columns with step 3
For i = 2 To Cells(1, Columns.Count).End(xlToLeft).Column Step 3
'Loop through the rows at every column
For j = 3 To Cells(Rows.Count, 1).End(xlUp).Row
Sheets("newFormat").Cells(lastRowOnNewFormatSheet, 1).Value = Sheets("oldFormat").Cells(j, 1) 'customer name
Sheets("newFormat").Cells(lastRowOnNewFormatSheet, 2).Value = Sheets("oldFormat").Cells(1, i) 'date
Sheets("newFormat").Cells(lastRowOnNewFormatSheet, 3).Value = Sheets("oldFormat").Cells(j, i) 'budget
Sheets("newFormat").Cells(lastRowOnNewFormatSheet, 4).Value = Sheets("oldFormat").Cells(j, i + 1) 'actual
lastRowOnNewFormatSheet = lastRowOnNewFormatSheet + 1 'update last empty row on newSheet
Next j
Next i
End Sub

Sum a cell based on unique value and unique date using vba

I have a data dump in Excel that consists of monthly crew member hours worked data. Currently, there are multiple rows of data for the same date.
I want to run a macro that keeps unique names and unique dates but delete rows that have duplicated dates (keep one row for each crew member per date). In the "hours" column, I wish to combine multiple shifts from the same day into one.
Here is my code so far and result.
Sub mcrCombineAndScrubDups()
For Each a In Range("A1", Cells(Rows.Count, "A").End(xlUp))
For r = 1 To Cells(Rows.Count, "A").End(xlUp).Row - a.Row
If a = a.Offset(r, 0) And a.Offset(0, 1) = a.Offset(r, 1) And a.Offset(0, 2) = a.Offset(r, 2) Then
a.Offset(0, 6) = a.Offset(0, 6) + a.Offset(r, 6)
a.Offset(r, 0).EntireRow.Delete
r = r - 1
End If
Next r
Next a
End Sub
With the current code, it combines all the hours worked for the entire month into one field rather than a row for each crew member for each date. I know a pivot table can be run but my boss wants more automatic. Thanks!

Excel formula to find a number within a range of numbers

EDIT, in short:
I have an excel table that looks like this:
tabel
Each cell contains a range of numbers.
I'm looking for a function that can search for a number in the whole table and indicate me the column and row where the range that it belongs to is located.
So, if I search for number 2346, it should function like this:
function (2346) > result (C1, R2)
I have a huge archive of photos (about 300.000 items) in a library.
Photos are stored in boxes, the boxes in shelves.
Each box has a range of inventory numbers of photos.
I want to create a map of the deposit that looks like this:
Shelf 1 - contains boxes 1, 2, 3, etc.
Box 1 - contains photos with inventory numbers between 1257-1321
Box 2 -"- between 2345-2522
Box 3 -"- between 123523-123643
Translated in an excel table, it would look like this:
Column 1 would be Shelf 1, containing the boxes with the images:
cell in column 1 / row 1 (that's box 1) contains the range of numbers: 1257-1321
cell in column 1 / row 2 (box 2), the range: 2345-2522
cell in column 1 / row 3 (box 3), range: 123523-123643
They are not in order, because they entered in more than 100 years in the collection, and they are arranged as they entered and by subject. Not to mention that the deposit has been moved a few times. So, I have a hard time to find one image when I'm looking for it.
But if I have this excel table, this map of the deposit, supposedly I want to enter the inventory number that I'm looking for, let's say "2346", and a formula that search throughout the whole table would indicate me that the item (number I look for, is in a range that is located in column 1, row 2, that means shelf one, box 2 in the deposit).
Actually the concept is very simple, excel is able to do MUCH more difficult tasks, and I'm amazed I can't find a way to do this. I'm a photographer and librarian, so my experience in programming is close to zero.
Thank you very much if you can help!
It's a bit of a lenghty one but not hard to understand, I made some sample data like so:
="C"&SUMPRODUCT(((VALUE(LEFT($A$1:$B$3,SEARCH("-",$A$1:$B$3)-1))<=D3)*(VALUE(RIGHT($A$1:$B$3,LEN($A$1:$B$3)-SEARCH("-",$A$1:$B$3)))>=D3))*COLUMN($A$1:$B$3))&", "&"R"&SUMPRODUCT(((VALUE(LEFT($A$1:$B$3,SEARCH("-",$A$1:$B$3)-1))<=D3)*(VALUE(RIGHT($A$1:$B$3,LEN($A$1:$B$3)-SEARCH("-",$A$1:$B$3)))>=D3))*ROW($A$1:$B$3))
Youll just have to change ranges
If you set up your worksheet like the image below (sheet name "Library"):
You could try the code:
Option Explicit
Sub Painting()
Dim LastRow As Long, i As Long, SearchValue As Long
Dim arrData As Variant
'Let s say that all data appears in sheet called Library
With ThisWorkbook.Worksheets("Library")
'Let s say that we are looking for the value in cell H1
SearchValue = .Range("H1").Value
'Find the Last row of column A sheet Library
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
'Create as array from Column A row 2 up to Column D row LastRow
arrData = .Range(.Cells(2, 1), .Cells(LastRow, 4)).Value
'Loop Array to find a match
For i = LBound(arrData) To UBound(arrData)
If arrData(i, 3) <= SearchValue And arrData(i, 4) >= SearchValue Then
'Pop up with Shelf & Box name
MsgBox "Search Value " & SearchValue & " included in:" & vbNewLine & "Shelf: " & arrData(i, 1) & vbNewLine & "Box: " & arrData(i, 2)
'Select the line where the Search Value found
.Range("A" & i + 1 & ":D" & i + 1).Select
'Exit the loop
Exit For
End If
Next i
End With
End Sub
Result:

Comparing two data tables on different tabs in Excel using VBA

I am relatively new to Macros and VBA in Excel, so I need some guidance on how to solve my current issue.
The end goal of my project is to have a macro compare two sets of data organized into rows and columns (We'll say table A is the source data, and table B is based off of user input). Each row in table B should correspond to a row in table A, but they could be out of order, or there could be incorrect entries in table B.
My thought is that for the first row in each table, the macro would compare each cell left to right:
If Sheets("sheet1").Cells(2, 1) = Sheets("sheet2").Cells(2, 1) Then
If Sheets("sheet1").Cells(2, 2) = Seets("sheet2").Cells(2, 2)
Ect, ect.
My problem comes in when the Cell in table B does not match Table A.
First, I would want it to check B row 1 against the next row in A, and keep going throughout table A until it finds a "complete match" with all five columns of the row matching.
I've been trying to do this with Else if and For/Next staements
For row= 2 to 10
'if statements go here
Else If Sheets("sheet1").Cells(2, 1) <> Sheets("sheet2").Cells(2, 1)
Next row
I may be completely misunderstanding the syntax here, but I have yet to produce a situation where if the criteria is not met, it goes to the next row.
If no complete match is found, the last cell in table B row 1 that couldn't be matched should be highlighted.
Then regardless of whether a match was found or not, we would move to table B row 2, and start the whole process over.
So, I have the logic worked out (I think), where the comparison ifs would be inside a loop (or something) that would cycle through table A row by row. Then that whole process would be in another loop (or something) that would cycle through Table B.
At the end of the process, there would either be no highlighted cells showing that all entered data is correct, or cells would be highlighted showing data that do no match.
I am fairly certain that the cycling through table B is not the issue. Rather, I'm having difficulty getting the Macro to move to the next table A row if something doesn't match.
Please let me know if I need to elaborate on anything.
Thanks!
You could try:
Option Explicit
Sub test()
Dim Lastrow1 As Long, Lastrow2 As Long, i As Long, j As Long
Dim Str1 As String, Str2 As String
'Find the last row of sheet 1
Lastrow1 = Sheet1.Cells(Sheet1.Rows.Count, "A").End(xlUp).Row
'Find the last row of sheet 2
Lastrow2 = Sheet2.Cells(Sheet2.Rows.Count, "A").End(xlUp).Row
For i = 2 To Lastrow1
'Let us assume that table has 3 columns. Merge 3 columns' values and create a string for each line
Str1 = Sheet1.Cells(i, 1).Value & "_" & Sheet1.Cells(i, 2).Value & "_" & Sheet1.Cells(i, 3).Value
For j = 2 To Lastrow2
'Let us assume that table has 3 columns. Merge 3 columns' values and create a string for each line
Str2 = Sheet2.Cells(j, 1).Value & "_" & Sheet2.Cells(j, 2).Value & "_" & Sheet2.Cells(j, 3).Value
'If both strings match a message box will appear
If Str1 = Str2 Then
MsgBox "Line " & i & " in table A match with line " & j & " in table B!"
Exit For
End If
Next j
Next i
End Sub
Sheet 1 structure:
Sheet 2 structure:

Why am I getting an error when trying to Fill Down using Active Sheet?

I am trying to search for specific terms and count the number of occurrences of that term within that column. I am using this macro by copying and pasting a macro button into my active sheet as the name of the worksheet I'll be applying this to will always be different/changing (each sheet is named for a month of the year so it will be changing every time I have a new month's worth of data). The length of the column is also variable with every given month.
This used to work without issue but recently I began getting a 1004 application defined or object defined error at the line that reads:
.Range("P7:R7").Formula = strFormulas
Are my formulas too long? Here's all of the code I am using for this section:
With ActiveSheet.Columns("P:T")
.ColumnWidth = 10
End With
Range("P6").Value = "DeptShortName"
Range("Q6").Value = "IfCAN"
Range("R6").Value = "Business Unit Name"
Range("S6").Value = "Business Unit"
Dim lastRow As Long
'Set formulas to appear in columns 1, 2, 3 after column O
Dim strFormulas(1 To 3) As Variant
'Set last row to fill for columns P-R based on variable length of column O
lastRow = Range("O" & Rows.Count).End(xlUp).Row
'Auto fill formulas defined below to columns 1,2, 3 after column O, so columns P, Q, R
With ActiveSheet
strFormulas(1) = "=IF(ISNUMBER(SEARCH(""CCS"",O7)),""CCS"",IF(ISNUMBER(SEARCH(""DSO"",O7)),""DSO"",IF(ISNUMBER(SEARCH(""ALC"",O7)),""ALC"",IF(ISNUMBER(SEARCH(""RCD"",O7)),""RCD"",IF(ISNUMBER(SEARCH(""CMNA"",O7)),""CMNA"",IF(ISNUMBER(SEARCH(""CRNA"",O7)),""CRNA"",IF(ISNUMBER(SEARCH(""CCSCAN"",O7)),""CCSCAN"",IF(ISNUMBER(SEARCH(""CARLYLE"",O7)),""CARLYLE"",IF(ISNUMBER(SEARCH(""CRS"",O7)),""CRS"",IF(ISNUMBER(SEARCH(""ICP"",O7)),""ICP"",IF(ISNUMBER(SEARCH(""MISCELLANEOUS"",O7)),""MISC"",IF(ISNUMBER(SEARCH(""NAO"",O7)),""NAO DIST"",IF(ISNUMBER(SEARCH(""NATIONAL"",O7)),""NATIONAL"",""""))))))))))))"
strFormulas(2) = "=IF(ISNUMBER(SEARCH(""CCSCAN"",O7)),""CAN"",IF(ISNUMBER(SEARCH(""ALC 0211"",O7)),""CAN"",IF(ISNUMBER(SEARCH(""ALC 0218"",O7)),""CAN"",IF(ISNUMBER(SEARCH(""ALC 0251"",O7)),""CAN"",""""))))"
strFormulas(3) = "=CONCATENATE(P7,Q7)"
.Range("P7:R7").Formula = strFormulas
.Range("P7:R" & lastRow).FillDown
End With

Resources