Macro Code to import data - excel

How do I import from one Excel file to another using VBA?
From Req List test.xlsx Columns A B K O P L M N U V
to Req List test day count summary.xlsx Columns A B C D E F G I K L
Both files are on the same path:
C:\Users\msiddique\Desktop
The range for the rows should be unlimited, as the master file is updated daily and more rows get added.

Windows("van baan naar AX Nederland.xlsx").Activate
Cells.Select
Selection.Copy
Windows("van baan naar AX Nederland met Macro's.xlsm").Activate
Cells.Select
ActiveSheet.Paste
if you put it in the same window its pretty easy, you just have to open both files.

Related

Filter and select random rows

I am not familiar with pandas and openpyxl package in Python but was just wondering if it is possible to select random, filtered rows from an excel file and save the output in a separate excel file ?
For example:
The data below represents what is in my excel file and what I would like to do is that:
A B C D E F G
1 x -8 x x x 10/12/2019
3 x +9 x x x 28/12/2019
4 x +2 x x x 12/12/2019
1 x -2 x x x 15/12/2019
4 x -9 x x x 19/12/2019
5 x -1 x x x 25/12/2019
Filter to only negative values in column C ("-ve")
Filter to only items (Column G) dated from 15/12/2019 onwards
Randomly select samples (for example, 2) from this filtered list
Save the 2 randomly selected samples in a new excel file, with the headers (A,B,C,D,E,F,G)
Is there any way i could do this ?
Potentially, I will be using the script for well over 20 times.
Thank you !
You can use:
from numpy.random import choice
df['G']=pd.to_datetime(df['G'])
#ensure that datelike column is of type datetime
out=df.loc[df['C'].le(-1) & df['G'].ge(pd.to_datetime('15/12/2019'))]
#Check your 1st and 2nd condition
out.loc[choice(out.index,2)].to_excel('filename.xlsx',index=False)
#get random 2 values from the filtered rows and save it to excel

excel formula help on 2 sheets

i have a excel workbook with these two sheets in it. I am trying to sepearate product by Type into separate columns as shown on sheet 2. Any help how to do this in in excel, by reading sheet 1 and getting results into sheet 2. Thanks in Advance !!
SHEET 1
product price type
A $10.98 PD
B $54.89 SLP
C $74.99 PD
D $82.99 PD
E $54.76 SLP
F
G $76.99 PD
SHEET 2
Product PD SLP
A $10.98
B $54.89
C $74.99
D $82.99
E $54.76
F
G $76.99
I have put everything into one table for better view:
both tables next to each other
In column G i think I have written:
=If($G$1=Table1[#type];INDEX(Table1[[Product]:[price]];Match(F2;Table1[Product]);2);"")
Original in German: =WENN($G$1=Tabelle1[#type];INDEX(Tabelle1[[Product]:[price]];FINDEN(F2;Tabelle1[Product]);2);"")

Finding top 5% in Excel

I have a list of data in Excel, with values attributed to the different samples. I would like to subset the top 5% from all my data. How can I do this in Excel?
sample value
a 0.6001437980
b 0.0983224370
c 0.0493093160
d 0.0427906350
e 0.0413478790
f 0.0299204810
g 0.0259600660
h 0.0215505810
i 0.0167398000
j 0.0131496290
k 0.0105364240
l 0.0082647980
m 0.0068507060
n 0.0065234580
o 0.0050233730
In cell C2, enter
=B2>=PERCENTILE($B$2:$B$63,0.95)
you can then copy this to C3:C63.
Column C now shows TRUE only for those rows with a B value in the top 5%.
Additionally you may like to apply a filter.
You can specifie rang of your data and then color it with very little effort.
Here is an example, where you can color top N records:
Hope it helps :)

Order rows chronologically by using Time Value provided in a column - Do this for many dates

I have a sheet which has Dates as first column and time as second column. Then in other columns more details (which are not part of the problem).
The problem is on a given date there are "n" number of rows (each with same date in 1st column). BUT, the time is not chronological.
Say, on 7th Jan there are 4 rows of data with times such as
7-jan-2016 14:25:33 x y z
7-jan-2016 10:43:51 v t s
7-jan-2016 13:01:02 h m p
7-jan-2016 12:48:15 l p l
9-jan-2016 problem same as above
I need to rearrange the rows chronologically FOR EACH DATE. Such that above looks like this:
7-jan-2016 10:43:51 v t s
7-jan-2016 12:48:15 l p l
7-jan-2016 13:01:02 h m p
7-jan-2016 14:25:33 x y z
9-jan-2016 no more problems.. and as above..
How can I achieve this without manually cut-pasting rows that are in 1000's.
Sort by the first column, then select Add Level and sort by the second column.

How to get pack all the nonnull data from a row of null and non-null data into an array using vbscript

I have an excel sheet,which has 144 coulms-(4 columns contains one set).i.e 36 sets i do have. Now how can i take out only the Non-Null data from the 2000X350 Excel sheet matrix data,into an 2D array using VBScript?
Here is an Example sheet:
PID T1 T1SD T1CD T1ST T2 T2SD T2CD T2ST T3 T3SD T3CD T3ST T4 T4SD T4CD T4ST ........
10 a b b t r t k l o
11 p p m d n n n b
.
.
PID on the column number 11 always.Evey set is comprised of (TN,TNSD,TNCD,TNST) where N=1 to 36. Array will not pick up data into it only iff a set full contains NUll data . Once all the data pick up will be done,it should then relase the data to each row. But thing should be remembered that if 2D array should assign data to the row from where it has been picked up.
Data(1,1)=(a,b,,b,t,,,r,t,k,l,o) During pick up
Cell(2,12)=(a,b,,b,t,,,r,t,k,l,o) when releasing the data.
that means data should need to mapped to the correct rows(setwise)
Please let me know if you have any confusion.
EDIT:
Output table
PID T1 T1SD T1CD T1ST T2 T2SD T2CD T2ST T3 T3SD T3CD T3ST T4 T4SD T4CD T4ST
10 a b b t r t k l o
11 p p m d n n n b
Thanks,Arup
Here is a partial answer that should get you a nudge in the right direction.
Sub Macro1()
Dim whichT As Integer
Dim whichC As Integer
Dim allNull As Boolean
Dim contents As String
For whichT = 0 to 8 ' this is which T set
allNull = True
for whichC = 1 to 4 ' this is which of the 4 elements
contents = Cells(2, whichT * 4 + whichC + 1)
Debug.Print "Contents of col ", whichT * 4 + whichC + 1, "are", contents
If Len(contents) > 0 then allNull = False ' any one of the non-blank elements sets to False
Next whichC
If allNull Then ... ' do some processing to move over the next 4.
Next whichT
End Sub

Resources