I have 2 columns, one with dates (column A:A, of type dd/mm/yyyy hh:mm) the other with values of a parameter (column B:B), registered at each date. But not all dates have registered values (in which case in column B I will have -9999.
What I need is to copy to other columns (say D:D and E:E) only the cells where there is a value other than -9999 and the correspondent date too. For example:
Example
My data series is pretty long, it can get to 10000 or more lines, so I cannot do this selection “manually”. I would prefer macros, not array formulae, because I want to choose the moment of the calculation.
This code should do what you are looking for. This will copy all rows with a value in column B, into columns D and E.'
Sub copyrows()
Dim RowNo, newRowNo As Long
RowNo = 2
newRowNo = 2
With ThisWorkbook.ActiveSheet
.Cells(1, 4).Value = "Date"
.Cells(1, 5).Value = "H_Selected"
Do Until .Cells(RowNo, 1) = ""
If .Cells(RowNo, 2) <> "-9999" Then
.Cells(newRowNo, 4) = .Cells(RowNo, 1)
.Cells(newRowNo, 5) = .Cells(RowNo, 2)
newRowNo = newRowNo + 1
End If
RowNo = RowNo + 1
Loop
End With
End Sub
Related
I am trying to write code that takes filtered criterium in column H (Associated work flow) from table below and than sums column G (Celková výše výnosu v EUR) according to columns C (Outbound/Inbound) and I (Úspěšně předáno) and place final number to Cell K3 by pressing button (Spustit).
I think it should work like this, i choose criterium in column H, for example Jade&Fountain, it will show me only rows that includes Jade&Fountain, i will press the button, and it will sum only those with Yes in column I + if there is I in column C, it will subtract from the total, if there is O, it will add to the total. Then it will show the total in Cell K3.
I add code i wrote, it should work, but i am not sure in the Do While Cells(i,2).SpecialCells(xlCellTypeVisible).Value <> "" section. Thanks for any response.
Sub While_loop()
Dim i As Integer
Dim x As Integer
i = 3
x = 0
Do While Cells(i, 2).SpecialCells(xlCellTypeVisible).Value <> ""
If Cells(i, 9).Value = "Yes" Then
If Cells(i, 3).Value = "O" Then
x = x + Cells(i, 7).Value
End If
If Cells(i, 3).Value = "I" Then
x = x - Cells(i, 7).Value
End If
End If
i = i + 1
Loop
Cells(2, 12).Value = x
End Sub
Table i am refering to
You can do that without VBA if you want to, following this tutorial https://exceljet.net/formula/count-visible-rows-only-with-criteria
Try this function (assuming data in row 3-15), the key is the SUBTOTAL function:
=SUMPRODUCT((C3:C15="O")*(I3:I15="Yes")*(SUBTOTAL(109,OFFSET(G3:G15,ROW(G3:G15)-MIN(ROW(G3:G15)),0,1))))
That gives the sum of all visible rows with your 2 criteria. If you want to deduct the I :
=SUMPRODUCT((C3:C15="O")*(I3:I15="Yes")*(SUBTOTAL(109,OFFSET(G3:G15,ROW(G3:G15)-MIN(ROW(G3:G15)),0,1))))-SUMPRODUCT((C3:C15="I")*(I3:I15="Yes")*(SUBTOTAL(109,OFFSET(G3:G15,ROW(G3:G15)-MIN(ROW(G3:G15)),0,1))))
English isn't my first language so I hope you will understand me
Beacuse I dont know anything about programming I am turning for help to you: Excel pros;)
I have a workbook with two sheets ( datafeed and record )
On "datafeed", column B, cells B2 to B400 I am capturing live prices from internet.
Currently I use the following :
Sub my_onTime()
Application.OnTime Now + TimeValue("00:00:01"), "my_Procedure"
End Sub
Sub my_Procedure()
With Sheets("record")
rw = .Cells(.Rows.Count, 1).End(xlUp).Row + 1
.Range(.Cells(rw, 1), .Cells(rw, 2)).Value = Sheets("datafeed").Range("a2:b2").Value
End With
ThisWorkbook.RefreshAll
my_onTime
End Sub
which currently records prices from "datafeed" to rows in "record".
Another problem is that records only one cell from "datafeed"; cell B2 . I dont know how to set it, so it would record all prices in range from cells B2:B400
My wish is that Excel would record price changes to"record" Sheet into columns not rows.
"Graphic" display what I want:
Sheet1 - "datafeed" ... B2 = 155............Sheet 2 - "record" cells : F2 = 155.....G2 = 150.....H2 - 145.....I2 - 140,....and so on
Sheet1 - "datafeed" ... B3 = 66.............Sheet 2 - "record" cells : F3 = 66.....G3 = 67....H3 - 66.....I3 - 65,....and so on
Sheet1 - "datafeed" ... B4 = 1015.............Sheet 2 - "record" cells : F4 = 1015.....G4 = 1025....H4 - 1035....I4 - 1045,....and so on
Also last recorded price must be put in first column Sheet 2 - "record"; cells: , F2,F3,F4.......(Right now last recorded value is put in last row )
Any help will be greatly appreciated.
Thank you!
p.s
I have added 3 pics for clarification
1. sheet datafeed
2. sheet record
3. what i wish for sheet record
replace this part of your procedure
With Sheets("record")
rw = .Cells(.Rows.Count, 1).End(xlUp).Row + 1
.Range(.Cells(rw, 1), .Cells(rw, 2)).Value = Sheets("datafeed").Range("a2:b2").Value
End With
with this
With Sheets("record")
rw = .Cells(.Rows.Count, 5).End(xlUp).Row + 1
.Cells(rw, 5).Value = Sheets("datafeed").Cells(2, 1).Value 'write name from column A
iColumn = 6 'set you want to start in column F with prices
For iRow = Sheets("datafeed").Cells(Sheets("datafeed").Rows.Count, 1).End(xlUp).Row To 2 Step -1
.Cells(rw, iColumn).Value = Sheets("datafeed").Cells(iRow, 2).Value
iColumn = iColumn + 1
Next iRow
End With
if you want the new line added to first line instead of the last
With Sheets("record")
iColumn = 6 'set you want to start in column F with prices
iRecordRow = 3 'set to what row number to put the current price
.Rows(iRecordRow & ":" & iRecordRow).Insert Shift:=xlDown
For iRow = Sheets("datafeed").Cells(Sheets("datafeed").Rows.Count, 1).End(xlUp).Row To 2 Step -1
.Cells(iRecordRow, iColumn).Value = Sheets("datafeed").Cells(iRow, 2).Value
iColumn = iColumn + 1
Next iRow
.Cells(iRecordRow, 5).Value = Sheets("datafeed").Cells(2, 1).Value 'write name from column A
End With
if you want it added to different row than 3 just change number in iRecordRow = 3
This image shows the before. https://drive.google.com/open?id=0B8BmxxuBoGYnVkhDaEF2b1J6ejA
The objective of the code is to look at the value of the first cell of column 1 then look for that same value in Column 4 by going down the Column. In the case of the first cell in Column 1 it would be honey and the corresponding row in Column 4 is 6. Then it will duplicate the values from Column 5 and Column 6 that corresponds with honey in Column 4 and put it in Column 2 and Column 3 in the row that corresponds with honey for Column 1. Every time a cell in Column 2 or Column 3 is filled it will be colored blue. I don't know how to get the syntax right to set a string in one cell equal to a string in another cell and determining if the cell is blank in the first place.
This image shows the after.
https://drive.google.com/open?id=0B8BmxxuBoGYnX1VXWllaQTAxWFE
Sub checkcolumns()
'j determines the row for Column 1. n determines the row for Column 4'
Dim j As Integer
Dim n As Integer
j = 1
n = 1
'The first part is a Do While loop and is intended to check if the first
'cell is filled with something. If it's not then the code won't run.'
Do While Cells(j,1).Value <> Not vbNullString
'The next part determines whether the first cell from Column 1 and
'first cell from Column 4 are the same. If they aren't then it will
'search for the cell in Column 4 that has the same value. n denotes the row
'for column 4 and the Do Until loop will determine which row in column 4
'has the exact value as the cell we're looking at from Column 1
if Cell(j,1) NotEqual Cell(n, 4)
Do Until cell(j, 1) Equalto Cell(n, 4)
n = n + 1
End
'The next if statements first determine whether Column 2 of the row we're
'looking at has a value already. If it does not then that cell is
'populated with whatever value is in Column 5 of the corresponding row for
'Column 4 which is found with n. This is repeated for Column 3 using
'Column 6.
if Cells(j, 2).Value <> vbNullString Then
Cells(j, 2) = Cells(n, 5)
Cells(j, 2).Interior.ColorIndex = 5
End if
if Cells(j, 3).Value <> vbNullString Then
Cells(j, 3) = Cells(n, 6)
Cells(j, 3).Interior.ColorIndex = 5
End if
'This else statement below is for the case where the cell value from
'Column 1 on that row is equal to the cell value of Column 4 on that
'same row, so j and n would be equal.
Else
if Cells(j, 2).Value <> vbNullString Else
Cells(j, 2) = Cells(n, 5)
Cells(j, 2).Interior.ColorIndex = 5
End If
if Cells(j, 3).Value <> vbNullString Else
Cells(j, 3) = Cells(n, 6)
Cells(j, 3).Interior.ColorIndex = 5
End If
End If
'Once it has checked the first row in Column 1. It will then look at the
'second row.
j = j + 1
End
End Sub
Put this formula in B2:
=VLOOKUP(A2,$D$2:$F$7,2,FALSE)
And then put this formula in C2:
=VLOOKUP(A2,$D$2:$F$7,3,FALSE)
A2 is the value you want to search in column D
$D$2:$F$7 creates a static table to search
2 or 3 is the column in that table (from first col of table) you want returned
False requires an exact match on the search
Once you put them in the cells, drag them down.
I have a table where user can insert multiple rows over multiple columns where some data is string and some numeric. I want to create a button such that when the user clicks it, it will create a new table on the same excel sheet but with some of the rows combined based on predefined condition.
Eg. The table "pre defined condition" states that alpha and gamma are similar and so on(it can many rows like this which show the conditions to combine rows..condition will always pertain to second row of the user defined table i.e table 1)...Table 1 will be created by a different user and he can enter as many rows as he wishes to. So using these 2 tables (Table 1 & Pre defined condition tabel) I want to create a new table which has certain rows combined with stringfrom two rows separated using "/" and numbers added.
The structure will remain the same for all tables.
Edit:One value in column 2 will always have same value in column 1.Basically column 2 is a dependent list(on column 1 ). There can be many pre -defined conditions and not just limited to 2 . Usually there won't be any duplicate values in column 2,but in case there are I want to combine them in a row at click of the button.
Table 1
A Alpha 100 1
B Beta 200 2
C Gamma 300 3
D Kappa 400 4
Pre Defined Condition
Alpha Gamma
Beta Kappa
Desired Output
A/C Alpha/Gamma 400 4
B/D Beta/Kappa 600 6
Assuming that your data starts in A2:D2 (A1:D1 left for titles), that you state two conditions (for example Alpha Gamma) in columns F and G (starting in the second row; first row left for titles), that there is a command button, and that the worksheet is named "Sheet1", the following code should do the trick.
Dim i As Integer
Dim j As Integer
Dim lLastRowPDC As Integer
Dim lLastRowData As Integer
Dim sConditions As String
Dim sOrigin As String
Dim sColumnA As String
Dim sColumnB As String
Dim iColumnC As Integer
Dim iColumnD As Integer
Private Sub CommandButton1_Click()
lLastRowPDC = Worksheets("Sheet1").Cells(2, 6).End(xlDown).Row 'Rows with Conditions, starting in the second row
lLastRowData = Worksheets("Sheet1").Cells(2, 1).End(xlDown).Row 'Rows with data, starting in the second row
For i = 2 To lLastRowPDC
sConditions = Worksheets("Sheet1").Cells(i, 6).Value & Worksheets("Sheet1").Cells(i, 7).Value 'create a string with the two conditions
sColumnA = ""
sColumnB = ""
iColumnC = 0
iColumnD = 0
For j = 2 To lLastRowData
sOrigin = Worksheets("Sheet1").Cells(j, 2).Value
If InStr(sConditions, sOrigin) > 0 Then
If InStr(sColumnA, Worksheets("Sheet1").Cells(j, 1).Value) = 0 Then
sColumnA = sColumnA & Worksheets("Sheet1").Cells(j, 1).Value & "/"
End If
If InStr(sColumnB, Worksheets("Sheet1").Cells(j, 2).Value) = 0 Then
sColumnB = sColumnB & Worksheets("Sheet1").Cells(j, 2).Value & "/"
End If
iColumnC = iColumnC + Worksheets("Sheet1").Cells(j, 3)
iColumnD = iColumnD + Worksheets("Sheet1").Cells(j, 4)
End If
Next j
sColumnA = Left(sColumnA, Len(sColumnA) - 1) 'remove last "/"
sColumnB = Left(sColumnB, Len(sColumnB) - 1) 'remove last "/"
Worksheets("Sheet1").Cells(i, 8).Value = sColumnA
Worksheets("Sheet1").Cells(i, 9).Value = sColumnB
Worksheets("Sheet1").Cells(i, 10).Value = iColumnC
Worksheets("Sheet1").Cells(i, 11).Value = iColumnD
Next i
End Sub
I have wbk1.worksheet(1) and wbk2.worksheet(1).
wbk1.worksheet(1) has a list of values in column A
wbk2.worksheet(2) has the same list of values that may occur multiple times in column A with a number value in the offset(0,1) cell.
I need to do an index or match to find all of values in wbk2 and sum all of the offset(0,1) values. Then take that sum and put it in the offset(0,1) cell in wbk1.worksheets(1).
Example:
Workbook 1, sheet 1
Column A Column B
value 1
value 2
value 3
Workbook 2, sheet 1
Column A Column B
value 1 15
value 2 2
value 1 3
value 1 12
End Result:
Workbook 1, sheet 1
Column A Column B
value 1 30
value 2 2
value 3 0
I've tried doing a for each loop, but I'm still a noob to vb, so clearly not doing something right:
For Each x In rngWbk1
Set cellrngwbk2 = wbk2.Worksheets(1).Cells.Find(What:=x, LookIn:=xlValues)
If Not cellrngwbk2 Is Nothing Then
For Each y In rngwbk1
If y = cellrngwbk2 Then
total = total + cellrngwbk2.Offset(0, 1).Value
Else
End If
Next y
x.Offset(0, 1).Value = total
total = 0 'resets total value for next x value
Else
End If
next x
If VBA is not a requirement, a simple =SUMIF() statement has the same effect.
The function would look something like this:
=SUMIF([Wbk2.xlsx]Sheet1!A2:A5,Table1[[#This Row],[ID]],[Wbk2.xlsx]Sheet1!B2:B5)
There is more efficient way. You can use SUMIF formula to calculate values and then rewrite formula with result values. If rngWbk1 corresponds to the values in column A in wbk1.worksheets(1), you can use following code:
Dim frm As String
Dim startCell As String
startCell = Replace(rngWbk1.Cells(1, 1).Offset(0, -1).Address, "$", "")
frm = "=SUMIF('[" & wbk2.Name & "]" & wbk2.Worksheets(1).Name & "'!A:A," & startCell & ", '[" & wbk2.Name & "]" & wbk2.Worksheets(1).Name & "'!B:B)"
With rngWbk1.Offset(0, 1)
.Formula = frm
.Calculate
.Value = .Value
End With
If rngWbk1 doesn't correspond values in column A, you need to adjust startCell in example to startCell = "A2" and change With rngWbk1.Offset(0, 1) to sth like With wbk1.Worksheets(1).Range("B1:B100")