excel - 将变量放入 Excel VBA 函数时出错

标签 excel debugging error-handling excel-formula vba

Sub airtableCleaner()
    Dim x As Integer
    Dim argCounter As Integer
    Dim A As String
    Dim B As String
    Dim folderLocation As Variant
    Dim Answer As VbMsgBoxResult

'Ask user if they want to run macro
Answer = MsgBox("Do you want to run this macro? Please use airtable Download as CSV - Column 1: Primary key, Column 2: Airtable Linkz", vbYesNo, "Run Macro")
If Answer = vbYes Then

folderLocation = Application.InputBox("Enter a folder location where your image assets will be")


'Cleanup to just amazons3 dl.airtable links
Columns("B:B").Select
Selection.Replace What:="* ", Replacement:="", LookAt:=xlPart, _
    SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
    ReplaceFormat:=False
Selection.Replace What:="(", Replacement:="", LookAt:=xlPart, _
    SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
    ReplaceFormat:=False
Selection.Replace What:=")", Replacement:="", LookAt:=xlPart, _
    SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
    ReplaceFormat:=False

'Count Cells
Range("B2").Activate
Do
    If ActiveCell.Value = "" Then Exit Do
    ActiveCell.Offset(1, 0).Activate
    argCounter = argCounter + 1

Loop

'Copy Image Links to new cells to format in Column C
Columns("B:B").Select
Selection.Copy
Columns("C:C").Select
ActiveSheet.Paste
Application.CutCopyMode = False

'Clean up links to only have names in Column C
Selection.Replace What:="https://dl.airtable.com/", Replacement:="", _
LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:= _
False, ReplaceFormat:=False



'Create Batch on Column D
    Range("D2").Select
ActiveCell.FormulaR1C1 = _
    "=CONCATENATE(""COPY "",CHAR(34),RC[-1],CHAR(34),"" "", CHAR(34), [" & folderLocation & "],RC[-3],"".png"",CHAR(34))"
Range("D2").Select
Selection.AutoFill Destination:=Range("D2:D9")
Range("D2:D9").Select

'Delete header row 1 information
Rows("1:1").Select
Selection.Delete Shift:=xlUp

'Repaste values back into column D removing formulas
    Columns("D:D").Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False
Application.CutCopyMode = False


End If
End Sub

我有这套Excel VBA代码。我收到错误

Run-time error '1004' Application-defined or object-defined error

在这一行

"=CONCATENATE(""复制 "",CHAR(34),RC[-1],CHAR(34),"""", CHAR(34), ["& 文件夹位置 & "], RC[-3],"".png"",CHAR(34))"

我已将 folderLocation 变量值设置为 c:\doge 并创建一个反射(reflect)此情况的文件夹

我的代码工作正常,直到我在 Excel 函数内引入变量

我在这里做错了什么?

编辑

这是我使用的原始公式

=CONCATENATE("复制",CHAR(34),C5,CHAR(34),"", CHAR(34), "c:\doge\",A5,".png",CHAR( 34))

其中c:\doge\是我想要输入用户输入的地方。

最佳答案

这就是你正在尝试的吗?

folderLocation = "c:\doge\"

Range("D2").Formula = "=CONCATENATE(""COPY "",CHAR(34),C5,CHAR(34),"" "", CHAR(34), " & _
                      Chr(34) & folderLocation & Chr(34) & ",A5,"".png"",CHAR(34))"

关于excel - 将变量放入 Excel VBA 函数时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44346154/

相关文章:

php - PHP导致页面HTTP错误500(内部服务器错误):

excel - 仅当引号引起来时才删除换行符

excel - 以编程方式将 ComboBox 插入单元格内

ios - 如何在 Captive Portal 中调试浏览器?

debugging - nx 构建命令失败,类型错误 : Cannot read properties of undefined (reading 'cloud' )

c# - MVC 5 C#将错误报告回数据库

r - “Replacement Length Equal to Zero”

java - 我可以使用什么输入文件格式来创建方程式?

vba - 是否可以将范围传递给用户表单属性?

xcode - Xcode4.2中的无线调试