Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 11-04-2015, 07:27 AM
Officer_Bierschnitt Officer_Bierschnitt is offline How to refer to a variable with BLANKs Windows 7 64bit How to refer to a variable with BLANKs Office 2013
Advanced Beginner
How to refer to a variable with BLANKs
 
Join Date: Oct 2015
Posts: 79
Officer_Bierschnitt is on a distinguished road
Default How to refer to a variable with BLANKs

Hi,



I simply want to activate a worksheet in a file I already have open.
The sheet_name has a BLANK, however, so just putting the variable_name holding the sheet_name in brackets causes an error.
What to do here?

Thanks a lot!

Best regards,

DataNibbler
Reply With Quote
  #2  
Old 11-04-2015, 06:49 PM
NoSparks NoSparks is offline How to refer to a variable with BLANKs Windows 7 64bit How to refer to a variable with BLANKs Office 2010 64bit
Excel Hobbyist
 
Join Date: Nov 2013
Location: British Columbia, Canada
Posts: 831
NoSparks is just really niceNoSparks is just really niceNoSparks is just really niceNoSparks is just really niceNoSparks is just really nice
Default

What error are you getting ?
Reply With Quote
  #3  
Old 11-05-2015, 01:28 AM
Officer_Bierschnitt Officer_Bierschnitt is offline How to refer to a variable with BLANKs Windows 7 64bit How to refer to a variable with BLANKs Office 2013
Advanced Beginner
How to refer to a variable with BLANKs
 
Join Date: Oct 2015
Posts: 79
Officer_Bierschnitt is on a distinguished road
Default

Hi,

I am getting a Runtime_error 9 "Subscript out of Range". I don't know for sure what is the reason, but that line is highlighted, so I guess it's because of the BLANK in the name.
I resorted to accessing the sheet by number, that works, but it's not very secure - anyone might add a sheet for whatever reason so the numbering would change.
Reply With Quote
  #4  
Old 11-05-2015, 01:35 AM
macropod's Avatar
macropod macropod is offline How to refer to a variable with BLANKs Windows 7 64bit How to refer to a variable with BLANKs Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Please post the code containing the problem line, plus the actual name of the sheet.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #5  
Old 11-05-2015, 02:18 AM
Officer_Bierschnitt Officer_Bierschnitt is offline How to refer to a variable with BLANKs Windows 7 64bit How to refer to a variable with BLANKs Office 2013
Advanced Beginner
How to refer to a variable with BLANKs
 
Join Date: Oct 2015
Posts: 79
Officer_Bierschnitt is on a distinguished road
Default

Well, the code in that place is the following

----------------------------------------
'v_Sheetname = "Data Input " & Date
'Sheets(v_Sheetname).Activate
'-----------------------------------------

The name of the sheet is like >>Data Input 04.11.2015<<.
The objective of that code is to activate the sheet with today's date, which holds yesterday's data.
The second line (with the Activate keyword) is underlined when the error occurs.

Thanks a lot!
Reply With Quote
  #6  
Old 11-05-2015, 02:30 AM
macropod's Avatar
macropod macropod is offline How to refer to a variable with BLANKs Windows 7 64bit How to refer to a variable with BLANKs Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

So, given that today's date is 05.11.2015, if a sheet named "Data Input 05.11.2015" doesn't exist, you'll get a 'subscript out of range' error... There's also the question of whether the Date keyword on your system returns dates expressed in the DD.MM.YYYY format.

Even so, you shouldn't need to activate a worksheet to work with it. For example:
Code:
Sub Test()
Dim v_Sheetname As String
v_Sheetname = "Data Input " & Format(Date, "DD.MM.YYYY")
MsgBox Sheets(v_Sheetname).Range("A1").Value
End Sub
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #7  
Old 11-05-2015, 02:45 AM
Officer_Bierschnitt Officer_Bierschnitt is offline How to refer to a variable with BLANKs Windows 7 64bit How to refer to a variable with BLANKs Office 2013
Advanced Beginner
How to refer to a variable with BLANKs
 
Join Date: Oct 2015
Posts: 79
Officer_Bierschnitt is on a distinguished road
Default

Well,

the sheet does exist at the time I run my macro - it is prepared every day before 8 o' clock.
The output of the DATE function is formatted like that, at least it is when I output it in a MsgBox.
You're right, I don't need to activate the sheet, just like I don't need to SELECT cells all the time just to put some values into them. It just makes the code a lot easier to read (for not-savvy others), which is one of my primary development standards.

