简体   繁体   中英

VBA Excel String Escape backspace character

I'm making a macro for progress reports that will update the total income, cost, and profit cells for the user by only asking the user for the date of the progress report they want to add. I had this working when all the files were on a jump drive, but when I moved them all to my hard drive and changed the string so it wasn't looking in the jump drive it pops up an open dialog box for every cell. It works when you select the file you want but I was trying not to have to do it this way.

I have an input box show up as soon as you start the macro that asks for the date in a specified format. This works fine, only shows up once and that's what I want.

I'm thinking the issue has something to do with my wkbk variable because of the \\ character and therefore isn't finding the file and opening up the open dialog box for each cell.

Is there a way to escape this character in VBA? All the escaping character information I've found is for ' or " and never anything else.

    Range("J38:K38").Select
    myNum = Application.InputBox("Please enter the date of report to be added EX 5-12-13")
Dim wkbk As String
    wkbk = "Documents\Project for dad\Sent\PROGRESS REPORTS " & myNum & ".xls"

ActiveCell.FormulaR1C1 = _
    "=RC[-2]+'[wkbk]Day'!R38C10:R38C11"
Range("J39:K39").Select
ActiveCell.FormulaR1C1 = _
    "=RC[-2]+'[wkbk]Day'!R39C10:R39C11"
Range("J40:K40").Select
ActiveCell.FormulaR1C1 = _
    "=RC[-2]+'[wkbk]Day'!R40C10:R40C11"

You have a number of errors with your code:

  1. If the path for the file is bad you will get the behavior you describe
  2. You are not using .FormulaR1C1 correctly

This code will work BUT your solution is not robust:

Sub Test2()
  'Dim's go at the top
  Dim wkbk As String
  Dim RelativeOffSet As String
  Dim strRange

  'This is just the file name
  myNum = Application.InputBox("Please enter the date of report to be added EX 5-12-13")
  wkbk = "Documents\Project for dad\Sent\PROGRESS REPORTS"

  'gets the "C:\" from the cell relative to activeCell (h38 after 1st select)
  RelativeOffSet = "=RC[-2]"
  Range("J38:K38").Select
  strRange = "$J$38:$K$38"

  ActiveCell.FormulaR1C1 = RelativeOffSet
  ActiveCell.Value = "='" & ActiveCell.Value & wkbk & "[" & myNum & ".xlsx]Sheet1'!" + strRange 
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM