Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help Copying From Other Excel Workbooks

Status
Not open for further replies.

GovNewbi

Technical User
Jul 14, 2010
83
CA
Ok so I am BRAND NEW to programming in vb and pretty much brand new to programming period (with minimal experience in C++). My job has me writing a program which I have been told is NOT beginner programming and I am very lost on a few items. If there is anyone out there who can answer a few questions I would be eternally grateful!!!

I’ll start with a problem that I somewhat understand what the problem is…

In this part of the code I am trying to fetch information from other excel workbooks and copy the information into the workbook I am using. I realize this might be hard to follow because it is just a small chunk of a real long code and for that I am sorry. The error I am getting is

Compile Error:
Expected: list separator or )

It also highlights the word “Reporting” in the file location.

Here is part of the code…

Sub Fetch1()
Dim wb As Workbook
Dim ws As Worksheet
Dim CtCell As Range
Dim StGraphT As Range
Dim Proj As Range
Dim Yr As Range
Dim CT As Range
Dim CT2 As Range
Dim QRT As Range
Dim j As Integer
Dim CopyVal As String

Set CT = Range("CostType")
Set CT2 = Range("CostType2")
Set QRT = Range("Quarter")
Set Yr = Range("StYr")
Set Proj = Range("StProj").Offset(j, 0)
Set CtCell = Range("CtCell")
Set StGraphT = Range("StGraphT")
j = 0


Set ws = ActiveSheet
Set wb = Workbooks.Open("P:\ODD\OSD\BO\CARE Cost & Operations Forms _
- CONFIDENTIAL\CARE Reporting by OSR & CSR Project ID\OSR " _
& Proj.value & "\OSR" & Proj.value & " " & CT2.Value & "\OSR" & Proj.value _
& "_" & Yr.Value & "_" & QRT.Value & "_" & CT.Value & ".xls")
wb.Worksheets(SheetVal).Range(CopyVal).Copy
ws.Range("StFill").Offset(j, 0).Paste
wb.Close
j = j + 1
Loop

Application.ScreenUpdating = True

Call Graph1

End Sub


I know I have the location right so I am wondering if there is a simple error that I just can’t seem to see. A missing comma or something? PLZ HELP :(
 

Do yourself a favor and try:
Code:
[blue]
Dim strXLFile As String[/blue]

    Set ws = ActiveSheet

    [blue]strXLFile[/blue] = "P:\ODD\OSD\BO\CARE Cost & Operations Forms _
    - CONFIDENTIAL\CARE Reporting by OSR & CSR  Project ID\OSR " _
    & Proj.value & "\OSR" & Proj.value & " " & CT2.Value & "\OSR" & Proj.value _
    & "_" & Yr.Value & "_" & QRT.Value & "_" & CT.Value & ".xls")
 
    [blue]Debug.Print strXLFile [/blue]

    Set wb = Workbooks.Open([blue]strXLFile[/blue])

Put the Break on Debug line and see in Immediate window the path to your xl file, check if it is corect.

Have fun.

---- Andy
 

Hi,

Several variables not assigned like.

Where does your [red]loop begin[/red]?

How does your [green]loop end[/green]?
Code:
[red]DO[/red] [green]While {What?????}[/green]
    wb.Worksheets([b]SheetVal[/b]).Range([b]CopyVal[/b]).Copy
    ws.Range("StFill").Offset(j, 0).Paste
    wb.Close
    j = j + 1
Loop

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
The Loop is defined in the full code. I can try sending this whole sub but just to warn you it is long. Well long for me but I haven't seen much code.

Here it is...

Sub Fetch1()
Dim wb As Workbook
Dim ws As Worksheet
Dim CtCell As Range
Dim StGraphT As Range
Dim Proj As Range
Dim Yr As Range
Dim CT As Range
Dim CT2 As Range
Dim QRT As Range
Dim j As Integer
Dim CopyVal As String

Set CT = Range("CostType")
Set CT2 = Range("CostType2")
Set QRT = Range("Quarter")
Set Yr = Range("StYr")
Set Proj = Range("StProj").Offset(j, 0)
Set CtCell = Range("CtCell")
Set StGraphT = Range("StGraphT")
j = 0

Application.ScreenUpdating = False

If Range("CostType").Value = "CAPEX(IS)" Then
Do Until ActiveCell = ""
Range("StProj").Offset(j, 0).Activate
If (CtCell.Value = "PNCB") And (StGraphT.Value = "Delineation & Development") Then
CopyVal = "H15"
ElseIf (CtCell.Value = "PNCB") And (StGraphT.Value = "Gathering, Distribution and Storage") Then
CopyVal = "H16"
ElseIf (CtCell.Value = "PNCB") And (StGraphT.Value = "Transportation Infratructure") Then
CopyVal = "H17"
ElseIf (CtCell.Value = "PNCB") And (StGraphT.Value = "Co-Generation Plants") Then
CopyVal = "H18"
ElseIf (CtCell.Value = "PNCB") And (StGraphT.Value = "Bitumen Production Facilities & Equipment") Then
CopyVal = "H19"
ElseIf (CtCell.Value = "PNCB") And (StGraphT.Value = "Emulsion Treating & Cleaning (Including Water Treatment)") Then
CopyVal = "H20"
ElseIf (CtCell.Value = "PNCB") And (StGraphT.Value = "Steam Generation and Distribution") Then
CopyVal = "H21"
ElseIf (CtCell.Value = "PNCB") And (StGraphT.Value = "Environmental Monitoring") Then
CopyVal = "H22"
ElseIf (CtCell.Value = "PNCB") And (StGraphT.Value = "Turnaround / Expense Projects") Then
CopyVal = "H23"
ElseIf (CtCell.Value = "PNCB") And (StGraphT.Value = "Research") Then
CopyVal = "H24"
ElseIf (CtCell.Value = "PNCB") And (StGraphT.Value = "Other") Then
CopyVal = "H25"
ElseIf (CtCell.Value = "PNCB") And (StGraphT.Value = "Total Capital Costs") Then
CopyVal = "H27"
ElseIf (CtCell.Value = "Strategic") And (StGraphT.Value = "Delineation & Development") Then
CopyVal = "I15"
ElseIf (CtCell.Value = "Strategic") And (StGraphT.Value = "Gathering, Distribution and Storage") Then
CopyVal = "I16"
ElseIf (CtCell.Value = "Strategic") And (StGraphT.Value = "Transportation Infratructure") Then
CopyVal = "I17"
ElseIf (CtCell.Value = "Strategic") And (StGraphT.Value = "Co-Generation Plants") Then
CopyVal = "I18"
ElseIf (CtCell.Value = "Strategic") And (StGraphT.Value = "Bitumen Production Facilities & Equipment") Then
CopyVal = "I19"
ElseIf (CtCell.Value = "Strategic") And (StGraphT.Value = "Emulsion Treating & Cleaning (Including Water Treatment)") Then
CopyVal = "I20"
ElseIf (CtCell.Value = "Strategic") And (StGraphT.Value = "Steam Generation and Distribution") Then
CopyVal = "I21"
ElseIf (CtCell.Value = "Strategic") And (StGraphT.Value = "Environmental Monitoring") Then
CopyVal = "I22"
ElseIf (CtCell.Value = "Strategic") And (StGraphT.Value = "Turnaround / Expense Projects") Then
CopyVal = "I23"
ElseIf (CtCell.Value = "Strategic") And (StGraphT.Value = "Research") Then
CopyVal = "I24"
ElseIf (CtCell.Value = "Strategic") And (StGraphT.Value = "Other") Then
CopyVal = "I25"
ElseIf (CtCell.Value = "Strategic") And (StGraphT.Value = "Total Capital Costs") Then
CopyVal = "I27"
ElseIf (CtCell.Value = "Sustaining") And (StGraphT.Value = "Delineation & Development") Then
CopyVal = "J15"
ElseIf (CtCell.Value = "Sustaining") And (StGraphT.Value = "Gathering, Distribution and Storage") Then
CopyVal = "J16"
ElseIf (CtCell.Value = "Sustaining") And (StGraphT.Value = "Transportation Infratructure") Then
CopyVal = "J17"
ElseIf (CtCell.Value = "Sustaining") And (StGraphT.Value = "Co-Generation Plants") Then
CopyVal = "J18"
ElseIf (CtCell.Value = "Sustaining") And (StGraphT.Value = "Bitumen Production Facilities & Equipment") Then
CopyVal = "J19"
ElseIf (CtCell.Value = "Sustaining") And (StGraphT.Value = "Emulsion Treating & Cleaning (Including Water Treatment)") Then
CopyVal = "J20"
ElseIf (CtCell.Value = "Sustaining") And (StGraphT.Value = "Steam Generation and Distribution") Then
CopyVal = "J21"
ElseIf (CtCell.Value = "Sustaining") And (StGraphT.Value = "Environmental Monitoring") Then
CopyVal = "J22"
ElseIf (CtCell.Value = "Sustaining") And (StGraphT.Value = "Turnaround / Expense Projects") Then
CopyVal = "J23"
ElseIf (CtCell.Value = "Sustaining") And (StGraphT.Value = "Research") Then
CopyVal = "J24"
ElseIf (CtCell.Value = "Sustaining") And (StGraphT.Value = "Other") Then
CopyVal = "J25"
ElseIf (CtCell.Value = "Sustaining") And (StGraphT.Value = "Total Capital Costs") Then
CopyVal = "J27"
ElseIf (CtCell.Value = "Reclamation / Abandonment") And (StGraphT.Value = "Delineation & Development") Then
CopyVal = "K15"
ElseIf (CtCell.Value = "Reclamation / Abandonment") And (StGraphT.Value = "Gathering, Distribution and Storage") Then
CopyVal = "K16"
ElseIf (CtCell.Value = "Reclamation / Abandonment") And (StGraphT.Value = "Transportation Infratructure") Then
CopyVal = "K17"
ElseIf (CtCell.Value = "Reclamation / Abandonment") And (StGraphT.Value = "Co-Generation Plants") Then
CopyVal = "K18"
ElseIf (CtCell.Value = "Reclamation / Abandonment") And (StGraphT.Value = "Bitumen Production Facilities & Equipment") Then
CopyVal = "K19"
ElseIf (CtCell.Value = "Reclamation / Abandonment") And (StGraphT.Value = "Emulsion Treating & Cleaning (Including Water Treatment)") Then
CopyVal = "K20"
ElseIf (CtCell.Value = "Reclamation / Abandonment") And (StGraphT.Value = "Steam Generation and Distribution") Then
CopyVal = "K21"
ElseIf (CtCell.Value = "Reclamation / Abandonment") And (StGraphT.Value = "Environmental Monitoring") Then
CopyVal = "K22"
ElseIf (CtCell.Value = "Reclamation / Abandonment") And (StGraphT.Value = "Turnaround / Expense Projects") Then
CopyVal = "K23"
ElseIf (CtCell.Value = "Reclamation / Abandonment") And (StGraphT.Value = "Research") Then
CopyVal = "K24"
ElseIf (CtCell.Value = "Reclamation / Abandonment") And (StGraphT.Value = "Other") Then
CopyVal = "K25"
ElseIf (CtCell.Value = "Reclamation / Abandonment") And (StGraphT.Value = "Total Capital Costs") Then
CopyVal = "K27"
ElseIf (CtCell.Value = "Total") And (StGraphT.Value = "Delineation & Development") Then
CopyVal = "L15"
ElseIf (CtCell.Value = "Total") And (StGraphT.Value = "Gathering, Distribution and Storage") Then
CopyVal = "L16"
ElseIf (CtCell.Value = "Total") And (StGraphT.Value = "Transportation Infratructure") Then
CopyVal = "L17"
ElseIf (CtCell.Value = "Total") And (StGraphT.Value = "Co-Generation Plants") Then
CopyVal = "L18"
ElseIf (CtCell.Value = "Total") And (StGraphT.Value = "Bitumen Production Facilities & Equipment") Then
CopyVal = "L19"
ElseIf (CtCell.Value = "Total") And (StGraphT.Value = "Emulsion Treating & Cleaning (Including Water Treatment)") Then
CopyVal = "L20"
ElseIf (CtCell.Value = "Total") And (StGraphT.Value = "Steam Generation and Distribution") Then
CopyVal = "L21"
ElseIf (CtCell.Value = "Total") And (StGraphT.Value = "Environmental Monitoring") Then
CopyVal = "L22"
ElseIf (CtCell.Value = "Total") And (StGraphT.Value = "Turnaround / Expense Projects") Then
CopyVal = "L23"
ElseIf (CtCell.Value = "Total") And (StGraphT.Value = "Research") Then
CopyVal = "L24"
ElseIf (CtCell.Value = "Total") And (StGraphT.Value = "Other") Then
CopyVal = "L25"
ElseIf (CtCell.Value = "Total") And (StGraphT.Value = "Total Capital Costs") Then
CopyVal = "L27"
End If
ElseIf Range("CostType").Value = "CAPEX(M)" Then
Do Until ActiveCell = ""
Range("StProj").Offset(j, 0).Activate
If (CtCell.Value = "PNCB") And (StGraphT.Value = "Delineation & Development") Then
CopyVal = "H15"
ElseIf (CtCell.Value = "PNCB") And (StGraphT.Value = "Mining Equipment") Then
CopyVal = "H16"
ElseIf (CtCell.Value = "PNCB") And (StGraphT.Value = "Utility Plants") Then
CopyVal = "H17"
ElseIf (CtCell.Value = "PNCB") And (StGraphT.Value = "Co-Generation Plants") Then
CopyVal = "H18"
ElseIf (CtCell.Value = "PNCB") And (StGraphT.Value = "Extraction / Tailings") Then
CopyVal = "H19"
ElseIf (CtCell.Value = "PNCB") And (StGraphT.Value = "Upgrading Facilities") Then
CopyVal = "H20"
ElseIf (CtCell.Value = "PNCB") And (StGraphT.Value = "Environmental Monitoring") Then
CopyVal = "H21"
ElseIf (CtCell.Value = "PNCB") And (StGraphT.Value = "Turnaround / Expense Projects") Then
CopyVal = "H22"
ElseIf (CtCell.Value = "PNCB") And (StGraphT.Value = "Research") Then
CopyVal = "H23"
ElseIf (CtCell.Value = "PNCB") And (StGraphT.Value = "Other") Then
CopyVal = "H24"
ElseIf (CtCell.Value = "PNCB") And (StGraphT.Value = "Total Capital Costs") Then
CopyVal = "H26"
ElseIf (CtCell.Value = "Strategic") And (StGraphT.Value = "Delineation & Development") Then
CopyVal = "I15"
ElseIf (CtCell.Value = "Strategic") And (StGraphT.Value = "Mining Equipment") Then
CopyVal = "I16"
ElseIf (CtCell.Value = "Strategic") And (StGraphT.Value = "Utility Plants") Then
CopyVal = "I17"
ElseIf (CtCell.Value = "Strategic") And (StGraphT.Value = "Co-Generation Plants") Then
CopyVal = "I18"
ElseIf (CtCell.Value = "Strategic") And (StGraphT.Value = "Extraction / Tailings") Then
CopyVal = "I19"
ElseIf (CtCell.Value = "Strategic") And (StGraphT.Value = "Upgrading Facilities") Then
CopyVal = "I20"
ElseIf (CtCell.Value = "Strategic") And (StGraphT.Value = "Environmental Monitoring") Then
CopyVal = "I21"
ElseIf (CtCell.Value = "Strategic") And (StGraphT.Value = "Turnaround / Expense Projects") Then
CopyVal = "I22"
ElseIf (CtCell.Value = "Strategic") And (StGraphT.Value = "Research") Then
CopyVal = "I23"
ElseIf (CtCell.Value = "Strategic") And (StGraphT.Value = "Other") Then
CopyVal = "I24"
ElseIf (CtCell.Value = "Strategic") And (StGraphT.Value = "Total Capital Costs") Then
CopyVal = "I26"
ElseIf (CtCell.Value = "Sustaining") And (StGraphT.Value = "Delineation & Development") Then
CopyVal = "J15"
ElseIf (CtCell.Value = "Sustaining") And (StGraphT.Value = "Mining Equipment") Then
CopyVal = "J16"
ElseIf (CtCell.Value = "Sustaining") And (StGraphT.Value = "Utility Plants") Then
CopyVal = "J17"
ElseIf (CtCell.Value = "Sustaining") And (StGraphT.Value = "Co-Generation Plants") Then
CopyVal = "J18"
ElseIf (CtCell.Value = "Sustaining") And (StGraphT.Value = "Extraction / Tailings") Then
CopyVal = "J19"
ElseIf (CtCell.Value = "Sustaining") And (StGraphT.Value = "Upgrading Facilities") Then
CopyVal = "J20"
ElseIf (CtCell.Value = "Sustaining") And (StGraphT.Value = "Environmental Monitoring") Then
CopyVal = "J21"
ElseIf (CtCell.Value = "Sustaining") And (StGraphT.Value = "Turnaround / Expense Projects") Then
CopyVal = "J22"
ElseIf (CtCell.Value = "Sustaining") And (StGraphT.Value = "Research") Then
CopyVal = "J23"
ElseIf (CtCell.Value = "Sustaining") And (StGraphT.Value = "Other") Then
CopyVal = "J24"
ElseIf (CtCell.Value = "Sustaining") And (StGraphT.Value = "Total Capital Costs") Then
CopyVal = "J26"
ElseIf (CtCell.Value = "Reclamation / Abandonment") And (StGraphT.Value = "Delineation & Development") Then
CopyVal = "K15"
ElseIf (CtCell.Value = "Reclamation / Abandonment") And (StGraphT.Value = "Mining Equipment") Then
CopyVal = "K16"
ElseIf (CtCell.Value = "Reclamation / Abandonment") And (StGraphT.Value = "Utility Plants") Then
CopyVal = "K17"
ElseIf (CtCell.Value = "Reclamation / Abandonment") And (StGraphT.Value = "Co-Generation Plants") Then
CopyVal = "K18"
ElseIf (CtCell.Value = "Reclamation / Abandonment") And (StGraphT.Value = "Extraction / Tailings") Then
CopyVal = "K19"
ElseIf (CtCell.Value = "Reclamation / Abandonment") And (StGraphT.Value = "Upgrading Facilities") Then
CopyVal = "K20"
ElseIf (CtCell.Value = "Reclamation / Abandonment") And (StGraphT.Value = "Environmental Monitoring") Then
CopyVal = "K21"
ElseIf (CtCell.Value = "Reclamation / Abandonment") And (StGraphT.Value = "Turnaround / Expense Projects") Then
CopyVal = "K22"
ElseIf (CtCell.Value = "Reclamation / Abandonment") And (StGraphT.Value = "Research") Then
CopyVal = "K23"
ElseIf (CtCell.Value = "Reclamation / Abandonment") And (StGraphT.Value = "Other") Then
CopyVal = "K24"
ElseIf (CtCell.Value = "Reclamation / Abandonment") And (StGraphT.Value = "Total Capital Costs") Then
CopyVal = "K26"
ElseIf (CtCell.Value = "Total") And (StGraphT.Value = "Delineation & Development") Then
CopyVal = "L15"
ElseIf (CtCell.Value = "Total") And (StGraphT.Value = "Mining Equipment") Then
CopyVal = "L16"
ElseIf (CtCell.Value = "Total") And (StGraphT.Value = "Utility Plants") Then
CopyVal = "L17"
ElseIf (CtCell.Value = "Total") And (StGraphT.Value = "Co-Generation Plants") Then
CopyVal = "L18"
ElseIf (CtCell.Value = "Total") And (StGraphT.Value = "Extraction / Tailings") Then
CopyVal = "L19"
ElseIf (CtCell.Value = "Total") And (StGraphT.Value = "Upgrading Facilities") Then
CopyVal = "L20"
ElseIf (CtCell.Value = "Total") And (StGraphT.Value = "Environmental Monitoring") Then
CopyVal = "L21"
ElseIf (CtCell.Value = "Total") And (StGraphT.Value = "Turnaround / Expense Projects") Then
CopyVal = "L22"
ElseIf (CtCell.Value = "Total") And (StGraphT.Value = "Research") Then
CopyVal = "L23"
ElseIf (CtCell.Value = "Total") And (StGraphT.Value = "Other") Then
CopyVal = "L24"
ElseIf (CtCell.Value = "Total") And (StGraphT.Value = "Total Capital Costs") Then
CopyVal = "L26"
End If
End If

