VBS - Copy all cells containing string to another sheet - vbscript

I have searched for over an hour looking for what I think is a simple task.
I have an Excel 2010 workbook with 2 sheets. In the first sheet, I have 1 column (A) with a couple thousand rows. They are arranged like this:
1 Title: aaaaa
2 Author: xxxxx
3
4 Title: bbbbb
5 Author: yyyyy
6
7 Title: ccccc
8 Author: zzzzz
9
10 etc.....
I want to copy the cells into the 2nd sheet so it looks like this in 2 columns:
1 Title: aaaaa Author: xxxxx
2 Title: bbbbb Author: yyyyy
3 Title: ccccc Author: zzzzz
It seems really simple, but I don't know VBS well at all. I need to search for cells that start with "Title" and copy that cell into column A of the other sheet. Then I need to search for cells that start with "Author" and copy that cell into column B of the other sheet.
Thanks,
Jono

To create a custom Text filter, select the filter of your column A, and then select Text Filter > Begins with
Then type "Title: ", but without the quotes. Hit enter and that should filter all your Titles. Copy and paste than, and the do the same for "Author: "

Related

Sort by text date

I have:
14/12 Text 1
23/12 Text 2
08/11 Text 3
08/10 Text 4
(European dates DD/MM)
I'd like to arrange this by date, but is in text format.
Desired output:
08/10 Text 4
08/11 Text 3
14/12 Text 1
23/12 Text 2
Does this do it? I assume you're talking about sorting a range like A2:A?
This formula sorts first by the month, then by the day.
=SORT(A2:A,MID(A2:A,4,2),1,1,1)
Reference:
SORT
MID

How to use two different excel files in same syntax procedure?

