找回密码
 加入
搜索
查看: 192|回复: 4

[系统综合] 如何将VBA代码转成au3运行?

[复制链接]
发表于 2024-4-20 13:04:49 | 显示全部楼层 |阅读模式
本帖最后由 tuanyuan_au3 于 2024-4-20 16:31 编辑

Sub 宏1()
'
' 宏1 宏
'

'
    Range("A1:J1").Select
    ActiveCell.FormulaR1C1 = "2024年4月明细[4月30日]"
    With ActiveCell.Characters(Start:=1, Length:=14).Font
        .Name = "宋体"
        .FontStyle = "加粗"
        .Size = 16
        .Strikethrough = False
        .Superscript = False
        .Subscript = False
        .OutlineFont = False
        .Shadow = False
        .Underline = xlUnderlineStyleNone
        .ColorIndex = xlAutomatic
        .TintAndShade = 0
        .ThemeFont = xlThemeFontNone
    End With
    With ActiveCell.Characters(Start:=15, Length:=13).Font
        .Name = "宋体"
        .FontStyle = "加粗"
        .Size = 9
        .Strikethrough = False
        .Superscript = False
        .Subscript = False
        .OutlineFont = False
        .Shadow = False
        .Underline = xlUnderlineStyleNone
        .ColorIndex = xlAutomatic
        .TintAndShade = 0
        .ThemeFont = xlThemeFontNone
    End With
    Range("C2").Select
End Sub

怎么把上面的代码转变成au3代码?
发表于 2024-4-20 14:29:13 | 显示全部楼层
重新用AU3写吧,不过我觉得VBA可能更方便一些,毕竟很多功能AU3并没有,比如打印...
发表于 2024-4-20 20:09:37 | 显示全部楼层
#include <Excel.au3>

; 启动Excel应用程序
Local $oExcel = _Excel_Open()

If @error Then
    MsgBox(0, "Error", "Failed to start Excel.")
    Exit
EndIf

; 打开工作簿
Local $oWorkbook = _Excel_BookOpen($oExcel, "路径\你的工作簿.xlsx")

If @error Then
    MsgBox(0, "Error", "Failed to open workbook.")
    _Excel_Close($oExcel)
    Exit
EndIf

; 激活工作表
_Excel_SheetActivate($oWorkbook, "Sheet1")

; 设置单元格内容和格式
_Excel_RangeWrite($oWorkbook, Default, "2024年4月明细[4月30日]", "A1")
_Excel_RangeFont($oWorkbook, Default, "A1", "宋体", 16, True, False, False, False, 0)

_Excel_RangeWrite($oWorkbook, Default, "其他文本", "A1", Default, Default, Default, 14)
_Excel_RangeFont($oWorkbook, Default, "A1", "宋体", 9, True, False, False, False, 0, Default, 14)

; 关闭工作簿并退出Excel应用程序
_Excel_BookClose($oWorkbook)
_Excel_Close($oExcel)

你试下,无测试过
发表于 2024-4-20 21:27:54 | 显示全部楼层


#include <Excel.au3>
Local $oExcel = _Excel_Open()
Local $oWorkbook = _Excel_BookNew($oExcel)
Local Const $xlUnderlineStyleNone = -4142
Local Const $xlAutomatic = -4105
Local Const $xlThemeFontNone = 0
$oExcel.Range("A1:J1").Select
$oExcel.ActiveCell.FormulaR1C1 = "2024年4月明细[4月30日]"
Local $Start = 1, $Length = 14
With $oExcel.ActiveCell.Characters($Start, $Length).Font
        .Name = "宋体"
        .FontStyle = "加粗"
        .Size = 16
        .Strikethrough = False
        .Superscript = False
        .Subscript = False
        .OutlineFont = False
        .Shadow = False
        .Underline = $xlUnderlineStyleNone
        .ColorIndex = $xlAutomatic
        .TintAndShade = 0
        .ThemeFont = $xlThemeFontNone
EndWith
$Start = 15
$Length = 13
With $oExcel.ActiveCell.Characters($Start, $Length).Font
        .Name = "宋体"
        .FontStyle = "加粗"
        .Size = 9
        .Strikethrough = False
        .Superscript = False
        .Subscript = False
        .OutlineFont = False
        .Shadow = False
        .Underline = $xlUnderlineStyleNone
        .ColorIndex = $xlAutomatic
        .TintAndShade = 0
        .ThemeFont = $xlThemeFontNone
EndWith
$oExcel.Range("C2").Select




 楼主| 发表于 2024-4-21 13:11:13 | 显示全部楼层
非常感谢,学到很多知识,谢谢两位!!
您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-5-3 21:34 , Processed in 0.071447 second(s), 20 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表