Tuesday, February 3, 2009

Some excel macros

I took the time to learn how to write a simple excel macro today that took me about 1 hour to learn but will save me at least 10 hours of work. I'm very proud of myself. Here is my code:

Code to copy from one workbook into another
Sub firstTry()
ActiveCell.Range("A1:B5") = Workbooks("scp_trymacro").Worksheets("sheet1").Range("A1:B5").Value
End Sub

Code to combine 2 cells into 1
Sub firstTry()
X = Workbooks("scp_trymacro").Worksheets("sheet1").Range("A1").Value
Y = Workbooks("scp_trymacro").Worksheets("sheet1").Range("B1").Value
ActiveCell.Range("A1") = X & " - " & Y
End Sub

Code to combine A1 and B1 from one worksheet and write it into another worksheet's column A
Sub firstTry()
For i = 1 To 5
X = Workbooks("scp_trymacro").Worksheets("sheet1").Cells(i, 1).Value
Y = Workbooks("scp_trymacro").Worksheets("sheet1").Cells(i, 2).Value
Cells(i, 1) = X & " - " & Y
Next i
End Sub

No comments: