VBA Issue Creating Filename with variables

mib1019

Board Regular
Joined
Nov 9, 2017
Messages
66
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
I am STUCK!!

I have a sub that creates a filename from extracting parts of text from cells A1 and A2 on an imported workbook, then concatenating those values. My code is putting my filename on two lines of that cell; debug window shows it the same way. When this sub will be called by another Sub SaveFile(), Excel hits Runtime error 1004.

Here is what the target cell, and debug window, look like after the GetFileName sub runs:
Hot List
20171108

Here's my code:

Sub GetFileName


Dim ListName As String
Dim ListDate As Date
Dim TextDate


Dim MyString1
Dim MyString2


MyString1 = Worksheets("Stock List").Range("A1").Value
MyString2 = Worksheets("Stock List").Range("A2").Value

ListName = Right(MyString1, Len(MyString1) - InStr(MyString1, ": ") - 1)
ListDate = DateValue(Right(MyString2, Len(MyString2) - InStr(MyString2, "y, ") - 1))


Dim strMonth
Dim strDay
Dim strYear


strYear = Year(ListDate)


If Len(Month(ListDate)) = 1 Then
strMonth = "0" & Month(ListDate)
Else
strMonth = Month(ListDate)
End If

If Len(Day(ListDate)) = 1 Then
strDay = "0" & Day(ListDate)
Else
strDay = Day(ListDate)
End If


TextDate = strYear & strMonth & strDay

strFileName = ListName & " " & TextDate


Debug.Print strFileName


End Suber sub SaveFile(), I get Runtime error 1004.

Maybe someone can help me with this pesky issue. I cannot figure it out!

Thanks!
MIB
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
Your code works for me.
Is anything missing from this line
Code:
strFileName = ListName & " " & TextDate
 
Upvote 0
Solution
Thanks for your answer.

When I run this code, it oddly 'wraps' the return on variables into two line, both in the debug window and in the cell on the worksheet (as if it puts in the first variable's value, then an ALT key, then the second variable's value). The variables are returned correctly, but the insertion of those values is causing the problem.

The debug window looks like this:
Hot List

Do you not get that same result? What am I missing?

Thanks again!


20171108
 
Upvote 0
Sorry, but my previous post has a couple of problems. What I meant to say is that my debug window, and the cell holding the value of the concatenated variables, looks like this:

Hot List
20171108
 
Upvote 0
It looks like you may have a line feed in there, which is why asked the question in post#2.
Could you answer that question?
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top