@ni Excel读取和写入

这段代码使用VBA在Excel中读取A1到P24的数据,进行乘以20的操作后,将结果存储到新的工作表中,从F1开始。函数gfncolTokenize用于拆分字符串。最终,结果保存到新的Excel文件BBB.xlsx中。
摘要由CSDN通过智能技术生成

Option Explicit

Private Sub Command1_Click()
Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
Dim sStartX As String
Dim sStartY As String
Dim sStartXCnt As Long
Dim sStartYCnt As Long
Dim sEndX As String
Dim sEndY As String
Dim sEndXCnt As Long
Dim sEndYCnt As Long
Dim colValueList As Collection
Dim sPreName As String
Dim colPreNameList As Collection
Dim sCatchValue As String
Dim iTargetY As Long
Dim sTargetX As String
Dim sTargetPoint    As String

Dim a1 As String


 Call IsExcelInstalled

' 创建 Excel 应用程序对象
Set xlApp = New Excel.Application

' 打开 Excel 文件
Set xlBook = xlApp.Workbooks.Open("C:\Documents and Settings\JE06387\桌面\QTJTEST\AAA.xlsx")

' 获取第一个工作表
Set xlSheet = xlBook.Sheets(1)

' 读取 A1 单元格的值
MsgBox xlSheet.Range("A1").Value

Dim sStartPoint As Collection
Dim sEndPoint As Collection


sPreName = "A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P"
sEndYCnt = 24

Set sStartPoint = gfncolTokenize(Text1.Text, ",")
Set sEndPoint = gfncolTokenize(Text2.Text, ",")

Set colPreNameList = gfncolTokenize(sPreName, ",")
'获取A1,B1,C1到P24的值,然后放入List中
For sStartYCnt = 1 To sEndYCnt
    For sStartXCnt = 1 To colPreNameList.Count
        
            sCatchValue = colPreNameList.Item(sStartYCnt) & sStartXCnt
            
            a1 = xlSheet.Range(sCatchValue).Value
            
            a1 = a1 * 20
            
            
            colValueList.Add sCatchValue, a1
            
    Next sStartXCnt
Next sStartYCnt


Dim xlWorkbook As Excel.Workbook
Dim xlWorksheet As Excel.Worksheet

' 创建 Excel 应用程序对象
Set xlApp = New Excel.Application

' 打开 Excel 文件
Set xlWorkbook = xlApp.Workbooks.Add()
' 选择工作表
Set xlWorksheet = xlWorkbook.Worksheets("Sheet1")

sTargetX = "F"
'向文件中从F1开始输入
For iTargetY = 1 To colValueList.Count
    
    sTargetPoint = sTargetX & iTargetY
    
    xlWorksheet.Range(sTargetPoint).Value = colValueList.Item(iTargetY)


Next iTargetY

' 重命名工作簿
xlWorkbook.SaveAs ("C:\Documents and Settings\JE06387\桌面\QTJTEST\QTJTEST2\BBB.xlsx")

' 保存并关闭 Excel 文件
xlWorkbook.Save
xlWorkbook.Close
xlApp.Quit

' 释放对象
Set xlWorksheet = Nothing
Set xlWorkbook = Nothing
Set xlApp = Nothing


' 关闭 Excel 文件和应用程序
xlBook.Close False
xlApp.Quit

' 释放对象引用
Set xlSheet = Nothing
Set xlBook = Nothing
Set xlApp = Nothing
End Sub


Public Function gfncolTokenize(ByVal psFullStr As String, ByVal psDelim As String, Optional ByVal psSuppleLastNull As Boolean = False) As Collection
    
    Dim iPos As Integer
    Dim sStr As String
    Dim colCollection As New Collection
    Dim sToken As String
    
    '
    '   parse (and modify) a copy of the passed string
    '
    sStr = psFullStr
    
    iPos = InStr(1, sStr, psDelim, 1)
    Do While iPos <> 0
        sToken = Trim$(Left$(sStr, iPos - 1))
        colCollection.Add sToken
        
        sStr = Mid$(sStr, iPos + 1)
        iPos = InStr(1, sStr, psDelim, 1)
    Loop
    
    '
    '   make the remainder of the string the last token
    '
    sToken = Trim$(sStr)
    If psSuppleLastNull = False Then
        If sToken <> "" Then
            colCollection.Add sToken
        End If
    Else
        colCollection.Add sToken
    End If
    
    Set gfncolTokenize = colCollection
    
    Set colCollection = Nothing
        
End Function
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值