Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4
Default Setting workbooks

I have some code that includes the line:

Set shRanks = Workbooks("Ranks.csv").Sheets("Ranks")

The problem is that the file I need to set as shRanks may have an
integer in its name - eg "Ranks17.csv"

How best can I do this?
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Setting workbooks


A CSV file is only one sheet since it is really just a text file. The
sheet name is always the same as the workbookname. the best solution is
just to use the first tab.

from
Set shRanks = Workbooks("Ranks.csv").Sheets("Ranks")


to

Set shRanks = Workbooks("Ranks.csv").Sheets(1)



I don't think this is going to work since the workbook name is also
wrong. I would need to see the statement you use to read the CSV file.
If you are using workbook open then try this


fileToOpen = Application _
GetOpenFilename("Excel Files (*.xls), *.xls")
If fileToOpen = False Then
MsgBox("Cannot Open file - Exiting Macro")
exit sub
End If

Set bk = workbooks.open(filename:=fileToOpen)

Set shRanks = bk.Sheets(1)


--
joel
------------------------------------------------------------------------
joel's Profile: 229
View this thread: http://www.thecodecage.com/forumz/sh...d.php?t=170681

Microsoft Office Help

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,058
Default Setting workbooks

Sub demo()
Dim shRanks As Worksheet
Dim n As Integer
n = 17
Set shRanks = Workbooks("Ranks" & n & ".csv").Sheets("Ranks")
End Sub

--
Gary''s Student - gsnu200909


"Gordon Rainsford" wrote:

I have some code that includes the line:

Set shRanks = Workbooks("Ranks.csv").Sheets("Ranks")

The problem is that the file I need to set as shRanks may have an
integer in its name - eg "Ranks17.csv"

How best can I do this?
.

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4
Default Setting workbooks

I must have been unclear.

There will be a worksheet open, whose name is "Ranks**.csv" where ** are
wildcards. I want to set that worksheet as shRanks, whatever the value
of **,

Thanks,

Gordon

Gary''s Student wrote:

Sub demo()
Dim shRanks As Worksheet
Dim n As Integer
n = 17
Set shRanks = Workbooks("Ranks" & n & ".csv").Sheets("Ranks")
End Sub

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Setting workbooks

Maybe you can set the variable right after you open the .csv file.

Dim scvwks as worksheet
dim CSVWkbk as workbook

set csvwkbk = workbooks.open(filename:="c:\ranks.csv")
set csvwks = activesheet



Gordon Rainsford wrote:

I have some code that includes the line:

Set shRanks = Workbooks("Ranks.csv").Sheets("Ranks")

The problem is that the file I need to set as shRanks may have an
integer in its name - eg "Ranks17.csv"

How best can I do this?


--

Dave Peterson


  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Setting workbooks


You can search the window names. The search is case sensitive so make
sure your search string matches the file name.


Sub test()

Dim FName As String
Dim hWndStart As Long
Dim WindowText As String

hWndStart = 0
level = 0
WindowText = "*Ranks*.csv"


FName = FindWindowLike(hWndStart, _
WindowText, level)

set bk = Workbooks(FName)
Set shRanks = bk.Sheets(1)

End Sub





function to get name
------------------------------------------------------


Declare Function GetWindow Lib "user32" (ByVal hWnd As Long, _
ByVal wCmd As Long) As Long
Declare Function GetDesktopWindow Lib "user32" () As Long

Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" _
(ByVal hWnd As Long, ByVal lpString As String, ByVal cch As Long)
_
As Long

Public Const GWL_ID = (-12)
Public Const GW_HWNDNEXT = 2
Public Const GW_CHILD = 5
'FindWindowLike
' - Finds the window handles of the windows matching the specified
' parameters
'
'hwndArray()
' - An integer array used to return the window handles
'
'hWndStart
' - The handle of the window to search under.
' - The routine searches through all of this window's children and
their
' children recursively.
' - If hWndStart = 0 then the routine searches through all windows.
'
'WindowText
' - The pattern used with the Like operator to compare window's
text.
'
'ClassName
' - The pattern used with the Like operator to compare window's
class
' name.
'
'ID
' - A child ID number used to identify a window.
' - Can be a decimal number or a hex string.
' - Prefix hex strings with "&H" or an error will occur.
' - To ignore the ID pass the Visual Basic Null function.
'
'Returns
' - The number of windows that matched the parameters.
' - Also returns the window handles in hWndArray()
'