Set ws = ActiveSheet
Set wb = Workbooks.Open("P:\ODD\OSD\BO\CARE Cost & Operations Forms _
- CONFIDENTIAL\CARE Reporting by OSR & CSR Project ID\OSR " _
& Proj.value & "\OSR" & Proj.value & " " & CT2.Value & "\OSR" & Proj.value _
& "_" & Yr.Value & "_" & QRT.Value & "_" & CT.Value & ".xls")
wb.Worksheets(SheetVal).Range(CopyVal).Copy
ws.Range("StFill").Offset(j, 0).Paste
wb.Close
j = j + 1
Loop

Application.ScreenUpdating = True

Call Graph1

End Sub

*Andrzejek I see how you are setting the file location as a string (wish I thought of that earlier b4 I wrote it in like 20 times) but I don't understand the part about the immediate window. Like I said I'm brand new. All I know how to do with the immediate window is copy a piece of code in an hit enter at the end of every line and prey it runs. Which it usually doesn’t for me. And I really don't understand what the debug.print does/should do.
 


So are you coding VB 5 0r 6 or

Excel VBA??? if so forum707.

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
I know it is in excel and it is 2007 excel. Does that make it the forum707? If so I'm sry. I really am lost.
 



You have assigned a range
Code:
Set StGraphT = Range("StGraphT")
Then you use that range variable in many statements like...
Code:
        If (CtCell.Value = "PNCB") And (StGraphT.Value = "Delineation & Development") Then
            CopyVal = "H15"
        ElseIf (CtCell.Value = "PNCB") And (StGraphT.Value = "Gathering, Distribution and Storage") Then
            CopyVal = "H16"
        ElseIf (CtCell.Value = "PNCB") And (StGraphT.Value = "Transportation Infratructure") Then
