电源查询和VBA

时间:2018-07-24 21:48:02

标签: excel vba

我是新来的,但是与一个更大的项目一起学习VBA,但是现在我陷入了困境。任何帮助将不胜感激。

sheet1中运行一个宏,该宏调用sheet2中的另一个宏,该宏再次运行一个模块。当我在sheet2中创建表时,要在工作簿中使用Power查询,宏将不再起作用。我收到以下消息:

  

“运行时错误'1004':应用程序定义的错误或对象定义的错误。”

通过删除宏的零碎部分,我发现问题似乎出在模块上。在该模块中,我编写了以下内容(我愿意用一种更简短的方式编写它,但是对此我是陌生的):

Sub fagrekke_dato2()


If Ark1.[E8].Value = 1 Then
Call DateFiller_endd1
Call DateFiller_startt1
Call DateFiller_endt1
End If

If Ark1.[E8].Value = 2 Then
Call DateFiller_endd1
Call DateFiller_startt1
Call DateFiller_endt1
Call DateFiller_endd2
Call DateFiller_startt2
Call DateFiller_endt2
End If

If Ark1.[E8].Value = 3 Then
Call DateFiller_endd1
Call DateFiller_startt1
Call DateFiller_endt1
Call DateFiller_endd2
Call DateFiller_startt2
Call DateFiller_endt2
Call DateFiller_endd3
Call DateFiller_startt3
Call DateFiller_endt3
End If

Range("E2:E200").Copy Destination:=Range("B2") 'forbedre?



End Sub

Sub DateFiller_endd1()

Dim k, l As Date
l = Cells(2, 5).Value
For k = 2 To Rows.Count Step Ark1.[E8].Value
Cells(k, 5).Value = l
l = l + 7
If Cells(k, 5).Value + 6 > Ark1.Cells(3, 4).Value Then Exit Sub
Next k
End Sub

Sub DateFiller_endt1()

Dim o, p As Date
p = Cells(2, 6).Value
For o = 2 To Rows.Count Step Ark1.[E8].Value
Cells(o, 6).Value = p
If Cells(o, 5).Value + 6 > Ark1.Cells(3, 4).Value Then Exit Sub
Next o
End Sub

Sub DateFiller_startt1()

Dim i, j As Date
j = Cells(2, 4).Value
For i = 2 To Rows.Count Step Ark1.[E8].Value
Cells(i, 4).Value = j
If Cells(i, 5).Value + 6 > Ark1.Cells(3, 4).Value Then Exit Sub
Next i
End Sub


Sub DateFiller_startt2()

Dim i, j As Date
j = Cells(3, 4).Value
For i = 3 To Rows.Count Step Ark1.[E8].Value
Cells(i, 4).Value = j
If Cells(i, 5).Value + 6 > Ark1.Cells(3, 4).Value Then Exit Sub
Next i
End Sub


Sub DateFiller_endd2()

Dim k, l As Date
l = Cells(3, 5).Value
For k = 3 To Rows.Count Step Ark1.[E8].Value
Cells(k, 5).Value = l
l = l + 7
If Cells(k, 5).Value + 6 > Ark1.Cells(3, 4).Value Then Exit Sub
Next k
End Sub

Sub DateFiller_endt2()

Dim o, p As Date
p = Cells(3, 6).Value
For o = 3 To Rows.Count Step Ark1.[E8].Value
Cells(o, 6).Value = p
If Cells(o, 5).Value + 6 > Ark1.Cells(3, 4).Value Then Exit Sub
Next o
End Sub
Sub DateFiller_startt3()

Dim i, j As Date
j = Cells(4, 4).Value
For i = 4 To Rows.Count Step Ark1.[E8].Value
Cells(i, 4).Value = j
If Cells(i, 5).Value + 6 > Ark1.Cells(3, 4).Value Then Exit Sub
Next i
End Sub


Sub DateFiller_endd3()

Dim k, l As Date
l = Cells(4, 5).Value
For k = 4 To Rows.Count Step Ark1.[E8].Value
Cells(k, 5).Value = l
l = l + 7
If Cells(k, 5).Value + 6 > Ark1.Cells(3, 4).Value Then Exit Sub
Next k
End Sub

Sub DateFiller_endt3()

Dim o, p As Date
p = Cells(4, 6).Value
For o = 4 To Rows.Count Step Ark1.[E8].Value
Cells(o, 6).Value = p
If Cells(o, 5).Value + 6 > Ark1.Cells(3, 4).Value Then Exit Sub
Next o
End Sub

该模块的目的是估计每周发生1至3次的事件的日期和时间。我确实相信问题在于该表无法定义其长度。

我可能还要补充一点:我循环使用此模块,在此检查是否有任何假期或活动与计划冲突。如果是这样,则更新所有日期的计划:

Dim sh1 As Worksheet, sh2 As Worksheet, lr As Long, fn As Range, sr As Range, rng As Range, n As Long
Set sh1 = Me 
Set sh2 = Ark13 
lr = sh1.Cells(Rows.Count, 5).End(xlUp).Row
For i = 2 To lr Step 1

Set fn = sh2.Range("E:E").Find(sh1.Cells(i, 5).Value, , xlValues, xlWhole)
If Not fn Is Nothing Then
Set sr = fn.Offset(0, 1)                    
Set er = sr.End(xlDown)
Set rng = sr
n = rng.Rows.Count
rng.EntireRow.Copy
sh1.Cells(i, 1).Resize(n).EntireRow.Insert
Call fagrekke_dato2
End If
Ark1.Range("D27:F29").Copy
Cells(2, 4).PasteSpecial xlPasteValues
Next

有人知道为什么在sheet2中创建表会遇到麻烦吗?

0 个答案:

没有答案