PDA

View Full Version : If Problem



Ann_BBO
08-07-2007, 11:55 PM
If Range("H8").Value = True And Range("J8").Value > Range("I8").Value And Range("K8").Value = False Then
z = Range("I8").Value
End If
For x = z To ListBox1.ListCount - 1
Application.ScreenUpdating = False
Set Tgt = ActiveSheet
Set wbSource = Workbooks.Open(Filename:=ListBox1.List(x))
Set Source = wbSource.Sheets(1).Columns(1)
If Range("H8").Value = True And Range("J8").Value > Range("I8").Value And Range("K8").Value = False Then
y = (x + (Tgt.Range("I8").Value)) * 38
End If

y = x * 38
.
.
Next x
From the above vba, why it cannot run the y = (x + (Tgt.Range("I8").Value)) * 38 even the condition is true. It run the y = x * 38 only

Thanks

JimmyTheHand
08-08-2007, 12:05 AM
Maybe its the condition.
I'm not sure about the priorities, but maybe you should try this:
If (Range("H8").Value = True) And (Range("J8").Value > Range("I8").Value) And (Range("K8").Value = False) Then
y = (x + (Tgt.Range("I8").Value)) * 38
End If

Anyway, what is the error message?

Jimmy

Charlize
08-08-2007, 12:07 AM
What happens after the if ... maybe use an else too.

Ann_BBO
08-08-2007, 12:31 AM
To Jimmy
It doesn't work. It still run y = x * 38 only!

Anyway thanks for your help

Bob Phillips
08-08-2007, 01:12 AM
It might be running it, but it gets overwritten immediately afterwards anyway. As Charlize said, add an else



If Range("H8").Value = True And Range("J8").Value > Range("I8").Value And Range("K8").Value = False Then
y = (x + (Tgt.Range("I8").Value)) * 38
Else
y = x * 38
End If

Ann_BBO
08-08-2007, 01:26 AM
I had try it the Else before
But it still not work.
It only run y = x * 38

I don't know why??:mkay

Bob Phillips
08-08-2007, 01:29 AM
Then it must be down to the values in those cells. Have you tried stepping through it and finding the values?

mdmackillop
08-08-2007, 07:48 AM
Given that Else is corrected, I'm dubious about the specification of Tgt. It could give you the same result all the time if you are closing your workbooks later in the loop, or get a value from that last opened.
I'm not clear either from which sheet you are getting your values. It looks like H8, J8 etc. will change with each loop. Is this intentional? You should really add a qualification to make this clear and avoid potential errors.

Ann_BBO
08-08-2007, 06:01 PM
The modified code is shown below but not work:
Private Sub cmdShowdata_Click()
Dim Tgt As Worksheet ' The name of the active.sheet
Dim Source As Range ' The name of the Filepaths, sheets, column in the source.workbook
Dim wbSource As Workbook ' The name of the source.workbooks
Dim cel As Range ' The individual cell of the active.sheet
Dim Rng As Range ' The range of the source.workbooks
Dim c As Range ' The target range of the source.workbooks
Dim x As Long ' The variable of the listBox1 sequence
Dim y As Integer ' The variable of the range distance between the Data range
Dim z As Integer
Dim a As Integer

' Checking whether has the file(s) in ListBox
If ListBox1.ListCount = False Then ' If no file(s) in the ListBox,
MsgBox "Please Input the File(s) in the ListBox" ' Show the message and Exit the Function
Exit Sub
Else


' Open the File(s) according the ListBox1 sequence
If Range("H8").Value = True And Range("J8").Value > Range("I8").Value And Range("K8").Value = False Then
z = Range("I8").Value
a = x * 38

Else

If Range("H8").Value = True And Range("J8").Value < Range("I8").Value And Range("K8").Value = True Then
z = 0
a = (x + (Tgt.Range("I8").Value)) * 38

Else

If Range("H8").Value = " " Then
z = 0
a = x * 38
End If

End If
End If

For x = z To ListBox1.ListCount - 1
Application.ScreenUpdating = False
Set Tgt = ActiveSheet
Set wbSource = Workbooks.Open(Filename:=ListBox1.List(x))
Set Source = wbSource.Sheets(1).Columns(1)
y = a

' Show the filepath(s) according the ListBox1 sequence
Tgt.Cells((y + 49), 3).Value = Me.ListBox1.List(x)
With Tgt
.Activate
' Clear old data
'(a)115 Vac/60Hz data clear
Range(.Cells((y + 33), 2), .Cells((y + 37), 3)).ClearContents
Range(.Cells((y + 33), 5), .Cells((y + 37), 5)).ClearContents
'(b)117, 129, 96 Vac/60Hz data clear
Range(.Cells((y + 33), 10), .Cells((y + 39), 10)).ClearContents
Range(.Cells((y + 33), 11), .Cells((y + 33), 11)).ClearContents

