Remove a specific word and everything after in a Cell - vba

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.
  

Related

First Column values are taking null values place

I have 3 records in pdatatest1:
| ID1 | Name1 | Class1 |
|------|-----------|--------|
| 1 | Methushal | 11 |
| 2 | Bush | 11 |
| 3 | Paul | 11 |
Datelist table is ptable:
| iDate | INAM |
|------------|------|
| 01-06-2022 | AM |
| 02-06-2022 | AM |
| 03-06-2022 | AM |
| 04-06-2022 | AM |
| 05-06-2022 | AM |
| 06-06-2022 | AM |
I inserted values are record1:
| ID | Name | Class | Indate | Intime | INAM1 |
|------|-----------|-------|------------|----------|-------|
| 1 | Methushal | 11 | 01-06-2022 | 08:00:00 | P |
| 1 | Methushal | 11 | 02-06-2022 | 08:00:00 | A |
| 1 | Methushal | 11 | 03-06-2022 | 08:00:00 | A |
| 1 | Methushal | 11 | 04-06-2022 | 08:00:00 | P |
| 1 | Methushal | 11 | 05-06-2022 | 08:00:00 | A |
My SQL Code giving result as below:
| dates12 | Name1 | INAM | INAM1 |
|------------|-----------|------|-------|
| 01-06-2022 | Methushal | AM | P |
| 01-06-2022 | Bush | AM | P |
| 01-06-2022 | Paul | AM | P |
| 02-06-2022 | Methushal | AM | A |
| 02-06-2022 | Bush | AM | A |
| 02-06-2022 | Paul | AM | A |
| 03-06-2022 | Methushal | AM | A |
| 03-06-2022 | Bush | AM | A |
| 03-06-2022 | Paul | AM | A |
| 04-06-2022 | Methushal | AM | P |
| 04-06-2022 | Bush | AM | P |
| 04-06-2022 | Paul | AM | P |
| 05-06-2022 | Methushal | AM | A |
| 05-06-2022 | Bush | AM | A |
| 05-06-2022 | Paul | AM | A |
| 06-06-2022 | Methushal | AM | NULL |
| 06-06-2022 | Bush | AM | NULL |
| 06-06-2022 | Paul | AM | NULL |
Looking for the result like below:
| dates12 | Name1 | INAM | INAM1 |
|------------|-----------|------|-------|
| 01-06-2022 | Methushal | AM | P |
| 01-06-2022 | Bush | AM | NULL |
| 01-06-2022 | Paul | AM | NULL |
| 02-06-2022 | Methushal | AM | A |
| 02-06-2022 | Bush | AM | NULL |
| 02-06-2022 | Paul | AM | NULL |
| 03-06-2022 | Methushal | AM | A |
| 03-06-2022 | Bush | AM | NULL |
| 03-06-2022 | Paul | AM | NULL |
| 04-06-2022 | Methushal | AM | P |
| 04-06-2022 | Bush | AM | NULL |
| 04-06-2022 | Paul | AM | NULL |
| 05-06-2022 | Methushal | AM | A |
| 05-06-2022 | Bush | AM | NULL |
| 05-06-2022 | Paul | AM | NULL |
| 06-06-2022 | Methushal | AM | NULL |
| 06-06-2022 | Bush | AM | NULL |
| 06-06-2022 | Paul | AM | NULL |
My sql code is:
Select Case when ptable.iDate Is null
then record1.Indate else ptable.idate end as dates12,
pdatatest1.Name1, ptable.INAM, record1.INAM1
into project1
From ptable full Join record1
on ptable.iDate=record1.Indate,pdatatest1
Select * From project1
And I have another code getting error in sql query I didn't get any result.
SELECT iDate dates12,
Name1,
INAM,
(SELECT INAM1
FROM record1
WHERE record1.ID = pdatatest1.ID1
) INAM1
FROM pdatatest1
JOIN ptable
ORDER BY iDate
I'm working this in vb.net table SQL Query. Please someone have a look once to correct my code to get correct result which I'm looking for.
You can use a cross join and a left join. For example:
select a.idate, b.name, a.inam, c.inam1
from ptable a
cross join pdatatest1 b
left join record1 c on c.id = b.id and c.indate = a.idate

Please I need to select city calling which has number of KM travelled is greater than 1000

select CITY_CALLING
sum(DISTANCE_KM)
from REAL_TRIP join
SOURCE_CITY
on SOURCE_CITY.city_id = REAL_TRIP.city_id
group by 1
city_CALLING | sum |
Visakhapatnam | 14.5920725980000014 |
Hyderabad | 2759.24699709970082 |
San Diego | 87.3699351497999999 |
Moscow | 984.947118170600447 |
Alexandria | 8.96134862429999934 |
Prague | 86.0471747345999916 |
Recife | 20.7398930000000021 |
Leeds | 140.606494992300014 |
Copenhagen | 14.7657918324999997 |
Fresno | 29.6572209023999989 |
Tijuana | 61.7240377603999946 |
Baton Rouge | 7.05829104329999968 |
Krasnodar | 296.730780097399986 |
Sochi | 237.51827971039998 |
Cincinnati | 116.423747349400003 |
Guwahati | 1057.34938192379968 |
Champaign | 6.8250736618000003 |
Vienna | 1180.11211812669899 |
Charlotte | 150.293475570500021 |
Raleigh-Durham | 152.720579113999946 |
select CITY_CALLING
sum(DISTANCE_KM)
from REAL_TRIP join
SOURCE_CITY
on SOURCE_CITY.city_id = REAL_TRIP.city_id
group by 1
HAVING SUM(DISTANCE_KM) > 10000;

VBA Copy & Paste Loop ( Generate Field Number)

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

Crosstab query to show the working hours per day for each Vessel

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

How can I do this in SQL in a Single Statement?

