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中运行呀?
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中运行呀?
要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 声明啊,我可没试过!应该差不多!你试试!
Range("A" & i).Select 改为
Range(char(65) & i).select 'char(65)代表A,同理char(66)代表B,依次类推