...
Apparently VARIABLE, StGraphT, is a RANGE of cells in your sheet, defined by the NAME StGraphT (not particularly a good practice, but not a problem). What IS the problem, is that StGraphT has MANY VALUES.

I believe that what you intended to do is loop thru that range and compare that value each time...
Code:
  dim r as range
  for each r in range("StGraphT")
    If (CtCell.Value = "PNCB") And ([b]r.Value[/b] = "Delineation & Development") Then
            CopyVal = "H15" 
....
  next
rather than the do loop and activating cells.

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Well the StGraphT is only one cell. The value depends on a user input at the start of the code. There is a UserForm with a list box and the user selects what they want. If they make multiple selections they will be listed in and to the right of StGraphT. In this sub I am only addressing the case where the user has only chosen one value.

I realized half way throught that I was using both the vaiable and the range.value but I am really new at this and figured I had bigger problems lol.

What this part of the program is suppose to do is start at the cell called StProj and depending on the value of StGraphT and CtCell (which is another user input) copy a value from another workbook into the cell beside StProj named StFill. Then I want to program to move down from StProj and depending on te correspondong values, copy a corresponding value into the cell beside IT. So on and soforth until it reaches an empty cell i.e. the end of the list under the StProj cell.
 

Code:
  dim r as range

  for each r in [b]range(Range("StGraphT"), Range("StGraphT").End(xldown))[/b]
    If (CtCell.Value = "PNCB") And (r.Value = "Delineation & Development") Then
            CopyVal = "H15" 
