无法增加一个定义的variables

Sub Modificador_BD_EQUIPOS_Y_HERRAMIENTAS() Dim titulo_codigoFila As String Dim titulo_operacion As String Dim mensaje_codigoFila As String Dim mensaje_operacion As String Dim codigoFila As Integer Dim modificacion As String titulo_codigoFila = "Modificar Ítem" mensaje_codigoFila = "Por favor, introduce el código del ítem" codigoFila = InputBox(mensaje_codigoFila, titulo_codigoFila) 'Gathers the code of the item, which is just the row the item's on columnaBase = 3 Set celdaCategoria = ActiveSheet.Cells(2, columnaBase) Set celdaInterseccion = ActiveSheet.Cells(codigoFila, columnaBase) 'defines the category cell, and the intersection cell, which is just the coordinates of the items data 'Do While celdaInterseccion.Value <> "" -- I COMMENTED OUT THE WHILE LOOP SO I WONT CRASH EXCEL titulo_operacion = Application.WorksheetFunction.Proper(celdaCategoria.Value) + " del Ítem" mensaje_operacion = "Introduce la información del campo " + UCase(celdaCategoria.Value) + ". El valor que deseas modificar es: " + celdaInterseccion.Value modificacion = InputBox(mensaje_operacion, titulo_operacion) 'shows an inputbox with info taken from the category cell and the item's current data for the category If Not modificacion = "" Then celdaInterseccion.Value = modificacion End If 'if the input is not an empty string, it overwrites the item's data columnaBase = columnaBase + 1 'the problem is HERE - it doesnt seem to add 1 back to columnaBase 'Loop whatever = celdaCategoria.Value MsgBox (whatever) 'made this last part so as to confirm if it adds 1 to columnaBase's value End Sub 

这是我为修改数据库中的项目数据而编写的代码。 数据库非常简单; 在右边的列中具有关于它们的某些信息的项目列表,在每一列顶上的数据类别的名称(例如在“date”列下面列出的是某些事物被购买的date)。

这些类别都位于Nº2行,每当类别结束时,从B列开始。 我的代码打算做的是,从一个预定义的列(绝对值)中分离,编辑一个项目的数据,按类别分类。 这对于第一列来说非常棒,但是当我将预定义的列值加1时,不知何故,它不会发生,而且它永远停留在开始的列上。

我完全不知道为什么地球上它不起作用。 注意我注释了While循环; 坠毁的Excel几次试图解决这个…只是一对夫妇:)

正如您在循环之前使用您定义的Cell对象 celdaInterseccion ,即使您在循环中增加了ColumnaBase ,也不会影响具有该更改的对象celdaInterseccion

如果你的testing停止循环将永远是相同的,你将永远卡在这个循环。

所以你只需要在循环结束处添加Set celdaInterseccion = ActiveSheet.Cells(codigoFila, columnaBase)来更新Cell对象并更改被testing的单元格。

这是你的代码与更正:

 Sub Modificador_BD_EQUIPOS_Y_HERRAMIENTAS() Dim titulo_codigoFila As String Dim titulo_operacion As String Dim mensaje_codigoFila As String Dim mensaje_operacion As String Dim codigoFila As Integer Dim modificacion As String titulo_codigoFila = "Modificar Ítem" mensaje_codigoFila = "Por favor, introduce el código del ítem" 'Gathers the code of the item, which is just the row the item's on codigoFila = InputBox(mensaje_codigoFila, titulo_codigoFila) columnaBase = 3 'defines the category cell, and the intersection cell, which is just the coordinates of the items data Set celdaCategoria = ActiveSheet.Cells(2, columnaBase) Set celdaInterseccion = ActiveSheet.Cells(codigoFila, columnaBase) Do While celdaCategoria.Value <> "" '-- I COMMENTED OUT THE WHILE LOOP SO I WONT CRASH EXCEL titulo_operacion = Application.WorksheetFunction.Proper(celdaCategoria.Value) & " del Ítem" mensaje_operacion = "Introduce la información del campo " + UCase(celdaCategoria.Value) & ". El valor que deseas modificar es: " & celdaInterseccion.Value 'shows an inputbox with info taken from the category cell and the item's current data for the category modificacion = InputBox(mensaje_operacion, titulo_operacion) If modificacion <> "" Then 'if the input is not an empty string, it overwrites the item's data celdaInterseccion.Value = modificacion End If columnaBase = columnaBase + 1 'the problem WAS here : 'you need to reSet the Cell Object that you are working with to continue looping properly Set celdaCategoria = ActiveSheet.Cells(2, columnaBase) Set celdaInterseccion = ActiveSheet.Cells(codigoFila, columnaBase) 'made this last part so as to confirm if it adds 1 to columnaBase's value 'Uncomment to check that it is working 'MsgBox celdaCategoria.Value Loop End Sub 

你似乎并没有在任何地方定义columnaBase。

所以只需添加

 Dim columnaBase As Integer 

到一开始,你添加你的variables。

要真正testingMsgbox输出是正确的,你需要添加行

 Set celdaCategoria = ActiveSheet.Cells(2, columnaBase) Set celdaInterseccion = ActiveSheet.Cells(codigoFila, columnaBase) 

在线之前

 whatever = celdaCategoria.Value MsgBox (whatever) 

真正testing+1是否有效

  columnaBase = columnaBase + 1 '<~~ add 1 to columnaBase 'the problem is HERE - it doesnt seem to add 1 back to columnaBase 'Loop whatever = celdaCategoria.Value '<~~ read value of celdaCategora 

问题是你增加了columnaBase之前已经设置了columnaBase 。 这两个variables是完全独立的。

如果您想查看增量的效果,则需要使用新的columnaBase值将Range对象设置为celdaCategoria


 columnaBase = columnaBase + 1 Set celdaCategoria = Cells(2, columnaBase) whatever = celdaCategoria.Value 

哪个应该输出正确的值。