So, what can I do, or how could I make sure the date is formatted like I need it?

Thanks a lot!
Reply With Quote
  #8  
Old 11-05-2015, 02:54 AM
macropod's Avatar
macropod macropod is offline How to refer to a variable with BLANKs Windows 7 64bit How to refer to a variable with BLANKs Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

The code I posted can be used just as easily to activate a sheet - just change:
MsgBox Sheets(v_Sheetname).Range("A1").Value
to:
Sheets(v_Sheetname).Range("A1").Activate

As I said, the issue may well be with what your code returns as the date.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #9  
Old 11-05-2015, 03:09 AM
Officer_Bierschnitt Officer_Bierschnitt is offline How to refer to a variable with BLANKs Windows 7 64bit How to refer to a variable with BLANKs Office 2013
Advanced Beginner
How to refer to a variable with BLANKs
 
Join Date: Oct 2015
Posts: 79
Officer_Bierschnitt is on a distinguished road
Default

Quote:
Originally Posted by macropod View Post
As I said, the issue may well be with what your code returns as the date.
That may well be - and how could I change that?
(if that is the case, your code would not work any better in my instance as it also uses that variable, no?)
Reply With Quote
  #10  
Old 11-05-2015, 03:43 AM
macropod's Avatar
macropod macropod is offline How to refer to a variable with BLANKs Windows 7 64bit How to refer to a variable with BLANKs Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

You might at least try the code I've posted and suggested.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #11  
Old 11-05-2015, 03:56 AM
Officer_Bierschnitt Officer_Bierschnitt is offline How to refer to a variable with BLANKs Windows 7 64bit How to refer to a variable with BLANKs Office 2013
Advanced Beginner
How to refer to a variable with BLANKs
 
Join Date: Oct 2015
Posts: 79
Officer_Bierschnitt is on a distinguished road
Default

Quote:
Originally Posted by macropod View Post
You might at least try the code I've posted and suggested.
I have. It returns the exact same error, just like I said.
Reply With Quote
  #12  
Old 11-05-2015, 04:04 AM
macropod's Avatar
macropod macropod is offline How to refer to a variable with BLANKs Windows 7 64bit How to refer to a variable with BLANKs Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

It works for me...
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #13  
Old 11-05-2015, 05:06 AM
Officer_Bierschnitt Officer_Bierschnitt is offline How to refer to a variable with BLANKs Windows 7 64bit How to refer to a variable with BLANKs Office 2013
Advanced Beginner
How to refer to a variable with BLANKs
 
Join Date: Oct 2015
Posts: 79
Officer_Bierschnitt is on a distinguished road
Default

Well, I thought so ... do you put that sheetname_variable in quotes or something?
I use the code
>>Sheets(v_Sheetname).Activate<<
which doesn't work.
The formatting of the date in a MsgBox is the exact same as on the sheet. Possibly the internal formatting that is used for the variable is different?
How could I control that?
Reply With Quote
  #14  
Old 11-05-2015, 07:40 AM
NoSparks NoSparks is offline How to refer to a variable with BLANKs Windows 7 64bit How to refer to a variable with BLANKs Office 2010 64bit
Excel Hobbyist
 
Join Date: Nov 2013
Location: British Columbia, Canada
Posts: 831
NoSparks is just really niceNoSparks is just really niceNoSparks is just really niceNoSparks is just really niceNoSparks is just really nice
Default

The error is caused by no existing sheet having a name exactly the same as the variable string.
I'd be looking for extra spaces in one or the other.
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to refer to a variable with BLANKs Counting Blanks in 1 Column and Non-Blanks in Another dogwood705 Excel 4 02-07-2015 08:45 AM
Run Time Error '91': Object variable or With block variable not set using Catalogue Mailmerge Berryblue Mail Merge 1 11-13-2014 05:36 PM
How to refer to a variable with BLANKs Run-time error 91 object variable or with block variable not set JUST ME Word VBA 4 03-25-2014 06:56 AM
How to refer to a variable with BLANKs object variable or with block variable not set MJP143 Excel 1 02-11-2013 05:07 AM
How to refer to a variable with BLANKs Run-time error '91': Object variable or With block variable not set tinfanide Excel Programming 2 06-10-2012 10:17 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 10:50 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft