Excel VBA开发

简单例子:

Function CopyPicture(rng1 As Range, patharg As String, pathargNew As String) As String
Dim r1 As Range
Dim total As Long
Dim success As Integer

Dim needStr As String

Dim MyFile As String

Dim isSucess As Integer

Dim copyNum As Integer


total = rng1.Rows.Count


If Right(patharg, 1) <> "\" Then

    patharg = patharg & "\"
 End If
 

If Right(pathargNew, 1) <> "\" Then

    pathargNew = patharg & "\"
 End If

For Each r1 In rng1 '循环当前选中列
    isSucess = 0
    needStr = "*" & r1 & "*.jpg" 'like通配符
    MyFile = Dir(patharg & needStr) '获取第一个文件
    
    If MyFile Like needStr Then
          success = success + 1
          FileCopy patharg & MyFile, pathargNew & MyFile
          copyNum = copyNum + 1
    End If
 

      If isSucess = 0 Then
        Do While MyFile <> ""
            MyFile = Dir        '第二次读入的时候不用写参数
            If MyFile = "" Then
                Exit Do         '当MyFile为空的时候就说明已经遍历完了,这时退出Do,否则还要运行一遍
            End If
             
            If MyFile Like needStr Then
               success = success + 1
               copyNum = copyNum + 1
               FileCopy patharg & MyFile, pathargNew & MyFile
               If copyNum > 4 Then
               
                Exit Do
               End If
                
            End If
        Loop
    End If

Next
    CopyPicture = "总共选择了" & total & "个产品,复制了" & success & "张图片"
End Function

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值