Copying worksheet programmatically causes run-time error 1004 in Excel

當在一個workbook中不斷複製粘貼裏面的worksheet, 會出現 run-time error的問題. (MS Support).

解决方法: 無.

MS 推薦大家關閉workbook,然後重新打開, 或者更改template.

article clipper remember Copying worksheet programmatically causes run time error 1004 in Excel
 

Access Public Constant from another workbook’s vba

假定要在workbook A中訪問workbook B的MYCONSTANT.

Const MYCONSTANT = 100

在另一個Workbook的ThisWork module中,定義一個Property Get
 
Property Get ConstantValue()

ConstantValue = MYCONSTANT
End Property
 
在另一個Workbook中調用
Sub GetVal()
MsgBox Workbooks(“OtherWorkbook”).ConstantValue
End Sub
 
* 對於class 需要先將class設置爲public.
* 此方法甚至可以返回class, 然而需要workbook b也enable macro地打開
 
Inspired by mrexcel
 
方法2
workbook a的vbe中添加workbook b的reference, 效用不大
article clipper remember Access Public Constant from another workbooks vba
 

How to Organize & Manage Your Files

很多時候,不同project都會大抵需要類似的文件, 例如創建一個website,基本上都要寫js, css等文件. 自己通常喜歡使用下圖所述的組織架構, 把*.js文件都放到js文件夾中, *.css文件都放到css文件夾中.

.

── css

│   └── style.css

── index.html

└── js

└── script.js

 

然而,每次都這麽做的話, 其實是挺浪費時間的. 於是, 可以先把將固定結構的文件夾保存起來, 每次創建新project的時候就直接按此創建一個副本, 再開始Project的工作.

 

以index.html爲例, 每個milestone就保存一個副本, 如:

  • index-v1.html
  • index-v2.html

然後再把這些舊的版本文件拖放到Archive文件夾中, 妥善保管. 在交貨階段, 只需要相應的文件拿出來發給客户. 詳細介紹, 可以參與該網志.

 

example How to Organize & Manage Your Files


folder structure How to Organize & Manage Your Files

article clipper remember How to Organize & Manage Your Files
 

How to create Cool Stickout Photos

image How to create Cool Stickout Photos image 3 How to create Cool Stickout Photos

上圖中狗仔像給禁錮在一個box之中, stickout就是將box壓扁,令到狗仔部份外漏出box之外, 狗仔頓時就躍于紙面. 下面接收如何通過PPT製作類似效果

PPT 2010:

1. 點擊圖片, 格式 – 刪除背景

image 4 How to create Cool Stickout Photos

2. 軟件會自動識別出背景, 假如背景是純色, 軟件會好好地識別出來, 該例中軟件都基本上識別出狗仔, 通過拖拽方框, 狗仔大抵就出來了, 如果要更完美可以通過Ribbon中的兩個按鈕,添加刪除標記的區域.

image 5 How to create Cool Stickout Photos

image 6 How to create Cool Stickout Photos image 7 How to create Cool Stickout Photos

3. 將原圖crop去一半, 再疊加上去,就可以出現stickout的效果

image 8 How to create Cool Stickout Photos

* 更簡單的做法是,不對全圖做去背景的操作,而是將圖片分為上下兩部份,分為兩圖. 只對上面的圖片做去背景.

image 9 How to create Cool Stickout Photos

PPT – 2003

1. 用圖示工具繪製一個任意多邊形

image 10 How to create Cool Stickout Photos

2. 填充繪製的多邊形

image 11 How to create Cool Stickout Photos image 12 How to create Cool Stickout Photos

3. 將多邊形同圖片Group在一起, 剪切 – 選擇性粘貼 – 粘貼為圖片

4. 去背景

 image 13 How to create Cool Stickout Photos image 14 How to create Cool Stickout Photos 

 
article clipper remember How to create Cool Stickout Photos
 

Word as Image

article clipper remember Word as Image
 

Get data from worksheet

從worksheet中cell取數,常見辦法如

with sheet1
    name = .cells(i,1)
    company = .cells(i,2)
end with

此方法直接,但是欠缺機動性,假如日後worksheet有任何改動,令到name不是出現在col 1,全部代碼都需要改.

方法1

將各col在代碼頂部定義為常數, 日後需要修改就在頂部定義中修改.修改地方集中到一處,但是還是麻煩

const nameCol = 1 , companyCol = 2
with sheet1
    name = .cells(i,nameCol)
    company = .cells(i,companyCol)
end with

Private Enum AllCols
  nameCol = 1
  companyCol
End Enum

with sheet1
    name = .cells(i,AllCols.nameCol)
    company = .cells(i,AllCols.companyCol)
end with

方法2

充分利用Table (Excel 2007 or higher).

先在worksheet中定義Table,再在VBA中通過ListObjects訪問

Const nameCol = "name", companyCOl = "company"

set tbl = Sheet1.ListObjects("tableName")
with sheet1
  For each row in tbl.ListRows
    name = row.range(, col(nameCol))
    company = row.range(, col(companyCol))
    'do something
  next row
end with

方法3

利用ADO, 對於Excel2003, 連本workbook的時候慎防內存洩露

本文受該文啓發, 特此鳴謝

article clipper remember Get data from worksheet
 

Every Presentation Ever: Communication FAIL

article clipper remember Every Presentation Ever: Communication FAIL
 

Regular Expressions in JavaScript

1.定義

Javascript中,定義正則表達式的方法有兩個:

1) RegExp Literal

/* /pattern/flags; */
var re = /mac/i;

2) RegExp Object Constructor

/* new RegExp("pattern","flags"); */
var re = new RegExp(window.prompt("Please input a regex.","yes|yeah"),"g");

 

2. Flag

1) Global Search

g     The global search flag makes the RegExp search for a pattern throughout the string, creating an array of all occurrences it can find matching the given pattern.

2) Flags

i     The ignore case flag makes a regular expression case insensitive. For international coders, note that this might not work on extended characters such as ?, ü, ?, ?.

3) Multiline Input

m     This flag makes the beginning of input (^) and end of input ($) codes also catch beginning and end of line respectively.

 

3. Pattern

參閱此處

 

4. 應用

1) RegExp.exec(string)

Applies the RegExp to the given string, and returns the match information.

var match = /s(amp)le/i.exec("Sample text")
//match then contains ["Sample","amp"] 

2) RegExp.test(string)

Tests if the given string matches the Regexp, and returns true if matching, false if not.   

var match = /sample/.test("Sample text")
//match then contains false 

3) String.match(pattern)

Matches given string with the RegExp. With g flag returns an array containing the matches, without g flag returns just the first match or if no match is found returns null.   

var str = "Watch out for the rock!".match(/r?or?/g)
//str then contains ["o","or","ro"] 

4) String.search(pattern)

Matches RegExp with string and returns the index of the beginning of the match if found, -1 if not.   

var ndx = "Watch out for the rock!".search(/for/)
//ndx then contains 10 

5) String.replace(pattern,string)

Replaces matches with the given string, and returns the edited string.   

var str = "Liorean said: My name is Liorean!".replace(/Liorean/g,'Big Fat Dork')
//str then contains "Big Fat Dork said: My name is Big Fat Dork!" 

6) String.split(pattern)

Cuts a string into an array, making cuts at matches.   

var str = "I am confused".split(/\s/g)
//str then contains ["I","am","confused"]

 

Example:

var s='<div style="text-align:center;width:inherit;text-color:blue;">SOME TEXT</div>'
var rx=new RegExp("<div .*?>(.*?)</div>","i");
s=s.replace(rx,"$1");

// s="SOME TEXT"
// http://www.tek-tips.com/viewthread.cfm?qid=1231301

article clipper remember Regular Expressions in JavaScript
 

Hide right column in SharePoint Home page

SharePoint中Team Site的主頁, 默認會給創建為按7:3的比例左右分割. 這是一個煩人的問題, 因為很多時候我們並不想以這個比例劃分左右Column.

解決方法:

1. SharePoint Designer

  1. 用SharePoint Designer打開default.aspx
  2. 在Design View中, 點擊右邊webpart 區域, 選擇Menu – Delete Columns from the table. 并改變左邊column的寬度到100%.
  3. 保存,退出

Tip: You can just as easily insert new rows and columns and then add new web part zones from the Insert, SharePoint Controls menu

2. Javascript

<script>
//http://techtrainingnotes.blogspot.com/2008/11/sharepoint-how-to-hide-right-web-part.html
function HideWebPartZone()
{
  var x = document.getElementsByTagName("TD")
  var i=0;
  for (i=0;i<x.length;i++)
  {
    if (x[i].width=="70%")
    {
      // left column
      x[i].style.width="100%"; 

      // center (otherwise empty) column
      if (document.all) // is IE
        var x2=x[i].nextSibling;
      else
        var x2=x[i].nextSibling.nextSibling;

      x2.style.width="0";
      x2.style.display="none";
      x2.innerHTML=""; 

      // right column
      if (document.all) // is IE
        x2=x[i].nextSibling.nextSibling;
      else
        x2=x[i].nextSibling.nextSibling.nextSibling.nextSibling;

      x2.style.width="0";
      x2.style.display="none";
      x2.innerHTML=""; 

      // right margin column
      if (document.all) // is IE
        x2=x[i].nextSibling.nextSibling.nextSibling;
      else
        x2=x[i].nextSibling.nextSibling.nextSibling.nextSibling.nextSibling.nextSibling;

      x2.style.width="0";
      x2.style.display="none";
      x2.innerHTML="";

      //all done
      return;
    }
  }
}

_spBodyOnLoadFunctionNames.push("HideWebPartZone")
</script>

此方法,在IE7中流暢度欠佳.

用設置Div box的width似乎更理想

3. 直接覆蓋

當創建web part page 之後,需要更改page 的layout (如三欄變為兩欄),可以用同名創建欲改變的layout的web part page 以覆蓋之!

image Hide right column in SharePoint Home page

image 3 Hide right column in SharePoint Home page

image 4 Hide right column in SharePoint Home page

註意:覆蓋會令導致原來頁面中所有內容丟失,請先做好備份! 同時該方法不適用於home page, 因為homepage 異於普通web part page,詳情參見此處

article clipper remember Hide right column in SharePoint Home page
 

SharePoint Cheat sheet

Sharepoint在某些時候,部份的元素實屬多餘,例如Quick Launch Bar等. 要將其去除,最好當然是通過SharePoint Designer. 然而很多時候, 無法通過底層修改SharePoint的模板. 於是,只能求助于CSS.

  1. SharePoint 2007 Cheat Sheet
  2. Cheat Sheet Tool
  3. Style Under Cursor 下載此webpart,并加載到閣下頁面,就可以通過hover的方式,獲取鼠標所指元素的class或者id

下面是Hide Quick Launch Bar代碼

<style>
     .ms-navframe { display: none }
</style>

article clipper remember SharePoint Cheat sheet