Save as syntax and range.select index syntax? - arrays

I'm trying to write some lines of codes that select a range in excel but the index to the range has a syntax error any suggestions? Also I'm trying to save a XML file as an xlsm file with file names from the concactenation of two file names stored in an array and is getting a similar error.any suggestions??
Range (Allfiles(index)).select 'Allfiles is an array containing the file names ' type error
Activeworkbooks.saveas "c:\Allfiles(1):&:Allfiles (count).xlsm", fileformat=52 'error

When you get an "error", a useful "error message" will give you a hint as to what the problem is. By reading this message and acting upon it, you can debug your code step by step.
Activeworkbooks.SaveAs "c:\Allfiles(1):&:Allfiles (count).xlsm", FileFormat = 52
' Variable not defined ^
The way to specify arguments in VBA is with :=, not =. Let's correct that and run it again...
Activeworkbooks.SaveAs "c:\Allfiles(1):&:Allfiles (count).xlsm", FileFormat:=52
' ^ Variable not defined
It's called ActiveWorkbook, not Activeworkbooks. Let's correct that and run it once more...
ActiveWorkbook.SaveAs "c:\Allfiles(1):&:Allfiles (count).xlsm", FileFormat:=52
' ^ The file could not be accessed.
I can't claim to know for sure what OS you're running, but given the C:\, I'll assume some flavour of Windows. You may know that : is an illegal character in paths in Windows?
Anyhow, I'm not sure where you're trying to save this file. My best guess:
ActiveWorkbook.SaveAs "C:\" & Allfiles(1) & Allfiles(UBound(Allfiles)) & ".xlsm", _
FileFormat:=52
As for you first line of code, Range (Allfiles(index)).Select, I have no clue what you're trying to do there. You may want to read Excel-VBA's help file to learn what Range does.

Related

error in looping over files, -fs- command

