深圳全飞鸿
标题: 关于Excel的VBA开发验证 [打印本页]
作者: syant 时间: 2025-1-12 20:47
标题: 关于Excel的VBA开发验证
关于Excel的VBA开发验证
1.打开宏开发
打开Excel表格,点击左上角【文件】-【更多】-【选项】,在弹出的“Excel选项”对话框中点击【自定义功能区】,将右侧【开发工具】勾选,然后点击【确定】,这样“开发工具”选项卡就显示出来了。
https://baijiahao.baidu.com/s?id=1786686340589736102&wfr=spider&for=pc
2、另存xlsm格式
3、注册一个测试证书
SELFCERT.EXE
C:\Program Files\Microsoft Office\root\Office16
作者: syant 时间: 2025-1-12 20:56
用VBA写一段代码,抓取当前sheet的A5至A11范围内单元格里的内容,并用逗号合并成一个字符串,并把这个字符串写到F2单元格
- Sub 按钮1_Click()
- Dim ws As Worksheet
- Dim cell As Range
- Dim result As String
-
- ' 获取当前活动的工作表
- Set ws = ActiveSheet
-
- ' 初始化结果字符串
- result = ""
-
- ' 遍历A5到A11范围内的单元格
- For Each cell In ws.Range("A5:A11")
- ' 如果单元格不为空,则将其内容添加到结果字符串中
- If cell.Value <> "" Then
- result = result & cell.Value & ""
- End If
- Next cell
-
- ' 移除最后一个多余的逗号
- If Len(result) > 0 Then
- result = Left(result, Len(result) - 1)
- End If
-
- ' 将结果字符串写入到F2单元格
- ws.Range("F2").Value = result
- End Sub
复制代码
作者: syant 时间: 2025-1-12 21:08
VBA能否实现从一个网址获取一个图片,并用这个图片来更新exel档上的一个图片
- Sub DownloadAndInsertImage()
- Dim imageUrl As String
- Dim imagePath As String
- Dim ws As Worksheet
- Dim pic As Picture
-
- ' 设置图片URL和保存路径
- imageUrl = "http://127.0.0.1/qrcode/temp/test1.png" ' 替换为实际的图片URL
- imagePath = Environ("TEMP") & "\downloaded_image.png"
-
- ' 下载图片
- DownloadFile imageUrl, imagePath
-
- ' 插入图片到工作表
- Set ws = ThisWorkbook.Sheets(1) ' 选择要插入图片的工作表
- Set pic = ws.Pictures.Insert(imagePath)
-
- ' 调整图片位置和大小(可选)
- With pic
- .Left = ws.Range("A1").Left
- .Top = ws.Range("A1").Top
- .Width = 100 ' 设置宽度
- .Height = 100 ' 设置高度
- End With
- End Sub
- Function DownloadFile(ByVal URL As String, ByVal LocalFilename As String) As Boolean
- Dim WinHttpReq As Object
- Set WinHttpReq = CreateObject("Microsoft.XMLHTTP")
- WinHttpReq.Open "GET", URL, False
- WinHttpReq.send
-
- If WinHttpReq.Status = 200 Then
- Dim oStream As Object
- Set oStream = CreateObject("ADODB.Stream")
- oStream.Open
- oStream.Type = 1
- oStream.Write WinHttpReq.responseBody
- oStream.SaveToFile LocalFilename, 2 ' 1 = no overwrite, 2 = overwrite
- oStream.Close
- DownloadFile = True
- Else
- DownloadFile = False
- End If
- End Function
- Private Sub CommandButton1_Click()
- 'MsgBox "Great!"
- DownloadAndInsertImage
- End Sub
复制代码
作者: syant 时间: 2025-1-12 21:26
在VBA中,Picture 对象有许多属性和方法,可以用来操作图片。以下是一些常用的 Picture 对象的属性:
- Name: 图片的名称。
- Left: 图片左上角的横坐标。
- Top: 图片左上角的纵坐标。
- Width: 图片的宽度。
- Height: 图片的高度。
- Locked: 一个布尔值,指示图片是否被锁定。
- Placement: 图片的位置类型(例如,xlMoveAndSize、xlMove、xlFreeFloating)。
- PrintObject: 一个布尔值,指示图片是否在打印时显示。
- ShapeRange: 返回包含该图片的 ShapeRange 对象。
- Visible: 一个布尔值,指示图片是否可见。
- ZOrder: 图片的堆叠顺序。
以下是一个示例代码,演示如何访问和修改这些属性:
- Sub PicturePropertiesExample()
- Dim ws As Worksheet
- Dim pic As Picture
-
- ' 获取要操作的工作表
- Set ws = ThisWorkbook.Sheets(1) ' 选择要操作的工作表
-
- ' 假设工作表中已经有一个图片对象
- On Error Resume Next
- Set pic = ws.Pictures(1)
- On Error GoTo 0
-
- If Not pic Is Nothing Then
- ' 输出图片的一些属性
- Debug.Print "Name: " & pic.Name
- Debug.Print "Left: " & pic.Left
- Debug.Print "Top: " & pic.Top
- Debug.Print "Width: " & pic.Width
- Debug.Print "Height: " & pic.Height
- Debug.Print "Locked: " & pic.Locked
- Debug.Print "Placement: " & pic.Placement
- Debug.Print "PrintObject: " & pic.PrintObject
- Debug.Print "Visible: " & pic.Visible
- Debug.Print "ZOrder: " & pic.ZOrder
-
- ' 修改图片的一些属性
- pic.Left = 100
- pic.Top = 100
- pic.Width = 200
- pic.Height = 200
- pic.Locked = True
- pic.Visible = False
- pic.ZOrder msoSendToBack
- End If
- End Sub
复制代码
欢迎光临 深圳全飞鸿 (http://www.nagomes.com/disc/) |
Powered by Discuz! X3.2 |