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

vb怎么修改一个已经打开的excel文件?

现在已经打开一个excel文件。

使用vb怎么样修改此文件。

谢谢。 --------------------编程问答-------------------- Excel.Application
Excel.Application.WorkSheets
Excel.Application.WorkSheets...... --------------------编程问答-------------------- 首先引用Excel
定义:
Dim xlsApp As Excel.Application
Dim xlsWb As Excel.WorkBook
Dim xlsWs As Excel.WorkSheet
接着
Set xlsApp = GetObject("", "Excel.Application") '获得已打开的Excel对象
然后判断xlsApp
If xlsApp Is Nothing Then
'没有对象被打开
Else
'对象已被打开
End If

接着吗~~~~~~~~就跟普通操作一般了 --------------------编程问答-------------------- 请问一下。我已经打开D:\fw.xls

我用secrureCRT运行以下脚本。说第6行GetObject出错。

#$language = "VBScript"   
#$inte易做图ce = "1.0"  

Sub Main
Set   xlsApp   =  GetObject("D:\fw.xls",   "Excel.Application")   
If   xlsApp   Is   Nothing   Then 

'没有对象被打开 

Else 

'对象已被打开 

End   If 
                                                   
end Sub                           

请问一下这个脚本应该怎么写,就是如果打开D:\fw.xls,把它的sheet1.Cells(1, 1).Value = Now  否则打开此文件把它的sheet1.Cells(1, 1).Value = Now
                                                   --------------------编程问答-------------------- http://topic.csdn.net/t/20041212/20/3638464.html

Dim xlsApp     As Excel.Application
Dim xlsWb     As Excel.Workbook
Set xlsApp = GetObject(, "Excel.Application")
If       xlsApp       Is       Nothing       Then   
'没有对象被打开   
Else   
'对象已被打开   
End       If   
Set xlsWb = xlsApp.Workbooks("fw.xls")
If xlsWb Is Nothing Then MsgBox "fw.xls 尚未打开": Exit Sub
xlsWb.Activate --------------------编程问答-------------------- Private Sub CmdExcel_Click()

On Error GoTo ErrorHandle
FileName = App.Path & "\出货明细.xls"
Dim Fso As New FileSystemObject

If CmdExcel.Caption = "Excel输入" Then

'导出到Excel输入
If Fso.FileExists(FileName) Then
   MsgBox "上次输入的出货明细没保存到,请重新保存", vbExclamation + vbOKOnly, "警告"
   oExcel.Workbooks.Open (FileName)
   oExcel.WindowState = xlMaximized
   oExcel.Visible = True
   List1.Text = oExcel.ActiveSheet.Range("M1").Value
   List1.Enabled = False
   CmdA.Enabled = False
   CmdM.Enabled = False
   CmdD.Enabled = False
   CmdAdd.Enabled = False
   CmdModify.Enabled = False
   CmdDelete.Enabled = False
   CmdExport.Enabled = False
   CmdReturn.Enabled = False
   CmdExcel.Caption = "保存"
   Exit Sub
End If

oExcel.Workbooks.Open (App.Path & "\Template\tmp.xls")
oExcel.ActiveWorkbook.SaveAs (FileName)
oExcel.WindowState = xlMaximized
oExcel.Visible = True
'参数设置
Dim i As Integer
sql = "select 合同号 from 出货合同 where 核销标志=0 order by 合同号"
rsContractNo.Close
rsContractNo.Open sql, cn
If rsContractNo.RecordCount > 0 Then
   rsContractNo.MoveFirst
   'i = 2
   oExcel.ActiveSheet.ComboBox1.Clear
   Do While Not rsContractNo.EOF
      oExcel.ActiveSheet.ComboBox1.AddItem (UCase(Trim(rsContractNo("合同号"))))
      rsContractNo.MoveNext
   Loop
   '===================================
   rsContractNo.MoveFirst
End If

sql = "select 组别 from 组别设置  order by 组别"
rsOther.Close
rsOther.Open sql, cn
If rsOther.RecordCount > 0 Then
   rsOther.MoveFirst
   Do While Not rsOther.EOF
      oExcel.ActiveSheet.ComboBox5.AddItem (Trim(rsOther("组别")))
      rsOther.MoveNext
   Loop
End If

