我只知道 Excel 的基本知识。我从客户那里得到了一份巨大的 Excel 表。我试图用示例数据解释我想要什么。这是一张表中的输入数据
我需要像下面这样的输出
我尝试了下面的程序
Sub TestCustomMerge()
Dim row2&, row4&, val1&, val2&, val3&
Application.DisplayAlerts = False
row2 = 2
row4 = 4
'// error at below line i.e. Run-time error 13: Type mismatch
val1 = Cells(row2, 1).Value
' // Also I am not getting how concatenate value of Cells(row2, 2) and Cells(row4, 2)
' // so that they appear as it is (not in a single line)
val2 = Cells(row2, 2).Value
Cells(row2, 1).Cells(row4, 1).Merge
Cells(row2, 1).Value = val1
Cells(row2, 2).Cells(row4, 2).Merge
'// set concatenated value here
Cells(row2, 2).Value = val2
Application.DisplayAlerts = True
End Sub
但它给了Run-time error 13: Type mismatch
答案1
它返回错误,因为值1定义为长整型,单元格值是字符串。我删除了&来自的字符暗淡部分。合并部分也已更新。
Sub TestCustomMerge()
Dim row2, row4, val1, val2, val3
Application.DisplayAlerts = False
row2 = 2
row4 = 4
val1 = Cells(row2, 1).Value
val2 = Cells(row2, 2).Value
Range(Cells(row2, 1), Cells(row4 - row2, 1)).Merge
Cells(row2, 1).Value = val1
Range(Cells(row2, 2), Cells(row4 - row2, 2)).Merge
Cells(row2, 2).Value = val2
Application.DisplayAlerts = True
End Sub
答案2
为什么不使用数据透视表?如果你的数据很大,在多行上运行这个 vba 脚本会遇到一些问题