I'm trying to split some datasets in two parts, running a loop over files like this:
cd C:\Users\Macrina\Documents\exports
qui fs *
foreach f in `r(files)' {
use `r(files)'
keep id adv*
save adv_spa*.dta
clear
use `r(files)'
drop adv*
save fin_spa*.dta
}
I don't know whether what is inside the loop is correctly written but the point is that I get the error:
invalid '"e2.dta'
where e2.dta is the second file in the folder. Does this message refer to the loop or maybe what is inside the loop? Where is the mistake?
You want lines like
use "`f'"
not
use `r(files)'
given that fs (installed from SSC, as you should explain) returns r(files) as a list of all the files whereas you want to use each one in turn (not all at once).
The error message was informative: use is puzzled by the second filename it sees (as only one filename makes sense). The other filenames are ignored: use fails as soon as something is evidently wrong.
Incidentally, note that putting "" around filenames remains essential if any includes spaces.

Opening Japanese-named files in Lua

I have bunch of XML files named in Japanese. I use Lua to read them and put the necessary informations into tables. I could open files named only in a single kanji like 名.xml, but for multiple kanjis like 名前.xml it was contrawise. Before I ran the Lua file, I set the command line's code page to 65001 (as UTF-8). And to read the files I need to encode the filename using WinAPI library from ACP (ASCII code page?) to UTF-8, but this encoding only works for the single kanjis. I've tried several suggestions across internet, using short path to the file, etc. but none of them worked. I tried to use the short path by running Lua as administrator--as stated in other similar question that you need administrator previleges to use the short path--but no luck.
...
for fn in io.popen("DIR xml /B /AA"):lines() do
...
local f = assert(io.open("xml\\" .. winapi.encode(winapi.CP_UTF8, winapi.CP_ACP, fn), "rb"))
...
end
...
But my code produced "Invalid argument" error. I searched this error but none of them are Lua-related, so I opened the C/C++-related ones, but what I got was only 'use _wfopen' or something like that. It's not implemented in Lua and neither I want to implement it myself. So anyone have any idea how to solve this? For more information just be sure to let me know. Thanks!
I don't know why your program does not work, but try this workaround:
local pipe = io.popen([[for %G in (xml\*) do #(type "%G" & echo #FILENAMEMARKER#%G)]], "rb")
local all_files = pipe:read"*a"
pipe:close()
for filecontent, filename in all_files:gmatch"(.-)#FILENAMEMARKER#(.-)\r?\n" do
-- process your file here
print('===== This is your file name:')
print(filename)
print('== This is your file content:')
print(filecontent)
print('== End of file')
end
I think you can use the Japanese alphabet in a table like
local jaAlphbet={"一","|","丶","ノ","乙","亅","<","二","亠","人","⺅","𠆢","儿","入","ハ","丷","冂","冖","冫","几","凵","刀","⺉","力","勹","匕","匚","十","卜","卩","厂","厶","又","マ","九","ユ","乃","𠂉","⻌","口","囗","土","士","夂","夕","大","女","子","宀","寸","小","⺌","尢","尸","屮","山","川","巛","工","已","巾","干","幺","广","廴,"廾","弋","弓","ヨ","彑","彡","彳","⺖","⺘","⺡","⺨","⺾","⻏","⻖","也","亡","及","久","⺹","心","戈","戸","手","支","攵","文","斗","斤","方","无","日","曰","月","木","欠","止","歹","殳","比","毛","氏","气","水","火","⺣","爪","父","爻","爿","片","牛","犬","⺭","王","元","井","勿","尤","五","屯","巴","毋","玄","瓦","甘","生","用","田","疋","疒","癶","白","皮","皿","目","矛","矢","石","示","禸","禾","穴","立","⻂","世","巨","冊","母","⺲","牙","瓜","竹","米","糸","缶","羊","羽","而","耒","耳","聿","肉","自","至","臼","舌","舟","艮","色","虍","虫","血","行","衣","西","臣","見","角","言","谷","豆","豕","豸","貝","赤","走","足","身","車","辛","辰","酉","釆","里","舛","麦","金","長","門","隶","隹","雨","青","非","奄","岡","免","斉","面","革","韭","音","頁","風","飛","食","首","香","品","馬","骨","高","髟","鬥","鬯","鬲","鬼","竜","韋","魚","鳥","鹵","鹿","麻","亀","啇","黄","黒","黍","黹","無","歯","黽","鼎","鼓","鼠","鼻","齊","龠"}
print(jaAlphbet[1])--and you can call the letters, letter by letter
sorry but thats all i know about the subject you are talking about but i hope this helps

Mathematica exportation faeled

Hello i'm new of this forum and new user of mathematica.
I used this sintax
Export[PathToSave <> "Frq-" <> NameToSave <> ".xls", frq]
in two different file but in one case it works and export my xls file, in another case i have this error:
StringJoin::string: String expected at position 2 in C:\.......-<>NameToSave<>.xls. >>
StringJoin::string: String expected at position 2 in C:\.......-<>NameToSave<>.xls. >>
Export::chtype: First argument C:\.......-<>NameToSave<>.xls is not a valid file specification. >>
dots are instead of my path.
I checked so many times the path to the directory and files but the error doesn't disappear.
Could somebody help me?
thanks.
I had a similar problem. Check what type of atomic expression is NameToSave. For example if NameToSave is an Integer you should convert it to a string using ToSting[NameToSave].
You can use Head[NameToSave] to check what type of atomic expression is NameToSave.

Inserting special ascii characters into a file using VBA (excel)

I am fairly new to VBA, but not to coding in general. I have an Excel worksheet, part of which I need to export into a text file. However, to ensure the escape characters don't collide with anything written in the worksheet, and also to match up with how escape characters are handled in other (Java & C) apps at our company, I would like to use the ascii character "thorn".
Some languages that I know, like perl, will simply let you enter an escape character and the ascii number of the symbol. E.g., I could type "\231" in order to get "thorn". It seems this is not supported in VBA. Or if it is, I am not sure how.
Lastly, below is a brief snippet of how I imagine I would implement the code, with just the key statements. If you have a better suggestion, or know why this wouldn't be the best way, please let me know.
Open sFullPath For Output As #1
(... some code to capture the values to be written to the next line of the file ...)
sLine = Join(sValues, <the thorn character>)
Print #1, sLine
The code above would be placed within a loop that would regenerate "sValues", the set of relevant cells in the next row to be printed to the text file.
Thanks so much!
Regards,
Mike
To print an ASCII character using its code number, use the chr() function
Extended ASCII isn't standard across all platforms. For whatever reason, the number needed is 222.
So you would need
sLine = Join(sValues, chr(222))
Print #1, sLine
Try replacing <the thorn character> with Chr(254). Let me know how that goes.

PostgreSQL: Creating a .sql file to insert data into a table the fastest way

I'm working on a project where I have to parse a bunch of .csv files, all of different formats and containing different kinds of data through some C++ functions. After that I extract data from the files and create a .sql file that can be imported in psql to insert the data into a PostgreSQL database at a later stage.
But I am not able to figure out the correct syntax for the .sql file. Here is a sample table and a sample .sql file reproducing the same errors I am getting:
Table Creation Code:
CREATE TABLE "Sample_Table"
(
"Col_ID" integer NOT NULL,
"Col_Message" character varying(50),
CONSTRAINT "Sample_Table_pkey" PRIMARY KEY ("Col_ID" )
)
insertion.sql (after the copy line, fields separated by a single tab character)
copy Sample_Table (Col_ID, Col_Message) from stdin;
1 This is Spaaarta
2 Why So Serious
3 Baazinga
\.
Now if I execute the above sql file, I get the following error:
ERROR: syntax error at or near "1"
LINE 2: 1 This is Spaaarta
^
********** Error **********
If it can help, I'm running a PostgreSQL 9.1 release, and all the above queries were executed through PGAdmin III Software.
PgAdmin doesn't support executing COPY commands in the same way that psql does (or at least, it didn't the last time I tried it with version 1.14). Use psql to execute the script, or use INSERT statements.
Three things to check:
Is there actually exactly one tab characters between the columns? Spaces are a no-go.
Are there more error messages? I'm missing at least one. (See below)
When you force case sensitive table and column names you have to do this consequently. Therefore you must write this:
copy "Sample_Table" ("Col_ID", "Col_Message") from stdin;
Otherwise you will get theese errors:
psql:x.sql:1: ERROR: relation "sample_table" does not exist
psql:x.sql:5: invalid command \.
psql:x.sql:5: ERROR: syntax error at or near "1"
LINE 1: 1 This is Spaaarta
^
With these things in place I can use your example data successfully.
EDIT Bug change: The questioner now has
ERROR: invalid input syntax for integer: "1 'This is Spaaarta'"
So something with the 1 is not OK.
My guess is, that this is an encoding problem. Windows with it's UTF-16 stuff might be the culprit here.
Debugging these kind of problems other the web is not easy, because to many semi-intelligent programs are in the line, most of them like to adjust "a few" things.
But first check a few things in psql:
\encoding
show client_encoding;
show server_encoding;
According to the pastebin data these should be the same and one of "SQL_ASCII", "LATIN1" or "UTF-8".
If they already are or if adjusting them does not help: Unix/Linux/cygwin has a hexdump -C x.sql program, post its output to pastebin. DO NOT USE the hexdump from any Windows editor like ultraedit - they have fooled me several times. When transferring the file to Linux be sure to use binary transfer.

Resources