我在VBA中有一个函数,通过调用其他较小的函数进行一些操作。此功能无法在任何工作表中工作,只需一个工作表。在工作表中,它不起作用,当从另一个函数获取值时,它就会从整个算法中脱离出来。
这是我的Function determinarCantidadPorPedido(ByVal material As String, ByVal mes As String)
Dim demanda As Double
Dim pProgramado As Double
Dim numPedidos As Integer
demanda = determinarDemanda(material, mes)
Call contarProductosMateriales
Sheets("LlegadaMateriales").Activate
Dim fila As Integer
Dim columna As Integer
For i = 1 To numMateriales Step 1
If Sheets("LlegadaMateriales").Cells(1 + i, 1) = material Then
fila = 1 + i
Exit For
End If
Next
For j = 1 To 12 Step 1
If Sheets("LlegadaMateriales").Cells(1, j + 1) = mes Then
columna = j + 1
Exit For
End If
Next
pProgramado = Sheets("LlegadaMateriales").Cells(fila, columna)
numPedidos = darCantidadPedidos(determinarDiasMes(mes), darLeadTime(material))
determinarCantidadPorPedido = WorksheetFunction.Round((demanda - pProgramado) / numPedidos, 0)
并调用Public Function determinarDemanda(ByVal material As String, ByVal mes As String) As Double
Dim filaProducto As Double
Dim numMat As Double
Dim columnaDemanda As Double
Worksheets("ProductosYMateriales").Activate
numMat = Sheets("ProductosYMateriales").Range(Cells(3, 1), Cells(3, 1).End(xlDown)).Rows.Count
For i = 1 To numMat Step 1
If Sheets("ProductosYMateriales").Cells(i + 2, 1) = material Then
filaProducto = i + 2
Exit For
End If
Next
For j = 1 To 12 Step 1
If Sheets("Demanda").Cells(1, j + 2) = mes Then
columnaDemanda = j + 2
Exit For
End If
Next
Dim numProd As Double
numProd = Sheets("ProductosYMateriales").Range(Cells(2, 2), Cells(2, 2).End(xlToRight)).Columns.Count
Dim demanda As Double
demanda = 0
For k = 1 To numProd Step 1
Dim x As Double
x = Sheets("Demanda").Cells(k + 1, columnaDemanda)
Dim y As Double
y = Sheets("ProductosYMateriales").Cells(filaProducto, k + 1)
Dim prod As Double
prod = x * y
demanda = demanda + prod
Next
determinarDemanda = demanda
和Function determinarCantidadPorPedido(ByVal material As String, ByVal mes As String)
Dim demanda As Double
Dim pProgramado As Double
Dim numPedidos As Integer
demanda = determinarDemanda(material, mes)
Call contarProductosMateriales
Sheets("LlegadaMateriales").Activate
Dim fila As Integer
Dim columna As Integer
For i = 1 To numMateriales Step 1
If Sheets("LlegadaMateriales").Cells(1 + i, 1) = material Then
fila = 1 + i
Exit For
End If
Next
For j = 1 To 12 Step 1
If Sheets("LlegadaMateriales").Cells(1, j + 1) = mes Then
columna = j + 1
Exit For
End If
Next
pProgramado = Sheets("LlegadaMateriales").Cells(fila, columna)
numPedidos = darCantidadPedidos(determinarDiasMes(mes), darLeadTime(material))
determinarCantidadPorPedido = WorksheetFunction.Round((demanda - pProgramado) / numPedidos, 0)
答案 0 :(得分:1)
这一行存在一个问题:
numMat = Sheets("ProductosYMateriales").Range(Cells(3, 1), Cells(3, 1).End(xlDown)).Rows.Count
当您致电Cells
时,会引用当前的有效工作表。因此,如果您在不同的工作表上调用该函数,您将得到不同的结果。
您必须指定要引用单元格的工作表。我想你想要这个:
numMat = Sheets("ProductosYMateriales").Range(Sheets("ProductosYMateriales").Cells(3, 1), Sheets("ProductosYMateriales").Cells(3, 1).End(xlDown)).Rows.Count