'=============================================================================
oExcel.ActiveSheet.Range("M1").Value = Trim(List1.Text)
i = 4
If rsShipmentDetail.RecordCount > 0 Then
   rsShipmentDetail.MoveFirst
   Do While Not rsShipmentDetail.EOF
   oExcel.ActiveSheet.Range("A" & i).Select
   oExcel.ActiveSheet.Range("A" & i).Value = Trim(rsShipmentDetail("合同编号"))
   oExcel.ActiveSheet.Range("B" & i).Value = Trim(rsShipmentDetail("货号"))
   oExcel.ActiveSheet.Range("C" & i).Value = Trim(rsShipmentDetail("制令号"))
   oExcel.ActiveSheet.Range("D" & i).Value = Trim(rsShipmentDetail("颜色"))
   oExcel.ActiveSheet.Range("E" & i).Value = Trim(rsShipmentDetail("尺寸"))
   oExcel.ActiveSheet.Range("F" & i).Value = Trim(rsShipmentDetail("组别"))
   oExcel.ActiveSheet.Range("G" & i).Value = IIf(rsShipmentDetail("m70") = 0, "", rsShipmentDetail("m70"))
   oExcel.ActiveSheet.Range("H" & i).Value = IIf(rsShipmentDetail("l75") = 0, "", rsShipmentDetail("l75"))
   oExcel.ActiveSheet.Range("I" & i).Value = IIf(rsShipmentDetail("xl80") = 0, "", rsShipmentDetail("xl80"))
   oExcel.ActiveSheet.Range("J" & i).Value = IIf(rsShipmentDetail("xxl85") = 0, "", rsShipmentDetail("xxl85"))
   oExcel.ActiveSheet.Range("K" & i).Value = IIf(rsShipmentDetail("free90") = 0, "", rsShipmentDetail("free90"))
   oExcel.ActiveSheet.Range("L" & i).Value = IIf(rsShipmentDetail("free95") = 0, "", rsShipmentDetail("free95"))
   oExcel.ActiveSheet.Range("M" & i).Value = IIf(rsShipmentDetail("free100") = 0, "", rsShipmentDetail("free100"))
   oExcel.ActiveSheet.Range("N" & i).Value = rsShipmentDetail("合计")
   rsShipmentDetail.MoveNext
   i = i + 1
   Loop
rsShipmentDetail.MoveFirst
End If
oExcel.ActiveWorkbook.Save
oExcel.ActiveSheet.Range("A" & i).Select
List1.Enabled = False
CmdA.Enabled = False
CmdM.Enabled = False
CmdD.Enabled = False
CmdAdd.Enabled = False
CmdModify.Enabled = False
CmdDelete.Enabled = False
CmdExport.Enabled = False
CmdReturn.Enabled = False
CmdExcel.Caption = "保存"
Else   '=============================================================================
   sql = "delete from 出货明细 where 出货日期='" & Trim(List1.Text) & "'"
   If rsShipmentDetail.State = adStateOpen Then rsShipmentDetail.Close
   rsShipmentDetail.Open sql, cn
   Call ShowRecord
   i = 4
   Dim iCode As Integer
   Do While Trim(oExcel.ActiveSheet.Range("A" & i).Value) <> ""
      iCode = rsShipmentDetail.RecordCount
      rsShipmentDetail.AddNew
      rsShipmentDetail("序号") = iCode + 1
      rsShipmentDetail("出货日期") = Trim(List1.Text)
      '写入值
      rsShipmentDetail("合同编号") = Trim(oExcel.ActiveSheet.Range("A" & i).Value)
      rsShipmentDetail("货号") = Trim(oExcel.ActiveSheet.Range("B" & i).Value)
      sql = "select 商品名称 from 出货成品 where 合同号='" & UCase(Trim(rsShipmentDetail("合同编号"))) & "' and 货号='" & UCase(Trim(rsShipmentDetail("货号"))) & "'"
      rsOther.Close
      rsOther.Open sql, cn
      rsShipmentDetail("商品名称") = Trim(rsOther("商品名称"))
      rsShipmentDetail("制令号") = Trim(oExcel.ActiveSheet.Range("C" & i).Value)
      rsShipmentDetail("颜色") = Trim(oExcel.ActiveSheet.Range("D" & i).Value)
      rsShipmentDetail("尺寸") = Trim(oExcel.ActiveSheet.Range("E" & i).Value)
      rsShipmentDetail("组别") = Trim(oExcel.ActiveSheet.Range("F" & i).Value)
      rsShipmentDetail("m70") = oExcel.ActiveSheet.Range("G" & i).Value
      rsShipmentDetail("l75") = oExcel.ActiveSheet.Range("H" & i).Value
      rsShipmentDetail("xl80") = oExcel.ActiveSheet.Range("I" & i).Value
      rsShipmentDetail("xxl85") = oExcel.ActiveSheet.Range("J" & i).Value
      rsShipmentDetail("free90") = oExcel.ActiveSheet.Range("K" & i).Value
      rsShipmentDetail("free95") = oExcel.ActiveSheet.Range("L" & i).Value
      rsShipmentDetail("free100") = oExcel.ActiveSheet.Range("M" & i).Value
      rsShipmentDetail("合计") = rsShipmentDetail("m70") + rsShipmentDetail("l75") + rsShipmentDetail("xl80") + rsShipmentDetail("xxl85") + rsShipmentDetail("free90") + rsShipmentDetail("free95") + rsShipmentDetail("free100")
      rsShipmentDetail.Update
      rsShipmentDetail.UpdateBatch
      i = i + 1
   Loop
   Call SortByContract
   oExcel.ActiveWorkbook.Close 1, FileName
   Fso.DeleteFile (FileName)
   List1.Enabled = True
   CmdA.Enabled = True
   CmdM.Enabled = True
   CmdD.Enabled = True
   CmdAdd.Enabled = True
   CmdModify.Enabled = True
   CmdDelete.Enabled = True
   CmdExport.Enabled = True
   CmdReturn.Enabled = True
   CmdExcel.Caption = "Excel输入"
End If
Exit Sub
ErrorHandle:
   MsgBox "Error" & Str$(Err.Number) & vbCrLf & Err.Description
   CmdExcel.Enabled = False
   CmdReturn.Enabled = True
End Sub
补充:VB ,  基础类
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,