Excel VBA質問スレ Part13

このエントリーをはてなブックマークに追加
573デフォルトの名無しさん
インデント消えてた orz
import sys, os
import win32com.client

def main(from_s, from_e, to, rng):
  if from_s[1] != from_e[1]:
    print 'error'
    return
  if rng[0] > rng[1]:
    print 'error'
    return
  pwd = os.path.abspath(os.curdir)
  xls = win32com.client.Dispatch('Excel.Application')
  try:
    wb = xls.Workbooks.Open(os.path.join(pwd, 'test1.xls'))
    sheet = wb.Sheets(1)
    c = 0
    for row in range(65535):
      if row >= from_s[0] and row <= from_e[0]:
        v = float(str(sheet.cells(row, from_s[1])))
        if v >= rng[0] and v <= rng[1]:
          sheet.cells(to[0] + c, to[1]).value = v
          c += 1
    wb.SaveAs(os.path.join(pwd, 'testout.xls'))
    wb.Close()
  except:
    print sys.exc_info()
  finally:
    xls.Quit()

if __name__ == '__main__':
  main((10, 2), (20, 2), (3, 5), (99.0, 1000.0)) # B10-B20 -> E3-