....
  next


Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Will this work if the size is always changing? Because I can't set a specific number of cells for the r range becuse the user may select 1,4,up to 21 items.
 


Please explain exactly what the user selects, in terms of rows and columns, and what you expect to happen as a result of their selection.

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Ok background information. I work for the Alberta Government and I am writing a program that will look at their mess of excel workbooks and create graphs to compare information. Each workbook has information on one particular oil extraction project and it’s costs.

When the user executes the macro a UserForm asks the user to select either capital (CAPEX) or operating (OPEX) costs. Then it will ask if it is a mining or in-situ project. From here it will open either the CAPEX mining, CAPEX in-situ, OPEX mining, or OPEX in-situ UserForm. They are all similar but have different things in the list boxes. In general the UserForm has these parts…
1. A listbox where the user can select one or multiple graph types (i.e. types of costs)
2. A listbox where the user selects ONLY one general type cost to look at (which corresponds to the worksheet within the workbook).
3. A textbox where the user inputs the year they want to look at.
4. And four option buttons where the user selects which quarter they want to see the data up to (this will trigger how many months are put in).
All this information is then going to a user input table I have created. Each thing simply listed in a column and the quarter creating a column that lists Jan-Mar if Q1, Jan-Jun if Q2 ect.

The user is then prompted with an inputbox to input the project numbers they want to see. If they have selected multiple graph types they can only select one project and vis versa. These project numbers are then added to the input table.