' Change the name to obey the data structure
'(a)115 Vac/60Hz corrsponding data change
Cells((y + 33), 1).Value = "115 Vac 60 Hz / 400 mAdc"
Cells((y + 34), 1).Value = "115 Vac 60 Hz / 300 mAdc"
Cells((y + 35), 1).Value = "115 Vac 60 Hz / 200 mAdc"
Cells((y + 36), 1).Value = "115 Vac 60 Hz / 100 mAdc"
Cells((y + 37), 1).Value = "115 Vac 60 Hz / 0 mAdc"
'(b)117, 129, 96 Vac/60Hz corrsponding data change
Cells((y + 33), 8).Value = "117 Vac 60 Hz / 400 mAdc"
Cells((y + 34), 8).Value = "129 Vac 60 Hz / 400 mAdc"
Cells((y + 35), 8).Value = "96 Vac 60 Hz / 400 mAdc"
Cells((y + 36), 8).Value = "117 Vac 60 Hz / 100 mAdc"
Cells((y + 37), 8).Value = "129 Vac 60 Hz / 100 mAdc"
Cells((y + 38), 8).Value = "96 Vac 60 Hz / 100 mAdc"
Cells((y + 39), 8).Value = "117 Vac 60 Hz / 0 mAdc"

'(1)Loop through names in column A with 115 Vac/60Hz type and extra the data in source.workbooks
For Each cel In Range(Cells((y + 33), 1), Cells((y + 37), 1))
If Not cel = "" Then
Set c = Source.Range("A3")
Set Rng = Nothing
Do While c.Row < Source.Range("A" & Source.Rows.Count).End(xlUp).Row
If c = cel Then
If Rng Is Nothing Then Set Rng = c.Offset(1)
Set Rng = Union(Rng, Range(c.Offset(1), c.Offset(1).End(xlDown)))
Set c = c.Offset(1).End(xlDown).Offset(1)
Else
Set c = c.Offset(1)
End If
Loop
' Extra the data to the active.sheet in the suitable range
cel.Offset(, 1) = Application.Average(Rng.Offset(, 7).End(xlDown)) / 1000
cel.Offset(, 2) = Application.Average(Rng.Offset(, 8).End(xlDown))
cel.Offset(, 4) = Application.Average(Rng.Offset(, 9).End(xlDown))
End If
Next
'(2)Loop through names in column A with the Single Data (Vpp & Tamb) in 117 Vac 60 Hz type
For Each cel In Cells((y + 33), 8)
If cel = "117 Vac 60 Hz / 400 mAdc" Then
Set c = Source.Find(cel)
Set Rng = Range(c.Offset(1), c.Offset(1).End(xlDown))
'Extra the Vpp and Tamb data
cel.Offset(, 3) = Application.Average(Rng.Offset(, 10))
cel.Offset(12, -2) = Application.Average(Rng.Offset(, 12))
End If
Next
'(3)Loop through names in column A with the Vpp in 117, 129, 96 Vac 60 Hz type
For Each cel In Range(Cells((y + 33), 8), Cells((y + 39), 8))
If Not cel = "" Then
Set c = Source.Range("A3")
Set Rng = Nothing
Do While c.Row < Source.Range("A" & Source.Rows.Count).End(xlUp).Row
If c = cel Then
If Rng Is Nothing Then Set Rng = c.Offset(1)
Set Rng = Union(Rng, Range(c.Offset(1), c.Offset(1).End(xlDown)))
Set c = c.Offset(1).End(xlDown).Offset(1)
Else
Set c = c.Offset(1)
End If
Loop
' Extra the data to the active.sheet in the suitable range
cel.Offset(, 2) = Application.Average(Rng.Offset(, 8))
End If
' Change sheet name into Analysis type
Next
.Name = Range("C2").Value
End With

' Refill the original name into range
Cells((y + 33), 1).Value = 0.4
Cells((y + 34), 1).Value = 0.3
Cells((y + 35), 1).Value = 0.2
Cells((y + 36), 1).Value = 0.1
Cells((y + 37), 1).Value = 0

Cells((y + 33), 8).Value = "117 Vac"
Cells((y + 34), 8).Value = "129 Vac"
Cells((y + 35), 8).Value = "96 Vac"
Cells((y + 36), 8).Value = "117 Vac"
Cells((y + 37), 8).Value = "129 Vac"
Cells((y + 38), 8).Value = "96 Vac"
Cells((y + 39), 8).Value = "117 Vac"

' Update the data into active.sheet
wbSource.Close False
Application.ScreenUpdating = True
Next x

' Complete the Analysis and Show the Message
MsgBox "Analysis Completed!!"
End If
Range("H8").Value = cmdShowdata.TakeFocusOnClick
Range("I8").Value = Range("F6").Value
Range("J8").Value = 0
Me.ListBox1.ForeColor = RGB(0, 0, 0)

Thanks

mdmackillop
08-09-2007, 12:07 AM
Have a look at the following for your initial test.
Dim test As Boolean
test = Range("H8").Value = True And Range("J8").Value > Range("I8").Value _
And Range("K8").Value = False

' Open the File(s) according the ListBox1 sequence
If test = True Then
z = Range("I8").Value
a = x * 38
Else
If Range("H8").Value = " " Then
z = 0
a = x * 38
Else
z = 0
a = (x + (Tgt.Range("I8").Value)) * 38
End If
End If
I note that x is given no value in your code
Are you really looking for a space " " in H8, or an empty cell ""?

Ann_BBO
08-09-2007, 12:11 AM
To mdmackillop

Thanks for your help!! I had solved this problem by other way. !!

Regrads
Ann