UtterAccess.com
X   Site Message
(Message will auto close in 2 seconds)

Welcome Guest ( Log In | Register )

2 Pages V   1 2 >  
Reply to this topicStart new topic
> Hide    
 
   
John_Ross
post Mar 14 2012, 08:37 AM
Post #1

UtterAccess Veteran
Posts: 337



Hi,

I’ve several workbooks titled Products A Jan12.xls, Products B Jan12.xls, Products C Jan12.xls etc. Here is how it works.

1. All the products workbooks are developed using the same template (Products Template.xls).
2. The Products Template.xls is separated into different products using a different excel file that acts as the report builder – Product Report Builder.xls
3. The location where Product Report Builder.xls saved is different than the individual products workbooks (i.e. Products A Jan12.xls, Products B Jan12.xls, Products C Jan12.xls etc.)

My problem:
I want to hide “Line 25” in sheet “Historic Sales” of each of the products workbook that I generated except workbook Products D Jan12.xls. I just want to add a vba code and button to Product Report Builder.xls that will hide “Line 25” in “Historic Sales” of each of the products workbook except workbook Products D Jan12.xls. How do I do that?

BTW, the name of the month will change every month.

Thank you.
Go to the top of the page
 
+
Bob G
post Mar 14 2012, 08:39 AM
Post #2

UtterAccess VIP
Posts: 8,188
From: CT



there are a few ways to approach this. getting some information in the beginning might help eliminate some choices.

are all these workbooks in the same folder?
are there any other workbooks in this folder ?
Go to the top of the page
 
+
John_Ross
post Mar 14 2012, 08:42 AM
Post #3

UtterAccess Veteran
Posts: 337



All the workbooks - Product A Jan12.xls, Product B Jan12.xls etc. are in the same folder - Jan12

There are other workbooks as well in the same folder.
Go to the top of the page
 
+
Bob G
post Mar 14 2012, 08:47 AM
Post #4

UtterAccess VIP
Posts: 8,188
From: CT



sorry i dont think of all the questions at the same time (IMG:style_emoticons/default/smile.gif)

are these workbooks the only ones that start with the word "Product" ?
Go to the top of the page
 
+
John_Ross
post Mar 14 2012, 08:53 AM
Post #5

UtterAccess Veteran
Posts: 337



Actually when I said Product A I meant a brand name like "A Jan12.xls", Product B refers to a different brand "B jan12.xls" etc. The names of the product do not change. It's the month Jan12, Feb12 that changes every month. These are the only files in the folder with brand names.
Go to the top of the page
 
+
Bob G
post Mar 14 2012, 08:59 AM
Post #6

UtterAccess VIP
Posts: 8,188
From: CT



here is what i was thinking in an overview format.

if you can distinguish these workbooks from others then you could do a for loop in an module. This is not syntax correct but just a look if you will.

CODE
for each workbook in this folder
if workbook.name = "Products D Jan12.xls" then
else
if right(name,2) = 12 then
  open the workbook
hide the row
else
end if
end if
next
Go to the top of the page
 
+
arnelgp
post Mar 14 2012, 09:22 AM
Post #7

UtterAccess Ruler
Posts: 1,090



Is "History Sales" a sheet among your workbooks?
Go to the top of the page
 
+
John_Ross
post Mar 14 2012, 09:26 AM
Post #8

UtterAccess Veteran
Posts: 337



Hi Bob,

A few issues:

1. The location of the Report Builder.xls (where I plan to use this code) is different than the product folder.
2. The name of the month changes every month.
3. Can you show me how to write an array, and I can follow that with the other products in the code on my own?

Thanks.
Go to the top of the page
 
+
John_Ross
post Mar 14 2012, 09:28 AM
Post #9

UtterAccess Veteran
Posts: 337



Hi Arnel,

Yes. I plan to hide Line 25 in worksheet "Historic Sales" in all the products xls except Product D.
Go to the top of the page
 
+
Bob G
post Mar 14 2012, 09:30 AM
Post #10

UtterAccess VIP
Posts: 8,188
From: CT



1. that would not matter as long as the product workbooks are in the same location
2. the name of the month wouldnt matter as we are keying on the year. this would be to distinguish from other workbooks in that folder that dont apply to this. if there is another visual difference you could use that


3. when and if i get a few extra minutes I will try and piece something together. I believe you can search UA for other threads that "loop thru workbooks in a folder"
Go to the top of the page
 
+
Bob G
post Mar 14 2012, 09:39 AM
Post #11

UtterAccess VIP
Posts: 8,188
From: CT



this is alot more than you are looking for but might be a good start

http://www.UtterAccess.com/forum/Vbscript-...rkbooks++folder
Go to the top of the page
 
+
arnelgp
post Mar 14 2012, 09:56 AM
Post #12

UtterAccess Ruler
Posts: 1,090



give me the fullpath name of your data (xls) file. Am I correct, your excel starts with capital letters, ie. "A jan12.xls", if not show me one real excel file name. I am almost done with the code.
Go to the top of the page
 
+
John_Ross
post Mar 14 2012, 10:09 AM
Post #13

UtterAccess Veteran
Posts: 337



Hi Arnel,

Thank you so much. Example name are "Colgate Jan12.xls", 'Tide Jan12.xls".

Thanks
Go to the top of the page
 
+
arnelgp
post Mar 14 2012, 10:28 AM
Post #14

UtterAccess Ruler
Posts: 1,090



then what "real" excel filename is Product D?
Go to the top of the page
 
+
John_Ross
post Mar 14 2012, 10:36 AM
Post #15

UtterAccess Veteran
Posts: 337



The name is "Crest Jan12.xls"
Go to the top of the page
 
+
Bob G
post Mar 14 2012, 10:40 AM
Post #16

UtterAccess VIP
Posts: 8,188
From: CT



that is why i was concentrating on the 12 as being the year.
Go to the top of the page
 
+
arnelgp
post Mar 14 2012, 10:42 AM
Post #17

UtterAccess Ruler
Posts: 1,090



thank you for your patience, one more thing, the full path where Crest Jan12.xls and the rest of the excel file resides, ie. (C:\data\excel), etc...
Go to the top of the page
 
+
John_Ross
post Mar 14 2012, 10:51 AM
Post #18

UtterAccess Veteran
Posts: 337



All "Product" files resides here:

C:\Documents and Settings\All Users\Desktop\Products\Jan12

The Report Builder.xls is located here (where I want the vba code to be at):

C:\Documents and Settings\All Users\Desktop\Products
Go to the top of the page
 
+
arnelgp
post Mar 14 2012, 11:16 AM
Post #19

UtterAccess Ruler
Posts: 1,090



heres your code:
CODE
Private Sub hideLine25()
    Dim strComputer As String
    Dim objWMIService As Object
    Dim colFiles As Object
    Dim objFile As Object
    Dim wb As Excel.Workbook
    Dim ws As Excel.Worksheet

    Dim strPath As String
    Dim strDrive As String
    Dim strExtension As String
    
    ' drive, path and file extension
    strDrive = "C:"
    strPath = "\Documents and Settings\All Users\Desktop\Products\Jan12\"
    strExtension = "xls"
    
    strComputer = "."
    Set objWMIService = GetObject("winmgmts:" _
                                & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
    Set colFiles = _
    objWMIService.ExecQuery("Select * From CIM_DataFile " & _
              "Where " & _
              "Drive = '" & strDrive & "' " & _
              "And Path = '" & Replace(strPath, "\", "\\") & "' " & _
              "And Extension = '" & strExtension & "'")
              
    For Each objFile In colFiles
        'Debug.Print strDrive & strPath & objFile.Filename & "." & strExtension
        ' check if Crest is in the file name, or it is a temporary excel file
        If (InStr(objFile.Filename, "Crest") = 0) And (InStr(objFile.Filename, "~!") = 0) Then
            Set wb = Application.Workbooks.Open(strDrive & strPath & objFile.Filename & "." & strExtension)
            On Error Resume Next
            ' if our sheet is not found, error message will appear
            ' and we dont want that
            Set ws = wb.Sheets("Historic Sales")
            On Error GoTo 0
            If Err.Number = 0 Then
                ' no error, our sheet was found
                ' then hide it.
                Rows("25:25").Select
                Selection.EntireRow.Hidden = True
                DoEvents
            End If
            Set ws = Nothing
            wb.Save
            wb.Close
            Set wb = Nothing
            DoEvents
        End If
    Next
End Sub

you have to put this in a Module.

This post has been edited by arnelgp: Mar 14 2012, 11:41 AM
Go to the top of the page
 
+
John_Ross
post Mar 14 2012, 11:19 AM
Post #20

UtterAccess Veteran
Posts: 337



Hi Arnel,

Thank you very much. The code looks great. BTW, I also mentioned that there are other unrelated files in Jan12 folder that are not product files, will they be affected as well?

None of them have "Historic Sales" sheet though.
Go to the top of the page
 
+

2 Pages V   1 2 >
Thank you for your support! Reply to this topicStart new topic

Jump To Forum:
 



RSS Go to Top  ·  Lo-Fi Version Time is now: 18th June 2013 - 01:59 AM