我对编写VBA代码非常陌生,所以希望这对你们所有人来说都不是太容易,但希望你能帮助我克服这个学习困难,我还没有超越。我正在编写一个代码来增加当前价格的数量,我想结合以下子程序,所以我不必单独运行每个代码。他们在这里:
Sub addingincurrentdate()
Workbooks("fuel prices w. macro program 2015").Activate
Worksheets("sheet1").Activate
Range("A1").End(xlDown).Offset(1, 0).Select
ActiveCell.Value = Date
ActiveCell.NumberFormat = "mm/dd/yy"
End Sub
Sub hoprice()
Workbooks("fuel prices w. macro program 2015").Activate
Worksheets("sheet1").Activate
Dim myvalue As Variant
myvalue = InputBox("How much do you want to add to the #2 Heating Oil Price?")
Range("B2").End(xlDown).Offset(1, 0).Select
ActiveCell.Value = ActiveCell.Offset(-1, 0).Value + myvalue
End Sub
Sub addingDieselprice()
Workbooks("fuel prices w. macro program 2015").Activate
Worksheets("sheet1").Activate
Dim myvalue As Variant
myvalue = InputBox("How much do you want to add to the Off Rd Delivered Price?")
Range("D2").End(xlDown).Offset(1, 0).Select
ActiveCell.Value = ActiveCell.Offset(-1, 0).Value + myvalue
End Sub
答案 0 :(得分:0)
如果我了解您的问题,您可以将main
子信息放在其他三个以下:
Sub main()
addingincurrentdate
hoprice
addingDieselprice
End Sub
然后 - 只需调用main
一次,它就会依次调用你的三个潜艇。也可以将它们组合成一个子,但如果它们已经在工作,那么从单个子句中调用它们就足够简单了。
答案 1 :(得分:0)
只需将每个宏中的所有代码放入一个并删除重复的行(我也删除并组合了select语句,因为这些不需要)
Sub Combined()
Workbooks("fuel prices w. macro program 2015").Activate
Worksheets("sheet1").Activate
with Range("A1").End(xlDown).Offset(1, 0)
.Value = Date
.NumberFormat = "mm/dd/yy"
end with
Dim myvalue As Variant
myvalue = InputBox("How much do you want to add to the #2 Heating Oil Price?")
Range("B2").End(xlDown).Offset(1, 0).Value = ActiveCell.Offset(-1, 0).Value + myvalue
myvalue = InputBox("How much do you want to add to the Off Rd Delivered Price?")
Range("D2").End(xlDown).Offset(1, 0).Value = ActiveCell.Offset(-1, 0).Value + myvalue
End Sub
答案 2 :(得分:0)
到目前为止,这两个答案都很不错。您还可以选择执行以下操作:
Sub CombinedMethods(WhatWork As String)
Dim myvalue As Variant
Workbooks("fuel prices w. macro program 2015").Activate
Worksheets("sheet1").Activate
If LCase(WhatWork) = "formatdate" Then
Range("A1").End(xlDown).Offset(1, 0).Select
ActiveCell.Value = Date
ActiveCell.NumberFormat = "mm/dd/yy"
Exit Sub
End If
If LCase(WhatWork) = "hoprice" Then
myvalue = InputBox("How much do you want to add to the #2 Heating Oil Price?")
Range("B2").End(xlDown).Offset(1, 0).Select
ActiveCell.Value = ActiveCell.Offset(-1, 0).Value + myvalue
Exit Sub
End If
If LCase(WhatWork) = "dieselprice" Then
myvalue = InputBox("How much do you want to add to the Off Rd Delivered Price?")
Range("D2").End(xlDown).Offset(1, 0).Select
ActiveCell.Value = ActiveCell.Offset(-1, 0).Value + myvalue
Exit Sub
End If
MsgBox "Expected to receive either formatdate, hoprice or dieselprice as argument. Received " & WhatWork & ". Exiting", vbInformation + vbOKOnly, "Unexpected argument"
End Sub
Sub Main()
CombineMethods ("formatdate")
CombineMethods ("hoprice")
CombineMethods ("dieselprice")
End Sub
这样,您可以使用不同的参数调用单个过程,并且该过程根据提供的参数执行适当的工作。
答案 3 :(得分:0)
将其全部简化为一个子:
Sub AllSubsInOne()
With Workbooks("fuel prices w. macro program 2015")
With .Sheets("sheet1")
.Cells(.Rows.Count, 1).End(xlUp).Offset(1, 0).Value = Format(Date, "dd/mm/yyyy")
With .Cells(.Rows.Count, 2).End(xlUp).Offset(1, 0)
.Value = .Offset(-1, 0).Value + InputBox("How much do you want to add to the #2 Heating Oil Price?")
End With
With .Cells(.Rows.Count, 4).End(xlUp).Offset(1, 0)
.Value = .Offset(-1, 0).Value + InputBox("How much do you want to add to the Off Rd Delivered Price?")
End With
End With
End With
End Sub
答案 4 :(得分:0)
你想要做的是用这样的东西调用每个子
sub call_other_subs()
call addingincurrentdate()
call hoprice()
call addingDieselprice()
end sub
尝试使用和不使用“call”一词来查看它是如何工作的。也可以使用和不使用括号“()”来尝试。尝试那些更好地了解这些东西是如何工作的。
另外,我建议你做一些关于如何从另一个子程序调用VBA子程序的互联网搜索,这将为你提供任意数量的这些想法变化的例子。