I have an excel file with information about variables (excel1) and another one with information about lists (excel2).
In order to create a syntax to generate a new syntax to create VARIABLE and VALUES LABELS, I used solution proposed by #eli.k here.
But with this solution I have to have a dataset with lists so I could use it instead of writing it “by hand” (copy/paste) (here). One problem came with L2, which has 195 entries so the new create variable would need to be bigger that 20.000 characters (is this possible in SPSS?), appearing all in one line.
What I want to know is if it’s possible to use excel2 automatically in code, line by line.
Using the following code:
GET DATA
/TYPE=XLSX
/FILE=" D:\excel1.xlsx "
/SHEET=name 'Folha1'
/CELLRANGE=FULL
/READNAMES=ON
/DATATYPEMIN PERCENTAGE=95.0.
STRING cmd1 cmd2 (a200).
SORT CASES by List.
MATCH FILES /FILE=* /FIRST=first /LAST=last /BY List. /* marking first and last lines.
DO IF first.
COMPUTE cmd1="VARIABLE LABELS".
COMPUTE cmd2="VALUE LABELS".
END IF.
IF not first cmd1=concat(rtrim(cmd1), " "). /* "/" only appears from the second varname.
COMPUTE cmd1=concat(rtrim(cmd1), " ", Var_label).
COMPUTE cmd2=concat(rtrim(cmd2), " ", Var).
DO IF last.
COMPUTE cmd1=concat(rtrim(cmd1), ".").
COMPUTE cmd2=concat(rtrim(cmd2), " ",' 1 "Afghanistan" 2 "Albania" (…) 195 "Zimbabwe".').
END IF.
EXECUTE.
SELECT IF ('List' 'L2').
ADD FILES /file=* /rename cmd1=cmd /file=* /rename cmd2=cmd.
EXECUTE.
I would like to know if there is a way to replace ' 1 "Afghanistan" 2 "Albania" (…) 195 "Zimbabwe".'' by some function/procedure to grab information from excel2 concerning L2, and showing it line by line:
(…)
VARIABLE LABELS V2 "Country"
/ V3 "Country Mother"
/ V4 "Country Father".
VALUE LABELS V2
V3
V4
1 "Afghanistan"
2 "Albania"
(…)
195 "Zimbabwe".
Thanks for helping me!
This issue is pretty complex and would usually be beyond the scope of Stack-Overflow Q&A but here's my answer anyway:
First I recreate the parts of your example data concerning the value labels only:
data list list/var list (2a5).
begin data
"v1" "L1"
"v2" "L2"
"v3" "L2"
"v4" "L2"
end data.
dataset name xl1.
data list list/list (a5) nb (f5) nb_txt (a20).
begin data
"L1" 1 "Female"
"L1" 2 "Male"
"L2" 1 "Afghanistan"
"L2" 2 "Albania"
"L2" 43 "Israel"
"L2" 195 "Zimbabwe"
end data.
dataset name xl2.
data list list/v1 v2 v3 v4 (4f3).
begin data
1 1 2 3
2 2 2 43
1 2 1 195
end data.
dataset name gen.
Now to work:
The first part is to create a macro for each list of variable labels. since some of the lists are long, I use ADD Value labels separately for each value.
dataset activate xl2.
string cmd (a200) cmdFin (a20).
sort cases by list nb.
match files /file=* /by list /first=first /last=last.
compute cmd=concat("add value labels !1 ", string(nb,f6), " '", rtrim(nb_txt), "' .").
if first cmd=concat("define dolist_", list, " (!pos=!cmdend) ", rtrim(cmd)).
if last cmdFin=" !enddefine .".
write outfile="path\create value label macros.sps"/cmd/cmdfin.
exe.
insert file="path\create value label macros.sps".
After inserting the generated syntax a macro has been defined for each of the value lists. Now we create an additional syntax that will run the related macro for each of the variable names in the list:
dataset activate xl1.
string cmd (a200).
compute cmd=concat("dolist_", list, " ", var, " .").
write outfile="path\run value label macros.sps"/cmd.
exe.
Now we can actually try out the generated macros on our original data:
dataset activate gen.
insert file="path\run value label macros.sps".

labelling different lines on split operation

I am using split on 1 of my fields.
It is split into different lines.
If I use .label('something'), then each line has the same name. but I want to give each line a different name.
How to label each line with different name ?
You need to use the regex functionality of .label
Try this:
.label("Ride ID: $1", "^.* > ride_id:(.+) > .*")
The $1 will be replaced by the first argument in the regex, ride_id:(.+) so you should end up your key labels as:
Ride ID: 4
Ride ID: 54
Ride ID: 5
Ride ID: 14
Ride ID: 50

hexadecimal to unique identifier

I have a panel data set where each user is identified by a unique 16-digit hexadecimal userid. When I import this into Stata the userid turns red as Stata does not recognize this format. How can I convert this hexadecimal into a unique numeric identifier that Stata can recognize so that I can do further analysis with the panel?
You should really provide a minimal working example.
I guess you are looking for something like this?
An example would be something like:
clear
input oldid var1 var2
"abc" 1 20
"abc" 2 21
"abc" 4 25
"def" 5 23
"def" 5 21
"hij" 6 27
end
egen newid = group(oldid)

SSIS vbscript for word delimits

I researched delimit issue for a while and I pull useful codes here and there, but I can't quite put it together.
I'm trying to parse the string by word in SSIS and I NEED help on vb script component.
I need to delimit my column data to the following deliminator:
"AND","OR","**", ","
I have a table like this
ID Description
1 apple AND orange, tangerine
2 avocado OR guacamole AND pineapple OR fruit
3 watermelon ** melon
And I want to parse the data like this
ID Description
1 apple
1 orange
1 tangerine
2 avocado
2 guacamole
2 pineapple
2 fruit
3 watermelon
3 melon
Thank you.
In order to parte string by words is enought a combination of replace a split:
(I assume that you know how to take ID)
split(
replace(
replace (
replace( Description, "AND", ","),
"OR", ","
),
"**", ","
), ","
)
this return a array of elements as you ask for:
id = 2
a=my_previous_functions_combination("avocado OR guacamole AND pineapple OR fruit")
for each fruit in a
do something with id and fruit
next
so far, help with vb. I don't know what you want to do in SSIS: a calculated member? a named set? Extend fact table? Read second answer part:
Second part:
To convert a row in multiples row you need a script. You can find a good example in SSIS - Script Component, Split single row to multiple rows post.

Resources