【问题标题】:Error putting a variable inside excel VBA function将变量放入 excel VBA 函数时出错
【发布时间】:2017-11-04 21:09:53
【问题描述】:
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 代码。我收到一个错误

运行时错误“1004”应用程序定义或对象定义错误

在这一行

"=CONCATENATE(""COPY "",CHAR(34),RC[-1],CHAR(34),"" "", CHAR(34), [" & folderLocation & "],RC[-3],"".png"",CHAR(34))"

我一直将folderLocation 变量值设置为c:\doge 并制作一个反映这一点的文件夹

在我在 excel 函数中引入变量之前,我的代码运行良好

我在这里做错了什么?

编辑

这是我使用的原始公式

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

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

【问题讨论】:

  • 您可以手动将公式放入单元格并分享该公式是什么吗?
  • 编辑更新
  • 查看我发布的答案

标签: excel debugging error-handling excel-formula vba


【解决方案1】:

这是你正在尝试的吗?

folderLocation = "c:\doge\"

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

【讨论】:

  • 你会不会碰巧知道如何格式化这个字符串? COPY "foo.png" "C:\batch\foo2.png"(原创)转COPY "C:\foo.png" "C:\batch\foo2.png"
  • 你的意思是这样? "复制""C:\foo.png""""C:\batch\foo2.png""
  • 是的,基本上就是这样,在第二个参数中添加一个C:\foo。 Excel VBA 格式让我很困惑。
猜你喜欢
  • 2013-03-27
  • 1970-01-01
  • 2018-09-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多