I need to transpose vertical data from column B to horizontal data.
My table looks like this:
85.98 | | | | | |
-------|--------|--------|--------|--------|--------|
97.62 | | | | | |
-------|--------|--------|--------|--------|--------|
100.00 | | | | | |
-------|--------|--------|--------|--------|--------|
100.00 | | | | | |
-------|--------|--------|--------|--------|--------|
| | | | | |
-------|--------|--------|--------|--------|--------|
89.81 | | | | | |
-------|--------|--------|--------|--------|--------|
78.70 | | | | | |
-------|--------|--------|--------|--------|--------|
100.00 | | | | | |
-------|--------|--------|--------|--------|--------|
| | | | | |
-------|--------|--------|--------|--------|--------|
94.32 | | | | | |
-------|--------|--------|--------|--------|--------|
| | | | | |
-------|--------|--------|--------|--------|--------|
| | | | | |
-------|--------|--------|--------|--------|--------|
| | | | | |
-------|--------|--------|--------|--------|--------|
90.91 | | | | | |
-------|--------|--------|--------|--------|--------|
0.00 | | | | | |
-------|--------|--------|--------|--------|--------|
88.54 | | | | | |
-------|--------|--------|--------|--------|--------|
76.96 | | | | | |
-------|--------|--------|--------|--------|--------|
94.32 | | | | | |
-------|--------|--------|--------|--------|--------|
89.11 | | | | | |
-------|--------|--------|--------|--------|--------|
And I want it to look like this:
85.98 | 97.62 | 100.00 | 100.00 | | |
-------|--------|--------|--------|--------|--------|
89.81 | 78.70 | 100.00 | | | |
-------|--------|--------|--------|--------|--------|
94.32 | | | | | |
-------|--------|--------|--------|--------|--------|
| | | | | |
-------|--------|--------|--------|--------|--------|
90.91 | 0.00 | 88.54 | 76.96 | 94.32 | 89.11 |
I am using the following code:
Sub Transpose()
Dim t As Range, u As Range
c = ActiveCell.Column
fr = ActiveCell.Row
lr = Cells(Rows.Count, c).End(xlUp).Row
r = fr
Do
Set t = Cells(r, c)
Set u = t.End(xlDown)
Range(t, u).Copy
t.Offset(, 1).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=True
r = u.End(xlDown).Row
Loop While r < lr
Application.CutCopyMode = False
End Sub
The problem is .End(xlDown) doesn't work because there are single rows of data. Is there a solution for this?
What you are trying to achieve can be accomplished with this...
Sub TransposeData()
Dim lr As Long
Dim rng As Range
Application.ScreenUpdating = False
lr = Cells(Rows.Count, 3).End(xlUp).Row
'Change the first row as required. C1 in the below line assumes that the data start from Row1 in Column C.
For Each rng In Range("C1:C" & lr).SpecialCells(xlCellTypeConstants, 1).Areas
rng.Copy
rng.Cells(1).Offset(0, 1).PasteSpecial xlPasteAll, Transpose:=True
Next rng
Application.CutCopyMode = 0
Application.ScreenUpdating = True
End Sub
Related
I would like to have some guidance or help to address the following problem:
I have the following data in a Spark Data frame.
I would like to create a window of n days preceding a succeeding a reference record and then calculate a division using reference values with the values in the window.
However I have not figured out how to do this kind of operation, everything that I find is just mean, count or sum operations in the window.
Original data looks like this:
| symbol_id | date | close | is_reference |
|----------|------------|----------|--------------|
| XXXX | 2000-01-19 | 809.9644 | FALSE |
| XXXX | 2000-01-20 | 784.274 | FALSE |
| XXXX | 2000-01-21 | 774.2831 | FALSE |
| XXXX | 2000-01-24 | 760.0106 | FALSE |
| XXXX | 2000-01-25 | 750.7335 | FALSE |
| XXXX | 2000-01-26 | 750.7335 | TRUE |
| XXXX | 2000-01-27 | 742.17 | FALSE |
| XXXX | 2000-01-28 | 749.3063 | FALSE |
| XXXX | 2000-01-31 | 750.02 | FALSE |
| XXXX | 2000-02-01 | 762.8653 | FALSE |
| XXXX | 2000-02-02 | 749.3063 | FALSE |
Expected output looks like this:
| symbol_id | date | close | is_reference | reference_change |
|----------|------------|----------|--------------|-------------------|
| XXXX | 2000-01-19 | 809.9644 | FALSE | 1.07889737170381 |
| XXXX | 2000-01-20 | 784.274 | FALSE | 1.04467697258748 |
| XXXX | 2000-01-21 | 774.2831 | FALSE | 1.03136878799201 |
| XXXX | 2000-01-24 | 760.0106 | FALSE | 1.0123573811479 |
| XXXX | 2000-01-25 | 750.7335 | FALSE | 1 |
| XXXX | 2000-01-26 | 750.7335 | TRUE | 1 |
| XXXX | 2000-01-27 | 742.17 | FALSE | 0.988593155893536 |
| XXXX | 2000-01-28 | 749.3063 | FALSE | 0.99809892591712 |
| XXXX | 2000-01-31 | 750.02 | FALSE | 0.999049596161621 |
| XXXX | 2000-02-01 | 762.8653 | FALSE | 1.01615992892285 |
| XXXX | 2000-02-02 | 749.3063 | FALSE | 0.99809892591712 |
I'm currently partition by symbol_id using the following snippet:
val window = Window.partitionBy(SYMBOL_ID)
.orderBy(col(DATE).desc)
.rowsBetween(5,0) // RangeBetween looks better but i just trying with rowsBetween for now
And trying to do something like this on reference_change column.
df
.withColumn("close_movement", $"close"/lit(col("close")
.where(col("is_reference") === true)).over(window)) // This command is wrong but its the most similar to thoughts in my mind.
So at the end I will be using the close WHERE is_reference = true divide by the close on the windows like the reference_change column we have on the expected output.
Thank you for your help!
I would just use a simple join:
val ref = df.filter($"is_reference")
df.join(ref, df.col("symbol_id") === ref.col("symbol_id") &&
abs(date_diff(df.col("date"), ref.col("date"))) <= 5)
.select(df.col("symbol_id"), df.col("date"), df.col("close"), df.col("is_reference"),
(df.col("close") / ref.col("close")).as("reference_change"))
Right now im working to generate a label based on quantity in excel. I managed to get it copy & paste based on value from cell. But, i didnt know how to make some cell change according to the loop.
Below is as example :
Current result :
| A | B | C | D | E |
|------------------------------- |----- |-------------------- |----- |----- |
| NMB IN DIA | | MADE IN THAILAND | | |
| INVOICE NO | : | MM035639 | | |
| C/NO | : | 1 | / | 2 |
| SHIP TO | : | A | | |
| QTY | : | 100 | | |
| NMB PARTS NO | : | SFASDF234 | | |
| | | *SFASDF234* | | |
| CUST PARTS NO | : | SFASDF234 | | |
| CUST ORDER NO | : | | | |
| ----------------------------- | --- | ------------------ | --- | --- |
| NMB IN DIA | | MADE IN THAILAND | | |
| INVOICE NO | : | MM035639 | | |
| C/NO | : | 1 | / | 2 |
| SHIP TO | : | A | | |
| QTY | : | 100 | | |
| NMB PARTS NO | : | SFASDF234 | | |
| | | *SFASDF234* | | |
| CUST PARTS NO | : | | | |
| CUST ORDES NO | : | | | |
Expected result :
| A | B | C | D | E |
|------------------------------- |----- |-------------------- |----- |----- |
| NMB IN DIA | | MADE IN THAILAND | | |
| INVOICE NO | : | MM035639 | | |
| C/NO | : | 1 | / | 2 |
| SHIP TO | : | A | | |
| QTY | : | 100 | | |
| NMB PARTS NO | : | SFASDF234 | | |
| | | *SFASDF234* | | |
| CUST PARTS NO | : | SFASDF234 | | |
| CUST ORDER NO | : | | | |
| ----------------------------- | --- | ------------------ | --- | --- |
| NMB IN DIA | | MADE IN THAILAND | | |
| INVOICE NO | : | MM035639 | | |
| C/NO | : | 2 | / | 2 |
| SHIP TO | : | A | | |
| QTY | : | 100 | | |
| NMB PARTS NO | : | SFASDF234 | | |
| | | *SFASDF234* | | |
| CUST PARTS NO | : | | | |
| CUST ORDES NO | : | | | |
As you can see on the expected result, the C/No is loop based on quantity. Not just copy paste. Is there anything I can add?
Below is my current code :
Private Sub CommandButton1_Click()
Dim i As Long
For i = 2 To Worksheets("Sheet3").Range("E3").Value
Range("A1:A9", Range("E9")).Copy Sheet3.Range("A65536").End(xlUp)(2)
Next i
End Sub
Just set the value of the relevant cell to i:
Private Sub CommandButton1_Click()
Dim i As Long
Dim NewLoc As Range
For i = 2 To Worksheets("Sheet3").Range("E3").Value
'Decide where to copy the output to
Set NewLoc = Sheet3.Cells(Sheet3.Rows.Count, "A").End(xlUp).OffSet(1, 0)
'Copy the range
Range("A1:E9").Copy NewLoc
'Change the value of the cell 2 rows down and 2 rows to the right
NewLoc.Offset(2, 2).Value = i
Next i
End Sub
I have made a Crosstab Query that should give information about the total working hours in each day for every Vessel we had in our small harbor.
my query:
TRANSFORM Sum(Main.WorkingH) AS SumOfWorkingH
SELECT DateValue([DeptDate]) AS [Date]
FROM Vessels INNER JOIN Main ON Vessels.ID = Main.VesselID
GROUP BY DateValue([DeptDate])
ORDER BY DateValue([DeptDate])
PIVOT Vessels.Vessel;
the problem here is this query is returning the total working hours start from departure date
| +---------------+--------+----+----+----+----+----+----+ |
| | | | | | | | | | |
| +---------------+--------+----+----+----+----+----+----+ |
| | Date | A1 | A2 | A3 | F3 | F4 | F5 | F6 | |
| | 26-May-17 | | | 32 | 29 | | | | |
| | 27-May-17 | 3 | 13 | | | | | | |
| | 28-May-17 | | | | | | | 73 | |
| | 29-May-17 | | | | 12 | 6 | 27 | | |
| | 01-Jun-17 | | | 10 | | 7 | 41 | | |
| | 02-Jun-17 | | 2 | 15 | 5 | | | | |
| | 03-Jun-17 | | 4 | | | | | | |
| +---------------+--------+----+----+----+----+----+----+ |
The desired Result
when a vessel leaves at 6/1 9pm and arrive back at 6/3 10am. This should appear as following:
6/1-->3Hours
6/2-->24Hours
6/3-->10Hours
**NOT** 6/1-->37Hours as in the previous table.
This is how it should look like
| +----------------+-----+----+----+----+--------+----+----+ |
| | Date | A1 | A2 | A3 | F3 | F4 | F5 | F6 | |
| +----------------+-----+----+----+----+--------+----+----+ |
| | 26-May-17 | | | 5 | 7 | | | | |
| | 27-May-17 | 3 | 13 | 24 | 21 | | | | |
| | 28-May-17 | | | 2 | | | | 9 | |
| | 29-May-17 | | | | 12 | 6 | 8 | 24 | |
| | 30-May-17 | | | | | | 18 | 24 | |
| | 31-May-17 | | | | | | | 15 | |
| | 01-Jun-17 | | | 10 | | 7 | 0 | | |
| | 02-Jun-17 | | 2 | 15 | 5 | 24 | | | |
| | 03-Jun-17 | | 4 | | | | 16 | | |
| +----------------+-----+----+----+----+--------+----+----+ |
These values are not accurate (I wrote them by hand), but I think you got the Idea
The Suggested Solution
while trying to fix this problem I made the following code which takes the
Public Function HoursByDate1(stTime, EndTime)
For dayloop = Int(EndTime) To Int(stTime) Step -1
If dayloop = Int(stTime) Then
WorkingHours = Hour(dayloop + 1 - stTime)
ElseIf dayloop = Int(EndTime) Then
WorkingHours = Hour(EndTime - dayloop)
Else
WorkingHours = 24
End If
HoursByDate1 = WorkingHours
Debug.Print "StartDate: " & stTime & ", EndDate:" & EndTime & ", The day:" & dayloop & " --> " & WorkingHours & " hours."
Next dayloop
End Function
It prints the data as following:
which is exactly what I want
But when I try to call this function from my query, It gets only the last value for each trip. as following:
| +-----------+----+----+----+----+----+----+----+ |
| | Date | A1 | A2 | A3 | F3 | F4 | F5 | F6 | |
| +-----------+----+----+----+----+----+----+----+ |
| | 5/26/2017 | | | 5 | 7 | | | | |
| | 5/27/2017 | 15 | 19 | | | | | | |
| | 5/28/2017 | | | | | | | 9 | |
| | 5/29/2017 | | | | 8 | 7 | 8 | | |
| | 6/1/2017 | | | 3 | | 6 | 0 | | |
| | 6/2/2017 | | 8 | 8 | 19 | | | | |
| | 6/3/2017 | | 9 | | | | | |
I seek any Solution: From VBA side of things or SQL Query Side.
Sorry for the very long question, but I wanted to show my effort on the subject because every time I am told that this is not enough Information
I have two sheets. WIRES and BOM.
WIRES looks like this:
| Ltg-Nr_ | Kurzname | Pin | Kurzname | Pin | Farbe |
|---------|----------|-----|----------|-----|-------|
| 712001 | AJ4 | 11 | LSTS | 7 | GE |
| 712002 | AJ4 | 10 | LSTS | 8 | SW |
| 712003 | KM_23.1 | 1 | KM_12.4 | 1 | BR |
| 712004 | AJ4 | 19 | GSR2 | 2 | GN |
| 712005 | AJ4 | 18 | GSR2 | 1 | SW |
| 712006 | AJ4 | 46 | CR_31AB | 1 | BR |
| 712007 | AJ4 | 49 | CR_CANP | 1 | OR/SW |
| 712008 | AJ4 | 50 | CR_CANM | 1 | OR/BR |
BOM looks like this:
| Con |
|------|
| GSR2 |
| AJ4 |
I want to do a macro in excel to search for each values from rows in sheet BOM, in sheet WIRES, and where it find the value to replace with value_"cell_in_front_of_it". After this put 1 in front of cells replaced.
For example, it has to search for GSR2 in sheet WIRES and where it find GSR2 to replase it with GSR2_2; next row replace GSR2 with GSR2_1.
After macro run i want table to look like this:
| Ltg-Nr_ | Kurzname | Pin | Kurzname | Pin | Farbe |
|---------|----------|-----|----------|-----|-------|
| 712001 | AJ4 | 11 | LSTS | 7 | GE |
| 712002 | AJ4 | 10 | LSTS | 8 | SW |
| 712003 | KM_23.1 | 1 | KM_12.4 | 1 | BR |
| 712004 | AJ4 | 19 | "GSR2_2" | "1" | GN |
| 712005 | AJ4 | 18 | "GSR2_1" | "1" | SW |
| 712006 | AJ4 | 46 | CR_31AB | 1 | BR |
| 712007 | AJ4 | 49 | CR_CANP | 1 | OR/SW |
| 712008 | AJ4 | 50 | CR_CANM | 1 | OR/BR |
I tried this:
I tried something like this:
Sub Macro1()
'
' Macro1 Macro
'
'
Dim Col As Integer
Dim Row As Integer
For Col = 2 To 4
For Row = 2 To 10
colo = Row + 1
Rows = Row + 1
Sheets("WIRES").Columns(2).Replace What:=Sheets("BOM").Cells(Row, 1).Text, Replacement:=Sheets("WIRES").Cells(Row, 2).Text & "_" & Sheets("WIRES").Cells(Rows, colo).Text, LookAt:=xlWhole, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Next Row
Next Col
End Sub
I made this, and it worked exactly as i wanted.
Thanks for your answers.
Dim roww, rowb As Integer
For roww = 2 To 150
For rowb = 1 To 10
Sheets("WIRES").Select
Cells(roww, 2).Select
If Selection.Value = Sheets("BOM").Cells(rowb, 1).Value Then
Selection.Value = Selection.Value & "_" & Selection.Offset(0, 1)
Selection.Offset(0, 1).Value = "1"
End If
Sheets("WIRES").Select
Cells(roww, 4).Select
If Selection.Value = Sheets("BOM").Cells(rowb, 1).Value Then
Selection.Value = Selection.Value & "_" & Selection.Offset(0, 1)
Selection.Offset(0, 1).Value = "1"
End If
Next rowb
Next roww
I'm having trouble deleting everything after and including the word APT and STE on Address column
If you look at the result blow, my vba code is deleting the word that the letters ste ( 2121 STEVENSON LN) or apt.
What is best way to to remove the word APT or STE and everything after that?
Below is my code,
Option Explicit
Sub Remove()
Dim Sht As Worksheet
Set Sht = ActiveWorkbook.Sheets("Data")
With Sht.Range("E:E")
.Replace "APT*", "", xlPart
.Replace "STE*", "", xlPart
End With
End Sub
My Data
+-------+-----+-----+----------+----------------------------------+-------+------+-----+
| Route | Pcs | Wgt | Location | Address | Suite | City | Zip |
+-------+-----+-----+----------+----------------------------------+-------+------+-----+
| SD-26 | 1 | 3 | | 5555 SOUTHWESTERN BLVD | | | |
| SD-26 | 1 | 7 | | 6666 EASTERN AVE APT 100 | | | |
| SD-05 | 1 | 1 | | 161112 HOMESTEAD ST | | | |
| SD-05 | 2 | 8 | | 2221 STEVENSON LN | | | |
| SD-04 | 1 | 8 | | 4040 OLD DENTON RD APT 2104 | | | |
| SD-04 | 1 | 3 | | 15811 E FRANKFORD RD APT 1507 | | | |
| SD-04 | 1 | 1 | | 835 WESTMINSTER DR | | | |
| SD-03 | 1 | 5 | | 9001 LAKESIDE CIR APT 5203 | | | |
| SD-03 | 1 | 3 | | 8880 UNION STATION PKWY APT 2104 | | | |
| SD-03 | 1 | 1 | | 420 E MAIN ST STE E | | | |
+-------+-----+-----+----------+----------------------------------+-------+------+-----+
Result
+-------+-----+-----+----------+--------------------------+-------+------+-----+
| Route | Pcs | Wgt | Location | Address | Suite | City | Zip |
+-------+-----+-----+----------+--------------------------+-------+------+-----+
| SD-26 | 1 | 3 | | 5555 SOUTHWE | | | |
| SD-26 | 1 | 7 | | 6666 EA | | | |
| SD-05 | 1 | 1 | | 161112 HOME | | | |
| SD-05 | 2 | 8 | | 2221 | | | |
| SD-04 | 1 | 8 | | 4040 OLD DENTON RD | | | |
| SD-04 | 1 | 3 | | 15811 E FRANKFORD RD | | | |
| SD-04 | 1 | 1 | | 835 WESTMIN | | | |
| SD-03 | 1 | 5 | | 9001 LAKESIDE CIR | | | |
| SD-03 | 1 | 3 | | 8880 UNION STATION PKWY | | | |
| SD-03 | 1 | 1 | | 420 E MAIN ST | | | |
+-------+-----+-----+----------+--------------------------+-------+------+-----+
Seems to me you can just include a space before and after APT/STE but before the wildcard character.
Sub RemoveAptSte()
Dim Sht As Worksheet
Set Sht = ActiveWorkbook.Sheets("Data")
With Sht.Range("E:E")
.Replace " APT *", vbNullString, xlPart
.Replace " STE *", vbNullString, xlPart
End With
End Sub
That should remove just about any false positive from consideration.