|
-
Sep 5th, 2010, 06:44 PM
#1
Thread Starter
New Member
code it better...
as i am a newbie to that field, i have the following code.
I would like to make it shorter and better.
Note that this code copies some cells from one worksheet to another worksheet, only at the last row that is empty.
this code must be repeated for 20 or more times. Range(*90) shows the place for the data for MIG. The range 89 show the place for ATE.
Finally, at the final code "BBB" will become "ccc", "DDD", for each type e.g bbb--> Mig, CCC-->ATE etc. Thats it.
so i am confused with ifs and thens, i tried to make something but not work (obviously). Therefore, i need your help!
Code:
Workbooks.Open Filename:="D:\MARFIN\VASIKA ARXEIA\NAFTEMPORIKI.xls"
Windows("NAFTEMPORIKI.xls").Activate
Sheets("AAA").Select
Range("O90").Select
Selection.Copy
Windows("OLA 20.xlsx").Activate
Sheets("MIG").Select
Range("c65536").End(xlUp).Offset(1, 0).PasteSpecial
Windows("NAFTEMPORIKI.xls").Activate
Sheets("AAA").Select
Range("K90").Select
Selection.Copy
Windows("OLA 20.xlsx").Activate
Sheets("MIG").Select
Range("D65536").End(xlUp).Offset(1, 0).PasteSpecial
Windows("NAFTEMPORIKI.xls").Activate
Sheets("AAA").Select
Range("L90").Select
Selection.Copy
Windows("OLA 20.xlsx").Activate
Sheets("MIG").Select
Range("E65536").End(xlUp).Offset(1, 0).PasteSpecial
Windows("NAFTEMPORIKI.xls").Activate
Sheets("AAA").Select
Range("M90").Select
Selection.Copy
Windows("OLA 20.xlsx").Activate
Sheets("MIG").Select
Range("F65536").End(xlUp).Offset(1, 0).PasteSpecial
Windows("NAFTEMPORIKI.xls").Activate
Sheets("AAA").Select
Range("C90").Select
Selection.Copy
Windows("OLA 20.xlsx").Activate
Sheets("MIG").Select
Range("G5536").End(xlUp).Offset(1, 0).PasteSpecial
Windows("NAFTEMPORIKI.xls").Activate
Sheets("AAA").Select
Range("N90").Select
Selection.Copy
Windows("OLA 20.xlsx").Activate
Sheets("MIG").Select
Range("H5536").End(xlUp).Offset(1, 0).PasteSpecial
Windows("NAFTEMPORIKI.xls").Activate
Sheets("BBB").Select
Range("H212").Select
Selection.Copy
Windows("OLA 20.xlsx").Activate
Sheets("MIG").Select
Range("I65536").End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues
Cells.Replace what:=Chr(44), Replacement:=Chr(46)
End Sub
Last edited by eliasbyvasoula; Sep 6th, 2010 at 02:04 AM.
-
Sep 5th, 2010, 07:11 PM
#2
Re: code it better...
Can you please place [code][/code] tags around your code so that it makes it easier to read. Thank you!
when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
https://get.cryptobrowser.site/30/4111672
-
Sep 6th, 2010, 03:48 AM
#3
Re: code it better...
Welcome to VBForums 
There are a few ways to make the code shorter and better.
The first is to use variables for the workbooks, as it makes the Windows() lines quicker and safer. To do that you would change the Workbooks.Open line to this:
Code:
Dim objBookSource as Workbook, objBookTarget as Workbook
Set objBookSource = Workbooks.Open (Filename:="D:\MARFIN\VASIKA ARXEIA\NAFTEMPORIKI.xls")
Set objBookTarget = WorkBooks("OLA 20.xlsx")
...and add this just before the End Sub:
Code:
Set objBookSource = Nothing
Set objBookTarget = Nothing
You can then change the Windows() lines to these:
Code:
objBookSource.Activate
objBookTarget.Activate
(you can use other names instead of objBookSource and objBookTarget if you like, but they are reasonably short and clear)
The next step is to not Activate or Select anything unless you absolutely need to, as they are slow and unreliable. In terms of lines like these:
Code:
objBookSource.Activate
Sheets("AAA").Select
Range("O90").Select
...you can simply merge the lines, as a Range is a child object of a Sheet, which is a child object of a Workbook. As such, those lines can be shortened to this:
Code:
objBookSource.Sheets("AAA").Range("O90").Select
At this stage you are likely to get an error if you run it, but the next step fixes that.
You can now eliminate the need for Selection entirely by removing .Select from the end of one line, and Selection from the start of the next. So this:
Code:
objBookSource.Sheets("AAA").Range("O90").Select
Selection.Copy
..become this:
Code:
objBookSource.Sheets("AAA").Range("O90").Copy
If you do this for all of your code, each block of 7 lines you've got will be reduced to just 2 lines. Have a go at that and show us what you end up with, as we may be able to improve it even further.
-
Sep 6th, 2010, 03:21 PM
#4
Thread Starter
New Member
Re: code it better...
I have made some changes in order to make it shorter. And this is the result... i will try your suggestions.
However, i found to have a problem with that line.
i would like to RePLACE all data inside the same workbook BUT this cxommand replaces only data that exists in the active sheet. As i went through web, there is not an easy right-way solution. right;
Code:
Cells.Replace what:=Chr(44), Replacement:=Chr(46)
Code:
Sub S3()
Workbooks.Open Filename:="D:\MARFIN\VASIKA ARXEIA\NAFTEMPORIKI.xls"
Windows("NAFT.xls").Activate
Sheets("AAA").Select
Cells.Find(what:="MRFr.AT", After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
Range("S90:Y90").Select
Selection.Copy
Windows("OLA 20.xlsx").Activate
Sheets("MIG").Select
Range("c65536").End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues
Cells.Replace what:=Chr(44), Replacement:=Chr(46)
End Sub
Last edited by eliasbyvasoula; Sep 6th, 2010 at 03:25 PM.
-
Sep 6th, 2010, 04:26 PM
#5
Re: code it better...
Just like Range, Cells is a child object of a sheet.
If you don't specify a parent object (in this case a sheet) then Excel will assume that you want whichever is Active/Selected at the time.
In order to do something on each sheet, you will need to use a loop for the Workbook.Sheets collection, eg:
Code:
Dim objSheet as WorkSheet
For Each objSheet In objBookSource.Sheets
MsgBox objSheet.Name
Next
-
Sep 7th, 2010, 05:18 AM
#6
Thread Starter
New Member
Re: code it better...
Thanxs, i am trying to do it!
However, now, i have a serious problem!
I use a web query, a very simple one e.g.
Code:
1
http://www.naftemporiki.gr/markets/excel_intraquote.asp?id=["id","Doste to symbolo poy thelete px NAYr.AT : "]
I use this to an excel sheet. each time that is opened is renewed. It is giving intraday quotes. I am multiplying stocks buyed*price to find the VALUE.
Note that, the number of ROWS that will be filled by the web query are not standards (e.g. one time 100, another time 320)
HOWEVER, i have that problem. the prices are taken in the simplest way:
Code:
A B C
PRICE VOL =A1*B1
1.01 120 =A2*B2
1.05 2300 =A3*B3
1.02 1300 =A4*B4
.
.
.
.
.
Values are given in a very nice way, however, when i close the excel file, and i am opening again, i renew the web query. Then, abnormality is appearring:
Code:
A B C
PRICE VOL =A1*B1
1.01 120 =A2*B2
1.05 2300 =A3*B3
1.02 1300 =A100*B100
1.04 300 =A101*B101
1.03 340 =A102*B102
1.07 500 =A103*B103
.
.
.
.
i have understood (i think!) that the values are taken until the point that was taken before (e.g. number rows filled 120) but the next tme, if they are less than before (e.g. number rows filled 75) they will be given normally until the ROW 75 and the next VALUe will be calculated from the ROW e.g. 121!!! i thing after the point that the table taken from web query is ending.
I tried many things but nothing solve that problem! I cannot calculate the Value!!!! .
The only solution that i found i s to copy the calculation formula over and over using a macrocommand (over 20 sheets! and per day!!!)
thanks in advance!
Last edited by eliasbyvasoula; Sep 7th, 2010 at 05:30 AM.
-
Sep 7th, 2010, 07:41 AM
#7
Re: code it better...
That is a completely different question to the one at the start of this thread, so it does not belong here - you should create a new thread for that question (in our Office Development forum).
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|