使用excel vba删除列时出错

时间:2018-04-16 09:50:53

标签: excel vba

sub column_delete()

dim x as workbook
dim sht1 as worksheet
dim currentColumn as integer
dim columnHeader as String

Set x = Workbooks.Open("Path to the file")
Set sht1 = x.Sheets("Page 1")
*For currentColumn = sht1.UsedRange.Columns.Count To 1 Step -1*

*columnHeader = shtl.UsedRange.Cells(1, currentColumn).Value*

Select Case columnHeader
 Case "Date", "Time", "Header" 
  'Nothing'
 Case Else
  *shtl.Columns(currentColumn).Delete*

End Select
Next

End Sub

对于以上所有突出显示的步骤,我收到错误,因为运行时错误'424': 需要对象。

但我相信我已宣布所需物品。有时候,当我重新打开excel时,错误会发生,而其他时间会出现。

请你帮我解决这个问题。

P.S:我是excel vba的初学者。

1 个答案:

答案 0 :(得分:2)

始终在模块顶部写上dim2 <- as.numeric(tab$dim2) names(dim2) <- tab$dim1 x <- outer(dim2, dim2, function(x,y) (x + y) / 2) as.dist(x) # Loc.1 Loc.2 Loc.3 Loc.4 # Loc.2 0.3250 # Loc.3 0.3230 0.3230 # Loc.4 0.2905 0.2905 0.2885 # Loc.5 0.2900 0.2900 0.2880 0.2555

因此,只要在编译时出现错误并突出显示非定义变量,就会避免Option Explicitsht1之类的错误:

enter image description here

相关问题