Macro to reconstruct receipts from database - database

I need some help with a database I am trying to create on excel. Currently, I managed to build a system where I paste on to the excel sheet a receipt, then a macro extract certain pieces of information and stores it in another sheet, something like this;
BUYER SELLER DATE PRODUCTS CURRENCY
A B 123 abc USD
D E 456 def GBP
Now, I can search this database using simple filtering. What I would to do now is, once I have filtered and am left with, lets say, 5 entries, I would like those to be reconstructed in another sheet, looking like the receipts do originally e.g
123 456
A D
B E
USD GBP
a d
b e
c f
I know I need to loop through each row and once in a row, loop through each column to extract the required piece of information(e.g date, products etc).
I have looked around and couldn't find anything.
Any help would be appreciated, thank you.

I think that this can help you to start:
Sub From_DB()
Dim i As Long
Dim col As Integer
Dim DB_Sheet, Rec_Sheet As Object
Set DB_Sheet = ThisWorkbook.Worksheets("Database")
Set Rec_Sheet = ThisWorkbook.Worksheets("Receipts")
col = 1
For i = 2 To DB_Sheet.Range("A" & Rows.Count).End(xlUp).Row
If DB_Sheet.Rows(i).Hidden = False Then
Rec_Sheet.Cells(1, col) = DB_Sheet.Cells(i, 3)
Rec_Sheet.Cells(2, col) = DB_Sheet.Cells(i, 1)
Rec_Sheet.Cells(3, col) = DB_Sheet.Cells(i, 2)
Rec_Sheet.Cells(4, col) = DB_Sheet.Cells(i, 5)
Rec_Sheet.Cells(5, col) = DB_Sheet.Cells(i, 4)
col = col + 1
End If
Next i
End Sub

Related

Effectively using 2D VBA Array variable and fetch whenever required

I am trying to develop a mechanism where I can store all the values in 2D array for below table.
Below is the code that I have developed, just to store values.
Sub temp()
Dim QtyArray(4, 11) As Integer, rw As Integer, col As Integer
For rw = 0 To 4
For col = 0 To 11
QtyArray(rw, col) = Cells(rw + 2, col + 3).Value
MsgBox QtyArray(rw, col)
Next col
Next rw
End Sub
I want to apply logic as per below:
First identify columns where COUNTA is 1 (highlighted in red) and move 1 qty of stock from that store to another store where COUNTA > 2, priority would be in order from store A to L. For example, first store with COUNTA=1 is store D, and it has qty 2. So I want to move these 2 qty to store E (not store C because it has already 1 qty, also H has 2 qty.)
If the COUNTA of any store's qty is zero, nothing to be done.
I want to fetch separate report as per below format, showing how the stock is being moved across store as per logic rule 1.
Output:
Explained in previous section.

Import range query formula

Hello I need help with a formula. I want to pull certain information from all 3 sheets and putting it on another google sheet spreadsheet.
Example of the info I need is all of Driver Bs information from Column A, E, F, H, I, K from week 6.There is more information on Sheet 2 and 3 from that week also that needs to be pulled.
I have this idea for the formula. I put U without a number next to it because there is rows of information constantly being added to the google docs.
=IMPORTRANGE("https google sheet goes here","Sheet1!A1:U" "Sheet2!A1:U" "Sheet3!A1:U"}, "where Col4 = 'Driver A'", "where Col21 = '6'", 0)
try:
=QUERY({
IMPORTRANGE("1tnAtFgxCl2DnyYL0cjzijsqw8WlvC1BxI80hduaPwi0", "Sheet1!A1:U");
IMPORTRANGE("1tnAtFgxCl2DnyYL0cjzijsqw8WlvC1BxI80hduaPwi0", "Sheet2!A1:U");
IMPORTRANGE("1tnAtFgxCl2DnyYL0cjzijsqw8WlvC1BxI80hduaPwi0", "Sheet3!A1:U")},
"select Col1,Col5,Col6,Col8,Col9,Col11
where Col4 = 'Driver A'
and Col21 = 12", 1)

Excel VBA code needed to copy row if criteria is met

I have an excel document with two sheets. Sheet 1 has columns A-Q and Sheet 2 has columns A-H. What I need is a code that will copy the information in a row from sheet 1 to sheet 2 if the criteria is met. The criteria is the word "Awarded" in column L (Sheet 1).
Also is it possible to have only specific columns in the row copied?
A B C D E F G H I J K L M N
X X Awarded X X
I would like to have only columns C,D,M, and N copied from the row if the word "awarded" is in column L. This information would be copied to Sheet 2 in the following fashion
Sheet 1 Sheet 2
D --> B
C --> C
M --> D
N --> F
I hope I'm being clear. Thanks in advance and let me know if I need to clarify!+
This is the code I currently have, which works. Only problem is it copies the entire row of information into sheet 2 when I only want rows D,C,M, and N to be copied.
Sub testing()
Set a = Sheets("Sheet1")
Set b = Sheets("Sheet2")
Dim d
Dim j
d = 1
j = 2
Do Until IsEmpty(a.Range("L" & j))
If a.Range("L" & j) = "Awarded" Then
d = d + 1
b.Rows(d).Value = a.Rows(j).Value
End If
j = j + 1
Loop
End Sub
First what you should do is change your data structure. Assuming you are using Excel 2007 or later, there is a great feature called Tables. If you highlight all of your data and go to Insert->Table, select the "My Table Has Headers" checkbox, and press ok, you will see a nicely formatted table. Do that for both of the data sets on each sheet.
This is more than just pretty formatting though, it is what is called a ListObject. In your VBA code, use the following to reference it:
Dim Table1 as ListObject, Table 2 as ListObject
Dim HeaderIndex as Integer
Dim MyColumnRange as Range
Set Table1 = Sheet1.ListObjects("TableName1")
`Change the table name under Formulas->Name Manager
Set Table2 = Sheet1.ListObjects("TableName2")
HeaderIndex = Application.WorksheetFunction.Match("ColumnLHeaderName", _
Table1.HeaderRowRange, 0)
Set MyColumnRange = Table1.ListColumns(HeaderIndex).DataBodyRange
MyColumnRange.Select
At this point, the select statement is just to show you what range you are dealing with now. The HeaderIndex refers to the header sub component of the table ListObject. Using Match() will allow you to specify the name of the column header without hard coding it's position. (i.e. if your data starts in column A, the header value in column L will return HeaderIndex = 12)
Now that you know what column you want, you select the ListColumn object. Then, the DataBodyRange is used to select the range component of that object. This is the entire range in that column. You can then iterate down the list to find the data you want.
EDIT: Updated Example:
'Specify your ranges you will be copying from beforehand, adding as many as you need here.
HeaderIndex_D = Application.WorksheetFunction.Match("ColumnXHeaderName", _
Table1.HeaderRowRange, 0)
HeaderIndex_C = Application.WorksheetFunction.Match("ColumnXHeaderName", _
Table1.HeaderRowRange, 0)
HeaderIndex_M = Application.WorksheetFunction.Match("ColumnXHeaderName", _
Table1.HeaderRowRange, 0)
HeaderIndex_N = Application.WorksheetFunction.Match("ColumnXHeaderName", _
Table1.HeaderRowRange, 0)
Set ColumnRange_D= Table1.ListColumns(HeaderIndex_D).DataBodyRange
Set ColumnRange_C= Table1.ListColumns(HeaderIndex_C).DataBodyRange
Set ColumnRange_M= Table1.ListColumns(HeaderIndex_M).DataBodyRange
Set ColumnRange_N= Table1.ListColumns(HeaderIndex_N).DataBodyRange
'Now, loop through each row that exists in your table. If the testing
'condition contained in MyColumnRange you previously defined is met,
'then assign the destination cell (which can be defined in the same way
'as above) equal to the lookup range's current row value (specified by i)
For i = 1 to MyColumnRange.Rows.Count
If MyColumnRange(i) = "Awarded" Then
DestinationCell1.Value = ColumnRange_D(i)
DestinationCell2.Value = ColumnRange_C(i)
DestinationCell3.Value = ColumnRange_M(i)
DestinationCell4.Value = ColumnRange_N(i)
End If
Next i

VBA create loop for column B based on value in column A

I have a code that does what I need. The problem is I have to do the range select manually I'd like to create a conditional loop.
Here's my code that I used so far
Sub Worksheet_functions()
Dim Sumtotal As Long
Sumtotal = WorksheetFunction.Sum(Selection)
Range("e2").Value = Sumtotal
Range("h2").Value = Range("e2") + Range("h2")
End Sub
This is what my data looks like
Price Volume E H
10 100
10 50
10 80
9 100
9 50
8 100
8 100
10 50
10 250
At the moment I'm manually selection the volume column from the top down based on the value in price column.
The conditions are
If The prices are the same continue selecting them until they are not
If the price goes down then those selected cells should be summed in e4 and added to h4
If the price goes up then those selected cells should be summed in e2 and added to h4
For my example this would look like:
( H2: 100+50+80 +50+250 = 530 )
( H4: 100+50 +100+100 = 350 )
Price Volume
( 10 100
100+50+80 < ( 10 50
( 10 80
Above volume to H2
(9 100
100+50 < (9 50
Above volume to H4 ( because 10>9)
(8 100
100+100 < (8 100
Above volume to H4 ( because 9>8)
(10 50
50+250 < (10 250
Above volume to H2 ( because 8<10)
Any suggestions on
How do I use the conditions to make it loop?
How do write a code that takes in consideration the last price?
Here's a way to get what you want.
It is much difficult to create loop and Nested If's to do this.
I let Excel do the job and used Formula instead.
Let say your data is in A1:B10.
In E2 type this in formula bar.
=IF(A2=A3,IF(ISNUMBER(A1),IF(A1<A2,1,IF(A1=A2,E1,0)),1),IF(ISNUMBER(A1),IF(A1<A2,1,IF(A1=A2,E1,0)),1))
Copy the formula all the way down until E10 or up to where you have data.
In H2, type this in formula bar:
=SUMIF(E:E,1,B:B)
Similarly in H4, type this in formula bar:
=SUMIF(E:E,0,B:B)
However, you want code in VBA which is below also using above formula:
Dim ws as Worksheet, lrow as long
Set ws = Thisworkbook.Sheets("Sheet1")'assuming your data is in Sheet1
With ws
lrow = .Range("A" & .Rows.Count).End(xlUp).Row
.Range("E2:E" & lrow).Formula = "=IF(A2=A3,IF(ISNUMBER(A1),IF(A1<A2,1,IF(A1=A2,E1,0)),1),IF(ISNUMBER(A1),IF(A1<A2,1,IF(A1=A2,E1,0)),1))"
.Range("E2:E" & lrow).Value = .Range("E2:E" & lrow).Value
.Range("H2").Formula = "=SUMIF(E:E,1,B:B)"
.Range("H2").Value = .Range("H2").Value
.Range("H4").Formula = "=SUMIF(E:E,0,B:B)"
.Range("H4").Value = .Range("H4").Value
End With
End Sub
Kinda late, but I hope this is what you want.

Loop in Excel VBA

I have a logic in place, but no idea as to how to execute/code it in Excel. The logic is below:
At office, I need to find out the value of many old articles based on their purchase value and age. I don't want to use VDB or other inbuilt functions.
In my spreadsheet:
A1 = "Table" (Name of the article)
B1 = "01/01/2005" (Date of purchase in MM/DD/YYYY format)
C1 = "1000" (Purchase value)
D1 = "=DATEDIF(B1,TODAY(),"Y")" (Gives the age of article in years)
E1 = "20" (Percentage depreciation for first year, which varies based on article)
F1 = "=C1*10%" '(Depreciated value should not be less than 10% of purchase value)
Now, in G1 I want to calculate the depreciation value of article "A1" with purchase value "C1" for "D1" number of years, # flat "E1"% for first year and 10% for subsequent years.
H1 = "=C1-G1" (Value of the article after depreciation)
I1 = "=IF(H1<=F1,F1,H1)"
Please help me with a macro or formula to loop and find out the value of G1.
Also, I want to apply this to "n" number of rows since there are "n" number of articles.
EDIT
#assylias Thanks for enlightening me about SO policy, putting me to work for myself to find the answer.
After some 30 min of googling followed by trial and error, I successfully wrote a macro to do what I wanted. Here it goes:
Sub DepVal()
'
' DepVal Macro
' Macro by Prashanth JC
'
' Keyboard Shortcut: Ctrl+d
'
fYear = 0
dVal = 0
tYear = ActiveCell.Offset(0, -3)
purVal = ActiveCell.Offset(0, -4)
depFirst = ActiveCell.Offset(0, -2)
depOther = 10
If tYear >= 1 Then
Do
If fYear = 0 Then
dVal = purVal - (purVal * depFirst) / 100
Else
dVal = dVal - (dVal * depOther) / 100
End If
fYear = fYear + 1
Loop Until fYear = tYear
ActiveCell.Value = dVal
Else
ActiveCell.Value = purVal
End If
End Sub
Finally, I formatted the G1 cell to a Number with Zero decimal place. Everything works perfect now!
Thanks again, SO and assylias!
You don't need a macro, it's simple math.
Assuming that your percentage (in E1) is stored as .2 (for 20%), not actually 20, this formula would work:
=MAX(F1,C1*(1-E1)-(C1*0.1*(D1-1)))

Resources