I have the following MySQL table:
+---------+------------+------+--------+------+---------+------------+-------+---------+----------+------------+------------+
| Version | Yr_Varient | FY | Period | CoA | Company | Item | Mvt | Ptnr_Co | Investee | GC | LC |
+---------+------------+------+--------+------+---------+------------+-------+---------+----------+------------+------------+
| 201 | 1 | 2010 | 1 | 11 | 23 | 1110105000 | 60200 | | | 450000 | 450000 |
| 201 | 1 | 2010 | 1 | 11 | 23 | 2110300000 | 60200 | | | -520000 | -520000 |
| 201 | 1 | 2010 | 1 | 11 | 23 | 1220221600 | | | | 78080 | 78080 |
| 201 | 1 | 2010 | 1 | 11 | 23 | 2130323000 | | | | 50000 | 50000 |
| 201 | 1 | 2010 | 1 | 11 | 23 | 2130322000 | | | | -58080 | -58080 |
| 201 | 1 | 2010 | 1 | 11 | 23 | 3100505000 | | | | -275000 | -275000 |
| 201 | 1 | 2010 | 1 | 11 | 23 | 3200652500 | | | | 216920 | 216920 |
| 201 | 1 | 2010 | 1 | 11 | 23 | 3900000000 | | | | 58080 | 58080 |
| 201 | 1 | 2010 | 1 | 11 | 26 | 1110105000 | 60200 | | | 376000 | 376000 |
| 201 | 1 | 2010 | 1 | 11 | 26 | 2110300000 | 60200 | | | -545000 | -545000 |
| 201 | 1 | 2010 | 1 | 11 | 26 | 1220221600 | | | | 452250 | 452250 |
| 201 | 1 | 2010 | 1 | 11 | 26 | 2130323000 | | | | -165000 | -165000 |
| 201 | 1 | 2010 | 1 | 11 | 26 | 2130322000 | | | | -118250 | -118250 |
| 201 | 1 | 2010 | 1 | 11 | 26 | 3100505000 | | | | -937750 | -937750 |
| 201 | 1 | 2010 | 1 | 11 | 26 | 3200652500 | | | | 819500 | 819500 |
| 201 | 1 | 2010 | 1 | 11 | 26 | 3900000000 | | | | 118250 | 118250 |
| 201 | 1 | 2010 | 1 | 11 | 37 | 1110105000 | 60200 | | | 777000 | 777000 |
| 201 | 1 | 2010 | 1 | 11 | 37 | 2110308000 | 60200 | 43 | | -255000 | -255000 |
| 201 | 1 | 2010 | 1 | 11 | 37 | 2130321500 | | | | 180000 | 180000 |
| 201 | 1 | 2010 | 1 | 11 | 37 | 2130322000 | | | | -77000 | -77000 |
| 201 | 1 | 2010 | 1 | 11 | 37 | 2310407001 | | 1 | | -625000 | -625000 |
| 201 | 1 | 2010 | 1 | 11 | 37 | 3100505000 | | | | -2502500 | -2502500 |
| 201 | 1 | 2010 | 1 | 11 | 37 | 3200652500 | | | | 2425500 | 2425500 |
| 201 | 1 | 2010 | 1 | 11 | 37 | 3900000000 | | | | 77000 | 77000 |
| 201 | 1 | 2010 | 1 | 11 | 43 | 1110105000 | 60200 | | | 2600000 | 2600000 |
| 201 | 1 | 2010 | 1 | 11 | 43 | 1140161000 | 60200 | | 23 | 430000 | 430000 |
| 201 | 1 | 2010 | 1 | 11 | 43 | 1140161000 | 60200 | | 26 | 505556 | 505556 |
| 201 | 1 | 2010 | 1 | 11 | 43 | 1140160000 | 60200 | 37 | | 255000 | 255000 |
| 201 | 1 | 2010 | 1 | 11 | 43 | 1160163000 | 60200 | 99999 | 48 | 49428895 | 49428895 |
| 201 | 1 | 2010 | 1 | 11 | 43 | 1160163000 | 60200 | 99999 | 49 | 188260175 | 188260175 |
| 201 | 1 | 2010 | 1 | 11 | 43 | 2310405500 | | | | -237689070 | -237689070 |
| 201 | 1 | 2010 | 1 | 11 | 43 | 2110300000 | 60200 | | | -1000 | -1000 |
| 201 | 1 | 2010 | 1 | 11 | 43 | 2110300500 | 60200 | | | -3999000 | -3999000 |
| 201 | 1 | 2010 | 1 | 11 | 43 | 1220221600 | | | | 1571112 | 1571112 |
| 201 | 1 | 2010 | 1 | 11 | 43 | 2130321500 | | | | -805556 | -805556 |
| 201 | 1 | 2010 | 1 | 11 | 43 | 2130322000 | | | | -556112 | -556112 |
| 201 | 1 | 2010 | 1 | 11 | 43 | 3100505000 | | | | -836000 | -836000 |
| 201 | 1 | 2010 | 1 | 11 | 43 | 3200652500 | | | | 781000 | 781000 |
| 201 | 1 | 2010 | 1 | 11 | 43 | 3300715700 | | 99999 | 32 | -440000 | -440000 |
| 201 | 1 | 2010 | 1 | 11 | 43 | 3300715700 | | 99999 | 26 | -61112 | -61112 |
| 201 | 1 | 2010 | 1 | 11 | 43 | 3900000000 | | | | 556112 | 556112 |
+---------+------------+------+--------+------+---------+------------+-------+---------+----------+------------+------------+
I need to take all rows with Mvt = 60200 and multiply every GC and LC record in that row by 1.1 and add a new row containing the changes back into the same table with FY set to 2011.
How can I do all this in 1 statement?
Is it even possible to do all this in 1 statement (I know very little about SQL)?
Can this be done in standard SQL as the database will be ported to another Database Server?
I don't know which server it will be.
In standard SQL (there may be better ways in vendor-specific implementations but I tend to prefer standard stuff where possible):
insert into mytable (
Version, Yr_Varient, Period, CoA, Company, Item, Mvt, Ptnr_Co, Investee,
FY, GC, LC
) select
Version, Yr_Varient, Period, CoA, Company, Item, Mvt, Ptnr_Co, Investee,
2011, GC*1.1, LC*1.1
from mytable
where Mvt = 60200
-- and FY = 2010
You may also want to limit your select statement a little more depending on the results of your testing, such as uncommenting the and FY = 2010 line above to stop copying all your 2009 and 2008 data as well, if any. I asume you only wanted to carry forward the previous year's stuff with a 10% increase on GC and LC.
The way this works is to run the select which gives modified data for FY, GC and LC as per your request, and pump all those rows back into the insert.
insert into mytable (
Version,Yr_Varient,FY,Period,CoA,Company,Item,Mvt,Ptnr_Co,Investee,GC,LC)
SELECT Version ,Yr_Varient,"2011" as FY, Period, CoA, Company , Item , Mvt ,Ptnr_Co , Investee , GC*1.1 as GC, LC*1.1 as LC FROM <table Name>
WHERE Mvt = 60200
INSERT INTO _table_
(Version,
Yr_Varient,
FY,
Period,
CoA,
Company,
Item,
Mvt,
Ptnr_Co,
Investee,
GC,
LC)
SELECT
Version,
Yr_Varient,
2011,
Period,
CoA,
Company,
Item,
Mvt,
Ptnr_Co,
Investee,
GC * 1.1,
LC * 1.1
FROM
_table_
WHERE
Mvt = 60200
AND FY <> 2011
This statement should work in any SQL-Database.
Edit: Too slow