简体   繁体   English

Excel VBA:基于变量选择单元格

[英]Excel VBA: selecting cells base on variables

I am trying to cycle through a row in my excel sheet. 我正在尝试在Excel工作表中循环浏览。 The row moves based on the data that is read into the sheet and the columns are variable and change based on the data. 该行根据读取到工作表中的数据而移动,而列则是可变的,并根据该数据而变化。 All the buttons are generated by another macro and when pressed enter their contents into the row I want to read from. 所有按钮都是由另一个宏生成的,按下后将它们的内容输入到我要读取的行中。 例如

My Problem is that when I try to reference the cells with my variable I always get an error, no matter what I try. 我的问题是,当我尝试用我的变量引用单元格时,无论尝试什么,总是会出错。 The most common error is a 'Type mismatch' 最常见的错误是“类型不匹配”

Sub Save()
Dim RowNum As Integer
Dim LastColumn As Long
Dim ws As Worksheet
Dim MyRange As Range
Dim answer As Integer
Dim i As Integer

Set ws = ActiveWorkbook.Sheets("Main")

answer = MsgBox("Sind Sie sicher, dass Sie speichen möchten?", vbYesNo + vbQuestion, "Sind Sie sicher")
If answer = vbNo Then
    Exit Sub
End If

Application.ScreenUpdating = False

'Finds the location of the button ("Speichen Freigegebene Protokolle") is pressed in order to locate the desired row
RowNum = ws.Buttons(Application.Caller).TopLeftCell.row
'Finds how many columns there are with this data set
LastColumn = ws.Cells(11, ws.Columns.count).End(xlToLeft).Column

'cycles through columns of the row
For i = 7 To LastColumn
    Set MyRange = ws.Range(Cells(RowNum, i), Cells(RowNum + 1, i))

    If MyRange.Value = "Gut" Then  '##Type Mismatch error##
        'Save
    ElseIf MyRange.Value = "Einzelfreigeben" Then
        'Save
    ElseIf MyRange.Value = "Nacharbeit" Then
        'Dont Save
    ElseIf MyRange.Value = "Ausschuss" Then
        'Dont save
    ElseIf MyRange.Value = "" Then
        Err = MsgBox("Fehler! Sie haben eine Protokole zu pruefen vergessen.", vbOKOnly, "Fehler")
        Exit Sub
    End If

Next i
End Sub

Any help would be really appreciated! 任何帮助将非常感激!

The answer already provided by @Scott Craner and @Rory. @Scott Craner和@Rory已经提供了答案。

You can replace all your If and ElseIf s and use a Select Case , like the code below: 您可以替换所有IfElseIf并使用Select Case ,如以下代码所示:

Select Case MyRange.Value
    Case "Gut"
        'Save
    Case "Einzelfreigeben"
        'Save
    Case "Nacharbeit"
        'Dont Save
    Case "Ausschuss"
        'Dont save
    Case ""
        Err = MsgBox("Fehler! Sie haben eine Protokole zu pruefen vergessen.", vbOKOnly, "Fehler")
        Exit Sub
End Select

The answer already provided by @Scott Craner and @Rory. @Scott Craner和@Rory已经提供了答案。 It was as simple as just referencing only the top left cell of the merged cells instead of all cells in the merged cell. 就像只引用合并单元格的左上角单元格而不是合并单元格中的所有单元格一样简单。

    Set MyRange = ws.Range(ws.Cells(RowNum, i), ws.Cells(RowNum, i))

Added the ws. 添加了ws。 before the Cells() was also a good tip to keep the code from changing sheets and workbooks. Cells()之前,它也是防止代码更改工作表和工作簿的好技巧。

Thanks all! 谢谢大家!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM