当前位置:编程学习 > VB >>

如何在EXCEL中运行此宏文件?

Sub pipeindex()
     Open "d:\PIPEINDEX.TXT" For Input As #1
     Line Input #1, s
     Line Input #1, s
     While Not EOF(1)
     Line Input #1, s
     If Trim(s) <> "" Then
     a = Left(s, 50)
     b = Right(s, 50)
     a = Trim(a)
     b = Trim(b)
    c = Len(a) - 1
    a = Right(a, c)
    
    Selection.TypeText Text:=a
    Selection.MoveRight Unit:=wdCell
    Selection.MoveRight Unit:=wdCell
    Selection.MoveRight Unit:=wdCell
    Selection.TypeText Text:=b
    Selection.MoveRight Unit:=wdCell
    Selection.MoveRight Unit:=wdCell
    Selection.MoveRight Unit:=wdCell
    Selection.MoveRight Unit:=wdCell
 
     End If
     Wend
     Close #1
End Sub

此代码能在WORD中运行,可在EXCEL中Selection.TypeText Text:=a这句总出错
如何修改才能在EXCEL中运行呀?
--------------------编程问答-------------------- 上面的那些东西有些都是WD里特有的参数,你可以这样:
要EXCEL里,不是用selection而是CELL
要在B5单元格输入123可以这样:
Range("B5").Select
ActiveCell.FormulaR1C1 = "123"
我认为可以这样改:
dim i as integer
i=1
Sub   pipeindex() 
   Open   "d:\PIPEINDEX.TXT"   For   Input   As   #1 
   Line   Input   #1,   s 
   Line   Input   #1,   s 
   While   Not   EOF(1) 
   Line   Input   #1,   s 
   If   Trim(s)   <>   ""   Then 
   a=Left(s,   50) 
   b=Right(s,   50) 
   a=Trim(a) 
   b=Trim(b) 
   c=Len(a)-1 
   a=Right(a,c) 

   Range("A" & i).Select     ‘第一次运行时是A1
  ActiveCell.FormulaR1C1 = a
  Range("A" & i+2).Select     ’然后是A3
  i=i+2                           '下次是A5
  End If 
      Wend 
      Close   #1 
End   Sub 

声明啊,我可没试过!应该差不多!你试试!


--------------------编程问答-------------------- 谢谢!!如何在列之间自动输入呀?从A到AZ --------------------编程问答-------------------- 也一样,
Range("A"   &   i).Select     改为
Range(char(65) & i).select  'char(65)代表A,同理char(66)代表B,依次类推
补充:VB ,  基础类
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,