Problema en selección de rango
Publicado por José Vicente (113 intervenciones) el 03/11/2020 19:22:25
Hola de nuevo, habiendo solucionado ya el tema de las etiquetas del eje X, extrayendo el día, me he dado cuenta que dependiendo del mes que seleccione me añade o me quita días. ¿Puede alguien echarme un cable?. Gracias.
Pongo el código completo del botón donde incluyo el gráfico.
Pongo el código completo del botón donde incluyo el gráfico.
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
Private Sub Guarda_btn_Click(sender As Object, e As EventArgs) Handles Guarda_btn.Click
valor = CStr(fechaIni_tb.Text) 'GUARDAMOS LA PRIMERA FECHA COMO STRING
fin = CStr(fechaFin_tb.Text) 'GUARDAMOS LA ÚLTIMA FECHA COMO STRING
Dim fecha1, fecha2 As Date
fecha1 = Date.Parse(valor)
fecha2 = Date.Parse(fin)
Dim numHojas As Int16 = 0
dias = DateDiff(DateInterval.Day, fecha1, fecha2)
numero = valor.Substring(valor.IndexOf("/") + 1, valor.LastIndexOf("/") - 3) 'EXTRAEMOS EL NÚMERO DE MES
mes = MonthName(numero, True) 'LE PONEMOS NOMBRE AL MES
exApp = New Application
exApp.Workbooks.Open(archivo) 'ABRIMOS EL LIBRO
exApp.Visible = True
numHojas = exApp.Worksheets.Count 'CONTAMOS LAS HOJAS DEL LIBRO
exApp.Range("A2").Select()
exApp.ActiveSheet.Select
Dim PrimeraCell As String = ""
Dim RangoSeleccionado As String = ""
Dim NumCol As Integer = 0
Dim NumRow As Integer = 0
Dim TotalNumCol As Integer = 0
Dim TotalNumRow As Integer = 0
If exApp.Selection.Find(valor) Is Nothing Then
MessageBox.Show("ERROR. NO HAY SELECCIONADO NADA.", "ERROR.", MessageBoxButtons.OK, MessageBoxIcon.Error)
exApp.Workbooks.Close()
exApp.Quit()
exHoja = Nothing
exLibro = Nothing
exApp = Nothing
Exit Sub
Else
exApp.Selection.Find(valor).Select()
PrimeraCell = "$A$" & (exApp.ActiveCell.Row - 1) 'SELECCIONAMOS LA PRIMERA CELDA DEL MES
NumCol = exApp.ActiveCell.Column
'NumRow = exApp.ActiveCell.Row - 2 'FUNCIONA EN MESES DE 30
NumRow = exApp.ActiveCell.Row 'NO FUNCIONA EN MESES DE 30
TotalNumRow = exApp.ActiveSheet.UsedRange.Rows.Count - 2 'NO FUNCIONA EN MESES DE 31
'TotalNumRow = exApp.ActiveSheet.UsedRange.Rows.Count 'FUNCIONA EN MESES DE 31
TotalNumCol = exApp.ActiveSheet.UsedRange.Columns.Count
End If
With exApp
exApp.Worksheets.Add(After:=exApp.Worksheets(numHojas)) ' EL AFTER ES PARA EVITAR CONFUNDIR LAS HOJAS
exApp.Worksheets(1).Select()
If fechaFin_tb.Text = fechaIni_tb.Text Then
' SELECCIONAMOS DESDE LA FECHA DESEADA HASTA EL FINAL
RangoSeleccionado = PrimeraCell & ":" & Chr(64 + TotalNumCol - NumCol + 1) & TotalNumRow
Else
' SELECCIONAMOS EL RANGO DESDE LA FECHA DESEADA
RangoSeleccionado = PrimeraCell & ":" & Chr(64 + TotalNumCol - NumCol + 1) & (NumRow + dias)
End If
.ActiveSheet.Range(RangoSeleccionado).Select()
.Selection.Copy ' COPIAMOS EL CONTENIDO
.ActiveWorkbook.Sheets(numHojas + 1).Select()
.ActiveSheet.Name = mes ' LE CAMBIAMOS EL NOMBRE
.ActiveSheet.Range("A1").Insert()
'TAMAÑO DE PAPEL Y MÁRGENES
.ActiveSheet.PageSetup.PaperSize = Excel.XlPaperSize.xlPaperA4
.ActiveSheet.PageSetup.LeftMargin = 53
.ActiveSheet.PageSetup.RightMargin = 40
.ActiveSheet.PageSetup.TopMargin = 35
.ActiveSheet.PageSetup.BottomMargin = 40
.ActiveSheet.Rows.Item(1).Font.Bold = 1 'NEGRITA
.ActiveSheet.Rows.Item(1).Font.ColorIndex = 49 'COLOR DEL ENCABEZADO
.ActiveSheet.Rows.Item(1).HorizontalAlignment = 3 'ALINEADO DEL ENCABEZADO
.ActiveSheet.Range("A1").Value = " FECHA "
.ActiveSheet.Range("B1").Value = " SISTÓLICA "
.ActiveSheet.Range("C1").Value = " DIASTÓLICA "
.ActiveSheet.Range("D1").Value = " PULSACIONES "
.ActiveSheet.Range("E1").Value = " SATURACIÓN "
.ActiveSheet.Range("A1:E1").Borders.LineStyle = 1
.ActiveSheet.Rows.Item(1).AutoFit()
.ActiveSheet.Range("A1:E1").Cells.Interior.Color = Color.Cyan
.ActiveSheet.Range("A2:E32").HorizontalAlignment = 3
.ActiveSheet.Rows.Font.Size = 12 ' TAMAÑO DE LA FUENTE
.ActiveSheet.Rows.Font.Name = "Adobe Garamond Pro Bold" 'TIPO DE FUENTE
.ActiveSheet.Columns.AutoFit() 'AJUSTE DE LAS COLUMNAS
.ActiveSheet.Range("A2:A32").NumberFormat = "dd/mm/yyyy" 'FORMATO FECHA PARA LA COLUMNA FECHA
.ActiveSheet.Range("A2:A32").Font.ColorIndex = 5 'COLOR DE LA FUENTE DE LA COLUMNA DE FECHAS
.ActiveSheet.UsedRange.Select
'EXTRAEMOS EL DÍA DE LA COLUMNA FECHA
Dim numFilas As Int16 = .ActiveSheet.UsedRange.SpecialCells(Excel.XlCellType.xlCellTypeLastCell).Row
For con As Int16 = 2 To numFilas
Dim con1 As Int16 = 2
.ActiveSheet.Range("F1:F" & con).FormulaLocal = "=DIA" & "(A" & con1 & ")"
con += 1
con1 += 1
Next
.ActiveSheet.Columns("F").Hidden = False 'OCULTAMOS LA COLUMNA CON LA SELECCIÓN DE DÍAS
'CREAMOS EL GRÁFICO
Dim chartPage As Chart
Dim exCharts As ChartObjects = .ActiveSheet.ChartObjects
Dim myChart As ChartObject = exCharts.Add(462, 2, 416, 400)
Dim ejeX, ejeY As Excel.Axes
chartPage = myChart.Chart
'DAMOS VALORES A LOS EJES DE LA GRÁFICA
Dim coleccion As SeriesCollection = chartPage.SeriesCollection
Dim serie1, serie2, serie3, serie4 As Excel.Series
serie1 = coleccion.NewSeries()
serie1.Name = .ActiveSheet.Range("B1").Value
serie1.XValues = .ActiveSheet.Range("F1:F33")
serie1.Values = .ActiveSheet.Range("B2:B32")
serie2 = coleccion.NewSeries()
serie2.Name = .ActiveSheet.Range("C1").Value
serie2.XValues = .ActiveSheet.Range("F1:F33")
serie2.Values = .ActiveSheet.Range("C2:C32")
serie3 = coleccion.NewSeries()
serie3.Name = .ActiveSheet.Range("D1").Value
serie3.XValues = .ActiveSheet.Range("F1:F33")
serie3.Values = .ActiveSheet.Range("D2:D32")
serie4 = coleccion.NewSeries()
serie4.Name = .ActiveSheet.Range("E1").Value
serie4.XValues = .ActiveSheet.Range("F1:F33")
serie4.Values = .ActiveSheet.Range("E2:E32")
chartPage.ChartType = Excel.XlChartType.xlXYScatterLinesNoMarkers
chartPage.ApplyDataLabels(Type:=XlDataLabelsType.xlDataLabelsShowNone, LegendKey:=False, HasLeaderLines:=True)
'DAMOS FORMATO A LOS EJES DEL GRÁFICO
With chartPage
ejeX = .Axes(, XlAxisGroup.xlPrimary)
ejeX.Item(XlAxisType.xlCategory).HasTitle = True
ejeX.Item(XlAxisType.xlCategory).AxisTitle.Characters.Text() = "DÍAS"
ejeX.Item(XlAxisType.xlCategory).HasMajorGridlines = True
ejeX.Item(XlAxisType.xlCategory).HasMinorGridlines = True
ejeX.Item(XlAxisType.xlCategory).MaximumScale = 31
ejeX.Item(XlAxisType.xlCategory).MinimumScale = 1
ejeX.Item(XlAxisType.xlCategory).TickLabels.Orientation = XlTickLabelOrientation.xlTickLabelOrientationAutomatic
ejeY = .Axes(, XlAxisGroup.xlPrimary)
ejeY.Item(XlAxisType.xlValue).HasTitle = True
ejeY.Item(XlAxisType.xlValue).AxisTitle.Characters.Text() = "VALORES"
ejeY.Item(XlAxisType.xlValue).HasMajorGridlines = True
ejeY.Item(XlAxisType.xlValue).HasMinorGridlines = True
ejeY.Item(XlAxisType.xlValue).MaximumScale = 100
ejeY.Item(XlAxisType.xlValue).LogBase = 10
End With
chartPage.HasTitle = True
chartPage.ChartTitle.Characters.Text = "TENSIÓN MENSUAL"
chartPage.ChartTitle.Position = XlChartElementPosition.xlChartElementPositionAutomatic
chartPage.HasLegend = True
chartPage.Legend.Position = XlLegendPosition.xlLegendPositionRight
.ActiveSheet.Range("E1").End(Excel.XlDirection.xlDown).Select() 'SELECCIONAMOS LA ÚLTIMA CELDA CON DATOS HOJA2
.ActiveWorkbook.Sheets(1).Select() 'SELECCIONAMOS LA HOJA 1 DEL LIBRO MODIFICADO
.ActiveSheet.Range("E1").End(Excel.XlDirection.xlDown).Select() 'SELECCIONAMOS LA ÚLTIMA CELDA CON DATOS HOJA1
If System.IO.File.Exists(archivo) = True Then
'SI EL ARCHIVO EXISTE LO BORRAMOS Y GUARDAMOS EL NUEVO
File.Copy(archivo, archivo_ltl, True)
System.IO.File.Delete(archivo_ltl)
Else
'SI NO EXISTE LO GUARDAMOS
exLibro.SaveAs(archivo)
End If
.ActiveWorkbook.Save() ' GRABAMOS LOS CAMBIOS EN EL LIBRO
exApp.Workbooks.Close()
exApp.Quit()
End With
exHoja = Nothing
exLibro = Nothing
exApp = Nothing
Dim P As System.Diagnostics.Process ' CERRAMOS TODOS LOS PROCESOS DE EXCEL ABIERTOS
Try
For Each P In System.Diagnostics.Process.GetProcesses
If P.ProcessName.ToUpper Like "*EXCEL*" Then
P.Kill()
End If
Next
Catch
End Try
GC.WaitForPendingFinalizers()
GC.Collect()
MessageBox.Show("FICHERO MODIFICADO Y GUARDADO", "PERFECTO.", MessageBoxButtons.OK, MessageBoxIcon.Information)
End Sub
Valora esta pregunta


0