'----------------------------------------------------------------------



Function FindWindowLike(ByVal hWndStart As Long, _
WindowText As String, ByVal level As Integer) As String
Dim r As String
' Hold the level of recursion:
' Hold the level of recursion:
'Hold the number of matching windows:

Dim sWindowText As String
Dim sClassname As String
Dim sID

'return nothing if not found
FindWindowLike = ""

' Initialize if necessary:
If level = 0 Then
hWndStart = GetDesktopWindow()
End If
' Increase recursion counter:
level = level + 1
' Get first child window:
hWnd = GetWindow(hWndStart, GW_CHILD)
Do Until hWnd = 0
DoEvents ' Not necessary
' Search children by recursion:
r = FindWindowLike(hWnd, WindowText, level)

If r < "" Then
FindWindowLike = r
Exit Function
Else

' Get the window text and class name:
sWindowText = Space(255)

r = GetWindowText(hWnd, sWindowText, 255)
sWindowText = Left(sWindowText, r)
If InStr(sWindowText, "Excel") Then
a = 1
End If
' Check that window matches the search parameters:
If sWindowText Like WindowText Then

FindWindowLike = sWindowText

Debug.Print "Window Found: "
Debug.Print " Window Text : " & sWindowText
Debug.Print " Window Handle: " & CStr(hWnd)

Exit Function
Else

' Get next child window:
hWnd = GetWindow(hWnd, GW_HWNDNEXT)
End If

End If
Loop
End Function


--
joel
------------------------------------------------------------------------
joel's Profile: 229
View this thread: http://www.thecodecage.com/forumz/sh...d.php?t=170681

Microsoft Office Help

  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Setting workbooks


You can search the window names. The search is case sensitive so make
sure your search string matches the file name.


Sub test()

Dim FName As String
Dim hWndStart As Long
Dim WindowText As String

hWndStart = 0
level = 0
WindowText = "*Ranks*.csv"


FName = FindWindowLike(hWndStart, _
WindowText, level)

set bk = Workbooks(FName)
Set shRanks = bk.Sheets(1)

End Sub





function to get name
------------------------------------------------------


Declare Function GetWindow Lib "user32" (ByVal hWnd As Long, _
ByVal wCmd As Long) As Long
Declare Function GetDesktopWindow Lib "user32" () As Long

Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" _
(ByVal hWnd As Long, ByVal lpString As String, ByVal cch As Long)
_
As Long

Public Const GWL_ID = (-12)
Public Const GW_HWNDNEXT = 2
Public Const GW_CHILD = 5
'FindWindowLike
' - Finds the window handles of the windows matching the specified
' parameters
'
'hwndArray()
' - An integer array used to return the window handles
'
'hWndStart
' - The handle of the window to search under.
' - The routine searches through all of this window's children and
their
' children recursively.
' - If hWndStart = 0 then the routine searches through all windows.
'
'WindowText
' - The pattern used with the Like operator to compare window's
text.
'
'ClassName
' - The pattern used with the Like operator to compare window's
class
' name.
'
'ID
' - A child ID number used to identify a window.
' - Can be a decimal number or a hex string.
' - Prefix hex strings with "&H" or an error will occur.
' - To ignore the ID pass the Visual Basic Null function.
'
'Returns
' - The number of windows that matched the parameters.
' - Also returns the window handles in hWndArray()
'

'----------------------------------------------------------------------



Function FindWindowLike(ByVal hWndStart As Long, _
WindowText As String, ByVal level As Integer) As String
Dim r As String
' Hold the level of recursion:
' Hold the level of recursion:
'Hold the number of matching windows:

Dim sWindowText As String
Dim sClassname As String
Dim sID

'return nothing if not found
FindWindowLike = ""

' Initialize if necessary:
If level = 0 Then
hWndStart = GetDesktopWindow()
End If
' Increase recursion counter:
level = level + 1
' Get first child window:
hWnd = GetWindow(hWndStart, GW_CHILD)
Do Until hWnd = 0
DoEvents ' Not necessary
' Search children by recursion:
r = FindWindowLike(hWnd, WindowText, level)

If r < "" Then
FindWindowLike = r
Exit Function
Else

' Get the window text and class name:
sWindowText = Space(255)

r = GetWindowText(hWnd, sWindowText, 255)
sWindowText = Left(sWindowText, r)
If InStr(sWindowText, "Excel") Then
a = 1
End If
' Check that window matches the search parameters:
If sWindowText Like WindowText Then

FindWindowLike = sWindowText

Debug.Print "Window Found: "
Debug.Print " Window Text : " & sWindowText
Debug.Print " Window Handle: " & CStr(hWnd)

Exit Function
Else

' Get next child window:
hWnd = GetWindow(hWnd, GW_HWNDNEXT)
End If

End If
Loop
End Function


--
joel
------------------------------------------------------------------------
joel's Profile: 229
View this thread: http://www.thecodecage.com/forumz/sh...d.php?t=170681

Microsoft Office Help

  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Setting workbooks

Wouldn't it be easier to just loop through the workbooks collection matching on
the names?

dim wkbk as workbook
dim csvWks as worksheet

set csvwks = nothing
for each wkbk in application.workbooks
if lcase(wkkb.name) like lcase("ranks*.csv") then
on error resume next
set csvwks = wkbk.worksheets("ranks")
on error goto 0
if csvwks is nothing then
msgbox "Found " & wkbk.name & vblf & "no ranks sheet!"
end if
exit for
end if
next wkbk

if csvwks is nothing then
msgbox "No sheet found by that name in any open workbook"
exit sub '???
end if


========
I wouldn't use, either. I'd still set a variable when open that csv file.

joel wrote:

You can search the window names. The search is case sensitive so make
sure your search string matches the file name.

Sub test()

Dim FName As String
Dim hWndStart As Long
Dim WindowText As String

hWndStart = 0
level = 0
WindowText = "*Ranks*.csv"

FName = FindWindowLike(hWndStart, _
WindowText, level)

set bk = Workbooks(FName)
Set shRanks = bk.Sheets(1)

End Sub

function to get name
------------------------------------------------------

Declare Function GetWindow Lib "user32" (ByVal hWnd As Long, _
ByVal wCmd As Long) As Long
Declare Function GetDesktopWindow Lib "user32" () As Long

Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" _
(ByVal hWnd As Long, ByVal lpString As String, ByVal cch As Long)
_
As Long

Public Const GWL_ID = (-12)
Public Const GW_HWNDNEXT = 2
Public Const GW_CHILD = 5
'FindWindowLike
' - Finds the window handles of the windows matching the specified
' parameters
'
'hwndArray()
' - An integer array used to return the window handles
'
'hWndStart
' - The handle of the window to search under.
' - The routine searches through all of this window's children and
their
' children recursively.
' - If hWndStart = 0 then the routine searches through all windows.
'
'WindowText
' - The pattern used with the Like operator to compare window's
text.
'
'ClassName
' - The pattern used with the Like operator to compare window's
class
' name.
'
'ID
' - A child ID number used to identify a window.
' - Can be a decimal number or a hex string.
' - Prefix hex strings with "&H" or an error will occur.
' - To ignore the ID pass the Visual Basic Null function.
'
'Returns
' - The number of windows that matched the parameters.
' - Also returns the window handles in hWndArray()
'

'----------------------------------------------------------------------

