无法增加一个已定义的变量

时间:2015-10-09 08:18:45

标签: excel vba excel-vba

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

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

类别全部位于第2行,并且从列B到类别结束时。我的代码打算做的是,从预定义列(绝对值)中分离,按类别编辑项目的数据。它在第一列中表现得非常好,但是当我向预定义列值添加1时,不会发生这种情况,并且它会永远停留在您开始的列上。

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

4 个答案:

答案 0 :(得分:1)

您似乎无法在任何地方定义columnaBase。

所以只需添加

Dim columnaBase As Integer

到开头,添加变量。

答案 1 :(得分:1)

要真正测试Msgbox输出是否正确,您需要添加行

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

whatever = celdaCategoria.Value
MsgBox (whatever)

真正测试+1是否有效

答案 2 :(得分:1)

当您使用循环前定义的单元格对象 celdaInterseccion时,即使在循环期间增加ColumnaBase,也不会影响对象celdaInterseccion有了这个改变。

你的测试停止循环总是一样的,你将永远陷入那个循环。

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

以下是修正后的代码:

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

答案 3 :(得分: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

问题是celdaCategoria已在您增加columnaBase之前设置。这两个变量完全相互独立。

如果要查看增量的效果,则需要使用新的celdaCategoria值将Range对象集更新为columnaBase

columnaBase = columnaBase + 1

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

哪个应输出正确的值。