Ayuda con condición CStr
Publicado por Carlos (3 intervenciones) el 10/06/2016 10:40:49
Muy buenas,
Tengo un problema con una macro de Excel que no consigo solucionar. Mediante Visual Basic para Excel realizo consultas a la base de datos (que está en SQL). En Excel cargo toda la información de la base de datos mediante consultas con macros. Pues bien, tengo lo siguiente:
Donde quiero que se cumpla:
Pero no lo hace, no aparece. Es la única referencia que no aparece.
¿Alguna idea?
Gracias.
Tengo un problema con una macro de Excel que no consigo solucionar. Mediante Visual Basic para Excel realizo consultas a la base de datos (que está en SQL). En Excel cargo toda la información de la base de datos mediante consultas con macros. Pues bien, tengo lo siguiente:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
Option Explicit
Dim oConexion As ADODB.Connection
Sub ConnectDB()
On Error GoTo err
Set oConexion = New ADODB.Connection
oConexion.Open "Provider=SQLOLEDB.1;Password=bandoiberica2015b;Integrated Security=SSPI;Persist Security Info=True;" & _
"User ID=Administrador;" & _
"Initial Catalog=BANDOIBERICA_2015;" & _
"Data Source=bandoserver3" 'ip o nombre del servidor
'MsgBox "Connexió establerta.", vbInformation, "1"
Exit Sub
err:
MsgBox "Error de connexió a la Base de dades: " & err.Description, vbInformation, ActiveWorkbook.Name
End Sub
Private Sub cargardatosCARGASSECUNDARIAS()
'***************
Dim ulti As Long
Dim Cadena As String
Dim Filas As Single, MiRango As Object
Set MiRango = Range(ActiveCell.Offset(1, 0), ActiveCell.Offset(Filas, 0))
'***************
ConnectDB
Dim fila As Integer
Dim columna As String
Dim rs As ADODB.Recordset
'Dim RsPreu As ADODB.Recordset
fila = 10
columna = "A"
Set rs = New ADODB.Recordset
'Set RsPreu = New ADODB.Recordset
rs.Open "SELECT [Bando Ibérica, S_A_$Item].[Unit Cost], [Bando Ibérica, S_A_$Item Ledger Entry].[Global Dimension 2 Code], [Bando Ibérica, S_A_$Item Ledger Entry].[Item Category Code], " & _
" [Bando Ibérica, S_A_$Item Ledger Entry].[Item No_], [Bando Ibérica, S_A_$Item].Description, [Bando Ibérica, S_A_$Item].[Tipo medida base], " & _
" [Bando Ibérica, S_A_$Item].[Medida base], SUM([Bando Ibérica, S_A_$Item Ledger Entry].Quantity) AS STOCKCALCULADO " & _
" FROM [Bando Ibérica, S_A_$Item Ledger Entry] INNER JOIN " & _
" [Bando Ibérica, S_A_$Item] ON [Bando Ibérica, S_A_$Item Ledger Entry].[Item No_] = [Bando Ibérica, S_A_$Item].No_ " & _
" WHERE ([Bando Ibérica, S_A_$Item Ledger Entry].[Global Dimension 2 Code] = '300') OR " & _
"([Bando Ibérica, S_A_$Item Ledger Entry].[Global Dimension 2 Code] = '600') OR " & _
"([Bando Ibérica, S_A_$Item Ledger Entry].[Global Dimension 2 Code] = '700') OR " & _
"([Bando Ibérica, S_A_$Item Ledger Entry].[Global Dimension 2 Code] = '701') OR " & _
"([Bando Ibérica, S_A_$Item Ledger Entry].[Global Dimension 2 Code] = '780') OR " & _
"([Bando Ibérica, S_A_$Item Ledger Entry].[Global Dimension 2 Code] = '800') OR " & _
"([Bando Ibérica, S_A_$Item Ledger Entry].[Global Dimension 2 Code] = '830') OR " & _
"([Bando Ibérica, S_A_$Item Ledger Entry].[Global Dimension 2 Code] = '840') OR " & _
"(([Bando Ibérica, S_A_$Item Ledger Entry].[Global Dimension 2 Code] = '602') AND ([Bando Ibérica, S_A_$Item Ledger Entry].[Item Category Code] = 'BFGEN') ) OR " & _
"(([Bando Ibérica, S_A_$Item Ledger Entry].[Global Dimension 2 Code] = '880') AND ([Bando Ibérica, S_A_$Item Ledger Entry].[Item Category Code] = 'TN10') AND ([Bando Ibérica, S_A_$Item Ledger Entry].[Item Category Code] = 'TN15') ) OR " & _
"([Bando Ibérica, S_A_$Item Ledger Entry].[Global Dimension 2 Code] = '503') " & _
"GROUP BY [Bando Ibérica, S_A_$Item].[Tipo medida base],[Bando Ibérica, S_A_$Item].[Medida base], [Bando Ibérica, S_A_$Item].[Unit Cost],[Bando Ibérica, S_A_$Item Ledger Entry].[Global Dimension 2 Code], " & _
"[Bando Ibérica, S_A_$Item Ledger Entry].[Item Category Code],[Bando Ibérica, S_A_$Item Ledger Entry].[Item No_] , [Bando Ibérica, S_A_$Item].Description " & _
"ORDER BY [Bando Ibérica, S_A_$Item Ledger Entry].[Global Dimension 2 Code], [Bando Ibérica, S_A_$Item Ledger Entry].[Item Category Code], " & _
"[Bando Ibérica, S_A_$Item Ledger Entry].[Item No_] ", oConexion, adOpenStatic, adLockReadOnly
Do While Not rs.EOF
'If rs("Global Dimension 2 Code") = "600" Then
' If Left(rs("Item No_"), 5) = "B2CBR" Or Left(rs("Item No_"), 5) = "ROUND" Then
' Range("I" & fila) = rs("Unit Cost")
'RsPreu.Open "select campprecio from tablalaquesea where codproducto='" & rs("Item No_") & "' order by lafechaquesea ", oConexion, adOpenStatic, adLockReadOnly
'RsPreu.MoveLast
'Range("A" & fila) = RsPreu("camprecio")
' Range("J" & fila) = rs("Global Dimension 2 Code")
' Range("K" & fila) = rs("Item Category Code")
' Range("L" & fila) = rs("Item No_")
' Range("M" & fila) = rs("description")
' Range("N" & fila) = rs("STOCKCALCULADO")
' fila = fila + 1
' Else
' End If
If rs("Global Dimension 2 Code") = "503" Then
If Val(Right(rs("Item No_"), 2)) >= 10 Then
If Range("L" & fila) = rs("Item No_") Then
' si el producte és el mateix, no el carrega.
Range("N" & fila) = rs("STOCKCALCULADO")
fila = fila + 1
' si el producte és el mateix, no el carrega.
Range("N" & fila) = rs("STOCKCALCULADO")
fila = fila + 1
Else
Range("A" & fila + 1, "z" & fila + 1).EntireRow.Insert (xlShiftDown)
Range("C" & fila) = "1" 'original, primera carga
Range("D" & fila) = "0" 'no grabada
Range("G" & fila) = rs("Tipo medida base")
Range("H" & fila) = rs("Medida base")
Range("I" & fila) = rs("Unit Cost")
'RsPreu.Open "select campprecio from tablalaquesea where codproducto='" & rs("Item No_") & "' order by lafechaquesea ", oConexion, adOpenStatic, adLockReadOnly
'RsPreu.MoveLast
'Range("A" & fila) = RsPreu("camprecio")
Range("J" & fila) = rs("Global Dimension 2 Code")
Range("K" & fila) = rs("Item Category Code")
Range("L" & fila) = rs("Item No_")
Range("M" & fila) = rs("description")
Range("N" & fila) = rs("STOCKCALCULADO")
'Range("O" & fila) = ""
fila = fila + 1
' Filas = ActiveCell.Value2
' MiRango.EntireRow.Insert (xlShiftDown)
' ActiveCell.Offset(0, -1).Copy Destination:=MiRango.Offset(-Filas, -1)
End If
Else
End If
Else
If rs("Global Dimension 2 Code") <> "880" Then
If rs("Global Dimension 2 Code") <> "530" Then
If Range("L" & fila) = rs("Item No_") Then 'poden faltar camps a comparar
' si el producte és el mateix, no el carrega.
Range("N" & fila) = rs("STOCKCALCULADO")
fila = fila + 1
Else
Range("A" & fila, "z" & fila).EntireRow.Insert (xlShiftDown)
Range("C" & fila) = "1" 'original, primera carga
Range("D" & fila) = "0" 'no grabada
Range("G" & fila) = rs("Tipo medida base")
Range("H" & fila) = rs("Medida base")
Range("I" & fila) = rs("Unit Cost")
Range("J" & fila) = rs("Global Dimension 2 Code")
Range("K" & fila) = rs("Item Category Code")
Range("L" & fila) = rs("Item No_")
Range("M" & fila) = rs("description")
Range("N" & fila) = rs("STOCKCALCULADO")
'Range("O" & fila) = ""
fila = fila + 1
End If
End If
End If
If rs("Global Dimension 2 Code") = "880" Then
If Right(CStr(rs.Fields("Item No_").Value), 4) = "100T" Or Right(CStr(rs.Fields("Item No_").Value), 4) = "100K" Then
If Range("L" & fila) = rs("Item No_") Then
' si el producte és el mateix, no el carrega.
Range("N" & fila) = rs("STOCKCALCULADO")
fila = fila + 1
' si el producte és el mateix, no el carrega.
Range("N" & fila) = rs("STOCKCALCULADO")
fila = fila + 1
Else
Range("A" & fila + 1, "z" & fila + 1).EntireRow.Insert (xlShiftDown)
Range("C" & fila) = "1" 'original, primera carga
Range("D" & fila) = "0" 'no grabada
Range("G" & fila) = rs("Tipo medida base")
Range("H" & fila) = rs("Medida base")
Range("I" & fila) = rs("Unit Cost")
'RsPreu.Open "select campprecio from tablalaquesea where codproducto='" & rs("Item No_") & "' order by lafechaquesea ", oConexion, adOpenStatic, adLockReadOnly
'RsPreu.MoveLast
'Range("A" & fila) = RsPreu("camprecio")
Range("J" & fila) = rs("Global Dimension 2 Code")
Range("K" & fila) = rs("Item Category Code")
Range("L" & fila) = rs("Item No_")
Range("M" & fila) = rs("description")
Range("N" & fila) = rs("STOCKCALCULADO")
'Range("O" & fila) = ""
fila = fila + 1
' Filas = ActiveCell.Value2
' MiRango.EntireRow.Insert (xlShiftDown)
' ActiveCell.Offset(0, -1).Copy Destination:=MiRango.Offset(-Filas, -1)
End If
Else
End If
End If
End If
rs.MoveNext
Loop
rs.Close
Set rs = Nothing
End Sub
Sub Sincro()
End Sub
Donde quiero que se cumpla:
1
2
If rs("Global Dimension 2 Code") = "880" Then
If Right(CStr(rs.Fields("Item No_").Value), 4) = "100T" Or Right(CStr(rs.Fields("Item No_").Value), 4) = "100K" Then
Pero no lo hace, no aparece. Es la única referencia que no aparece.
¿Alguna idea?
Gracias.
Valora esta pregunta


0