Function FindWindowLike(ByVal hWndStart As Long, _
WindowText As String, ByVal level As Integer) As String
Dim r As String
' Hold the level of recursion:
' Hold the level of recursion:
'Hold the number of matching windows:

Dim sWindowText As String
Dim sClassname As String
Dim sID

'return nothing if not found
FindWindowLike = ""

' Initialize if necessary:
If level = 0 Then
hWndStart = GetDesktopWindow()
End If
' Increase recursion counter:
level = level + 1
' Get first child window:
hWnd = GetWindow(hWndStart, GW_CHILD)
Do Until hWnd = 0
DoEvents ' Not necessary
' Search children by recursion:
r = FindWindowLike(hWnd, WindowText, level)

If r < "" Then
FindWindowLike = r
Exit Function
Else

' Get the window text and class name:
sWindowText = Space(255)

r = GetWindowText(hWnd, sWindowText, 255)
sWindowText = Left(sWindowText, r)
If InStr(sWindowText, "Excel") Then
a = 1
End If
' Check that window matches the search parameters:
If sWindowText Like WindowText Then

FindWindowLike = sWindowText

Debug.Print "Window Found: "
Debug.Print " Window Text : " & sWindowText
Debug.Print " Window Handle: " & CStr(hWnd)

Exit Function
Else

' Get next child window:
hWnd = GetWindow(hWnd, GW_HWNDNEXT)
End If

End If
Loop
End Function

--
joel
------------------------------------------------------------------------
joel's Profile: 229
View this thread: http://www.thecodecage.com/forumz/sh...d.php?t=170681

Microsoft Office Help


--

Dave Peterson
  #9   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Setting workbooks


Dave: Your solution won't work if it was opened with a different Excel
application. I thought about your solution first. But if I doubled
clicked on the file from a window explorer and 2nd excel application
came up and the CSV file was not in the list of files.

The right solution is to assign an object to the CSV file when it was
opened but when haven't seen the code that opens the book. I assumed
that the CSV file was not opened by the VBA code.


--
joel
------------------------------------------------------------------------
joel's Profile: 229
View this thread: http://www.thecodecage.com/forumz/sh...d.php?t=170681

Microsoft Office Help

  #10   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Setting workbooks

This line:
Set bk = Workbooks(FName)
assumes that the workbook named FName is open in the same instance of excel that
is running the code.




joel wrote:

Dave: Your solution won't work if it was opened with a different Excel
application. I thought about your solution first. But if I doubled
clicked on the file from a window explorer and 2nd excel application
came up and the CSV file was not in the list of files.

The right solution is to assign an object to the CSV file when it was
opened but when haven't seen the code that opens the book. I assumed
that the CSV file was not opened by the VBA code.

--
joel
------------------------------------------------------------------------
joel's Profile: 229
View this thread: http://www.thecodecage.com/forumz/sh...d.php?t=170681

Microsoft Office Help


--

Dave Peterson


  #11   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4
Default Setting workbooks

joel wrote:

Dave: Your solution won't work if it was opened with a different Excel
application. I thought about your solution first. But if I doubled
clicked on the file from a window explorer and 2nd excel application
came up and the CSV file was not in the list of files.

The right solution is to assign an object to the CSV file when it was
opened but when haven't seen the code that opens the book. I assumed
that the CSV file was not opened by the VBA code.


That's correct - the csv file is opened by another program.
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Excel global setting for all workbooks? Calculation Automatic Ben Excel Discussion (Misc queries) 1 April 19th 07 04:27 PM
Setting a workbooks protection/read only/locked Jason Kontkanen Excel Programming 1 September 23rd 05 04:29 PM
Global Setting For All Workbooks - Filename In Footer TOMB Excel Worksheet Functions 3 April 4th 05 06:53 PM
Setting Maximum number of Workbooks per Instance of Excel Tom Excel Programming 4 January 14th 05 01:35 PM
add-ins error + setting workbooks peach255 Excel Programming 0 July 21st 03 07:28 PM


All times are GMT +1. The time now is 03:54 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"