0
Sub Button1Click()

Dim temp As Worksheet
Set temp = ThisWorkbook.Worksheets("Input")

Dim tempAct As String
Dim tempCC As String
Dim tempRan As String

tempAct = Range("B1").Value
tempCC = Range("B2").Value
tempRan = Range("B3").Value

If temp.Range("B1:B3").Value = "" Then
MsgBox ("Blank Input!")

ElseIf temp.Range("B1:B3").Value <> "" Then
temp.Range("C1").Value = tempAct
temp.Range("C2").Value = tempCC
tmep.Range("C3").Value = tempRan
End If
End Sub

I am trying to make a simple code when pressing this button, it will simply copy the cell value onto the other cell, and also any idea how to put the input result in the listbox?

I am getting Type Missmatch error when generating this macro.

4

1 回答 1

0

我相信由于这条线而发生类型不匹配

If temp.Range("B1:B3").Value = "" Then

我不相信您可以将条件应用于范围内的所有单元格。如果您的范围是单个单元格,即Range("B1").Value,则条件语句在语法上是正确的,但当然不会给您想要的结果。

相反,您可以使用For循环来测试每个单元格,如下所示。

Sub Button1Click()

    Dim temp As Worksheet
    Dim rng As Range
    Dim Cancel As Boolean

    Cancel = False

    Set temp = ThisWorkbook.Worksheets("Input")

    Dim tempAct As String
    Dim tempCC As String
    Dim tempRan As String

    tempAct = Range("B1").Value
    tempCC = Range("B2").Value
    tempRan = Range("B3").Value

    Set rng = temp.Range("B1:B3")
    For Each cell In rng
        If IsEmpty(cell) Then
            MsgBox ("Blank Input!")
            Cancel = True
            Exit For
        End If
    Next

    If Cancel = False Then
        temp.Range("C1").Value = tempAct
        temp.Range("C2").Value = tempCC
        temp.Range("C3").Value = tempRan
    End If
End Sub
于 2013-09-09T04:56:30.180 回答