我昨天开始使用 VBA 编程,因此我还是新手。
我尝试制作一个自动为我定义某些区域的宏,虽然它在我的工作表中运行良好,但它仍然受到限制,因为我使用它的所有工作表都必须在设置中相同。
我目前正在定义 5 个区域
我的问题的最佳宏是提示用户输入容器编号,然后使用该编号自动确定可以在哪张纸上找到所述容器的信息,并自动确定列出的区域在哪里,并用给定的名称和用户输入的数字定义它们。
代码可以在这里看到:
Option Compare Text
Sub Definer()
Dim TankNr As Integer, rHF As Range, rAM As Range, rBV As Range, rTT As Range, rIT As Range
If MsgBox("Vil du definere nye områder for en tank", vbYesNo, "Confirm") = vbYes Then
TankNr = Application.InputBox("Indtast tank nr på den tank du vil definere områder for", "Tank Nummer", Type:=1)
If TankNr <> 0 Then
Set rHF = Sheets("Tank " & TankNr).Cells.Find(What:="HØJESTE FYLDEGRÆNSE =", LookAt:=xlWhole, MatchCase:=False, SearchFormat:=False)
If Not rHF Is Nothing Then
rHF.Offset(, 3).Name = "HF_" & TankNr
End If
Set rTT = Sheets("Tank " & TankNr).Cells.Find(What:="Enhed i tabellen : Liter", LookAt:=xlWhole, MatchCase:=False, SearchFormat:=False)
If Not rTT Is Nothing Then
rTT.CurrentRegion.Offset(2, 0).Resize(rTT.CurrentRegion.Rows.Count - 2).Name = "Tabel_tank" & TankNr
End If
Set rIT = Sheets("Tank " & TankNr).Cells.Find(What:="Interpolationstabel", LookAt:=xlWhole, MatchCase:=False, SearchFormat:=False)
If Not rIT Is Nothing Then
rIT.Resize(11, 11).Offset(1, 0).Name = "Int_tank" & TankNr
End If
Set rBV = Sheets("Tank " & TankNr).Cells.Find(What:="Bundvolumen =", LookAt:=xlWhole, MatchCase:=False, SearchFormat:=False)
If Not rBV Is Nothing Then
rBV.Offset(, 2).Name = "Bund" & TankNr
End If
Set rAM = Sheets("Tank " & TankNr).Cells.Find(What:="A-MÅL =", LookAt:=xlWhole, MatchCase:=False, SearchFormat:=False)
If Not rAM Is Nothing Then
rAM.Offset(, 1).Name = "A_mål" & TankNr
End If
MsgBox "Områder er blevet defineret"
End If
End If
End Sub
这有几个问题:
有问题的床单可以看到
由于我的声誉,我只能包含这两个链接,但如果有办法提供更多我的工作文档,请告诉我,我会很乐意这样做。
最佳答案
这是我将使用的方法。对于 HF,这只是一个示例,但希望您能理解并可以扩展到其他范围。我假设命名范围是包含“HF”的单元格右侧的单元格。
Sub OptimalSolution()
Dim TankNr As Integer, rHF As Range
If MsgBox("Do you wish to define new areas for a container?", vbYesNo, "Confirm") = vbYes Then
TankNr = Application.InputBox("Please enter the container number", "Container Number", , , , , , 1)
Set rHF = Sheets("Ark" & TankNr).Cells.Find(What:="HF", LookAt:=xlWhole, MatchCase:=True, SearchFormat:=False)
If Not rHF Is Nothing Then
rHF.Offset(, 1).Name = "HF_" & TankNr
End If
End If
End Sub
关于vba - 如何在VBA中创建一个可以自动确定矩阵边界的宏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43098922/