Then I have the program read the input table and generate a graph table. This depends on what the user has put in. They want 4 different kinds of graphs CAPEX one project multiple graph types, CAPEX one graph type multiple projects, OPEX one project multiple graph types, and OPEX one graph type multiple projects. I hope I haven’t lost you yet.

So here is the part I am having trouble with. I have four different branches coming off of here depended on the graph type they want. I have written four “fetch” subs, the first being the one I have posted. Each are slightly different but they all go into the P drive with the information in the graph table and pull the corresponding information. At least that is what I want it to do.

From there each fetch will go to its own “graph” Sub to generate the desired graph. I am assuming this will be a whole new set of problems. I plan on trying to use the macro recorder for the most part.

Sry if this isn’t clear. This is the first macro I have ever written and I’m still fuzzy on everything.
 


When the user "select(s) either capital (CAPEX) or operating (OPEX) costs," do they do that in the UserForm, using a control, or do they select something on the worksheet?

In general, cell selections are not the best way to reference ranges and code. Which is why I posted...
Code:
  dim r as range

  for each r in range(Range("StGraphT"), Range("StGraphT").End(xldown))
    If (CtCell.Value = "PNCB") And (r.Value = "Delineation & Development") Then
            CopyVal = "H15" 
....
  next
This code assumes that Range("StGraphT") is the first cell in a column of values, and range(Range("StGraphT"), Range("StGraphT").End(xldown)) defines the range of contiguous cells from Range("StGraphT"), down thru the last cell containing a value, assuming that there are at least TWO comtiguous cells in the range.


Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
The CAPEX OPEX selection is a userform with two buttons.

Ok so if I am understanding this right you are saying not to use the .activate and the (j,0) stuff. And this can be replaced with the for/next and range(Range("StGraphT"), Range("StGraphT").End(xldown))?
 


yes, or if you want to use an index like j...
Code:
j=0
do while (Range("StProj").Offset(j, 0).value <> "")
   if Range("StProj").Offset(j, 0).value = someothervalue then whatever

'.....

   j=j+1
Loop
but you're referencing the cell object rather then SELECTING.

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 

GovNewbi (is that a "Government Newbi"? :) )

[tt]Debug.Print[/tt] is a great way to see what's going on in your code, especially if you want to copy/paste (like a path to your file or an SQL in your query). At any time you can say: "I want to see what is in variable X"

Put the Brake in your code (have your cursor on the line where you want to stop and hit F9, that will highligh your line in BROWN color). To take it out just hit F9 again. The execution of your code will stop on this line and you can step thru your code (F8)

Have fun.

---- Andy
 
GovNewbi does stand for that haha. Most of the names I normaly use were taken. And I figured putting Newbi in the name would help ppl see I don't know much about programming so hopfully they will talk to me like I'm stupid haha.


So this Debug.Print, Where does it print to? (I have done the step through and break so I follow that part)
 


The IMMEDIATE window. View > Immediate

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
oh... ok. I didn't know the immediate window did that I thouhgt it was just for running pieces of code.

By the way it anyone is qurious I did solve my initial problem. After an entire day of looking at the file location I realized I used and underscore to break up the line inside quotations haha. It runs now.
 


I often run SQL and format my code, for ease of understanding...
Code:
    sSQL = "SELECT"
    sSQL = sSQL & "  Qty"
    sSQL = sSQL & ", Current_Op"
    sSQL = sSQL & ", Location"
    sSQL = sSQL & ", sf_cc"
    sSQL = sSQL & ", sf_oper"
    sSQL = sSQL & ", mach_grp"
    sSQL = sSQL & vbLf
    sSQL = sSQL & "FROM FPRPTSAR.MFG_ORDER_INFO"
    sSQL = sSQL & vbLf
    sSQL = sSQL & "WHERE PART_ID like '" & Trim(sPN) & "%'"
    sSQL = sSQL & "  And mfg_ord Not like 'M-%' "
    sSQL = sSQL & "  And mfg_ord Not like '%CMO%' "
    sSQL = sSQL & "  And mfg_ord Not like '%MEGA%' "


Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top