简体   繁体   中英

VBA: saving to specific path based on cell values

I made a macro to save an Excel file on a location based on some cell values. But when I run the macro the file won't save. The last line of the macro becomes yellow.

If I skip the dtMonth and dtMonthnumber the files saves just fine, so the problem is not dtYear , or Format(dtDate, "yymmdd") .

Do I need to concert the cell values?

The formulas in the cells are to convert date to month and year:

U1 =TEXT(Controle!H6;"mmmm")

U2 =TEXT(Controle!H6;"jjjj")

U3 =TEXT(H6;"mm")

Dim dtDate As Date
dtDate = Date

Dim dtMonth As String
Dim dtYear As String
Dim dtMonthnumber As String

dtMonth = ThisWorkbook.Sheets("Controle").Range("U1")
dtYear = ThisWorkbook.Sheets("Controle").Range("U2")
dtMonthnumber = ThisWorkbook.Sheets("Controle").Range("U3")

Dim strFile As String
strFile = "M:\X-tra pakketten\" & dtYear & "\" & dtMonthnumber & " - " & dtMonth & "\" & Format(dtDate, "yymmdd") & ".xlsx"
ActiveWorkbook.SaveAs Filename:=strFile, FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False

Sounds like a VBA bug, where Breakpoints are sometimes not properly deleted. Try this:

  1. Set a Breakpoint somewhere inside your procedure.
  2. Change something inside your code, so it needs to recompile (for example add a Debug.Print "" somewhere)
  3. Use the menu Debug > Delete all breakpoints ( Ctrl + Shift + F9 )
  4. Recompile it

That should solve the problem. If it still doesn't work, copy your code somewhere, delete the module, create a new module and insert the code again.

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