Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Creating a Simple macro
#1
I am new to this site and very new to Macro or programming..

Very simple quesetion !! I need to create a macro to copy data from any column and paste it to different column

Data could be in Column A B or any column and paste it to any other column, please assist.

Another question...lets say if column "B" has different data, do i need to highlight this data when i run the macro or just run the macro.

Any help would be apperciated.

Thanks
-Raaz
#2
Will need more information.
For example: Are you talking columns in Excell?

Be very detailed about what you are trying to accomplish with as many explanations as possible.

There are a lot of things Quick Macros can do.

Jimmy Vig
#3
Vig is right

Yhis works but may not be what you want

int w1=act(win("Microsoft Excel - Book1" "XLMAIN"))
lef 64 153 w1
rig 65 153 w1
int w2=win("Cell" "MsoCommandBarPopup")
lef 44 40 w2
act w1
lef 126 154 w1
rig 128 152 w1
lef 71 60 w2

Another question -
Just run the macro
#4
Time_Runner is all based off mouse clicks and might run differently based on column widths and other factors.

There are ways to get and set data from excell, you can even choose to run with excell hidden which is pretty neat.

You can get data and determine if (not)blank then run an if statement from there.

Or...maybe the task that is being performed doesn't need excell at all.

There's a lot of ways to go.
#5
This simply copies data from one column to another. It will replace any data in the column and will not delete the source data. Don't know if this what you are looking for, but it should get you started.

Excell has to be closed for this to run. Gintaras, do you have any way to get it working with Excell open?

This might be useful too:
range examples: "" (all), "sel" (selection), "A1:C3" (range), "A:C" (columns A, B and C), "3:3" (row 3), "A1" (cell), "Named" (named range)

Here is the code with notes as best as I understand it all:
Function Function6
Code:
Copy      Help
;;All of this will run hidden...
;;so you won't see a thing happen
;;except for outputting variables in QM.
;;Declares the filepath for Excel file to use
str sfile="$desktop$\Test.xls"
;;Declares the name of the sheet to use
;;Make sure the name of the sheet is correct!!
str sheet="Sheet1"
;;Works the Magic...
Excel.Application app._create
Excel.Workbook book=app.Workbooks.Open(_s.expandpath(sfile))
ExcelSheet es.ws=book.Worksheets.Item(sheet)
;;Declare the variable for the ARRAY you want for data in QM
ARRAY(str) a
;;Get Column A
es.GetCells(a "A:A")
;;Breaks apart data in ARRAY a
int r c
for r 0 a.len
,for c 0 a.len(1)
,,;;This creates a new variable for every peice of data
,,;;a[c r] could be something like a[1 20] which would be the a in column A row 20
,,;;r is indexed to "0" this resets the index to 1
,,r+1
,,;;Set data to new column "2" here is column "B"
,,;;which is the 2nd column in Excel.  
,,;;"3" would be column "C" and so on...
,,es.SetCell(a[c r] 2 r)
;;Saves the Excel sheet
book.Save
;;Closes the Excel
book.Close

Here is the simplified code without notes:
Function Function6
Code:
Copy      Help
str sfile="$desktop$\Test.xls"
str sheet="Sheet1"
Excel.Application app._create
Excel.Workbook book=app.Workbooks.Open(_s.expandpath(sfile))
ExcelSheet es.ws=book.Worksheets.Item(sheet)
ARRAY(str) a
es.GetCells(a "A:A")
int r c
for r 0 a.len
,for c 0 a.len(1)
,,r+1
,,es.SetCell(a[c r] 2 r)
book.Save
book.Close

Not too scary.

Jimmy Vig
#6
Error (RT) in "Function6 /98"oFunction6: 0x800A03EC,

Do you know what this error is?

I have Excel 2002; Does that matter
#7
I get this error when the file is open in Excel. Then book.Save fails because the file is locked by Excel.
#8
Thank you for your prompt rersponse. Okay this is what i need

Lets say column E has some values and it does not matter if column B has values or not, what i need to do is move values from column E to column B, again once i hvae this macro for E and B in future if i need to move data from any column to any column i can change column name right?

B E
----------- --------------------------------
2 3
3 43
33 332
334 343

Please assist. Also, please advise if i need to highlight values in column E when i run the macro?

Thank you,
--Raaz
#9
In Excel you can record Excel macro. I tried to record and this is the macro:

Sub Macro1()
'
' Macro1 Macro
' Macro recorded 2008.08.15 by G
'
' Keyboard Shortcut: Ctrl+k
'
Columns("E:E").Select
Selection.Cut
Columns("B:B").Select
ActiveSheet.Paste
End Sub

Now convert to QM:
Macro
Code:
Copy      Help
;/exe 1
ExcelSheet es.Init
IDispatch a=es.ws.Application
a.Columns("E:E").Select
a.Selection.Cut
a.Columns("B:B").Select
a.ActiveSheet.Paste


Forum Jump:


Users browsing this thread: 6 Guest(s)