我尝试使用一些条件值和循环从范围M13:M22
计算,但我的一些代码仅适用于单元格M13
,并且不会循环到单元格M22
。我该如何解决这个问题?
这是我的代码:
Private Sub CommandButton1_Click()
Dim pelanggan As Range, alamat As Range, diskon As Range, jdiskon As Range, tanggal As Range, jtempo As Range
Dim rout(1 To 10) As Variant, i As Long
Dim path As String
path = "\\Faizal\Data D Faizal\Daftar Harga\Price List"
Filename = Dir(path & "database.xlsx")
Set pelanggan = Range("E7")
Set alamat = Range("E8")
Set diskon = Range("L25")
Set tanggal = Range("L7")
Set jdiskon = Range("P13")
Set jtempo = Range("K30")
getalamat = Application.WorksheetFunction.VLookup(pelanggan & Range("J7"), Workbooks("database.xlsx").Worksheets("DB").Range("A6:N1350"), 14, False)
getdiskon = Application.WorksheetFunction.VLookup(pelanggan & Range("J7"), Workbooks("database.xlsx").Worksheets("DB").Range("A6:N1350"), 6, False)
getjdiskon = Application.WorksheetFunction.VLookup(pelanggan & Range("J7"), Workbooks("database.xlsx").Worksheets("DB").Range("A6:N1350"), 11, False)
getjtempo = Application.WorksheetFunction.VLookup(pelanggan & Range("J7"), Workbooks("database.xlsx").Worksheets("DB").Range("A6:N1350"), 13, False)
alamat.Value = getalamat
diskon.Value = getdiskon / 100
jdiskon.Value = getjdiskon
tanggal.Value = DateValue(Now)
jtempo.Value = getjtempo
'here is the calculation that won't go loop
For i = 13 To 22
getharga = Application.WorksheetFunction.VLookup(Range("D" & i) & Range("E" & i), Workbooks("database.xlsx").Worksheets("Gold").Range("E4:H80"), 4, False)
If jdiskon = "Nett" Then
Range("M" & i).Value = getharga - (getharga * diskon)
Range("L25").ClearContents
ElseIf jdiskon = "Pot" Then
Range("M" & i).Value = getharga
Range("L25").Value = diskon
ElseIf jdiskon = "Diskon Kitir" Then
Range("M" & i).Value = getharga
Range("L25").ClearContents
End If
Next
End Sub
答案 0 :(得分:0)
您的问题:“ ...我的代码仅适用于单元格M13,不会循环到单元格M22。如何解决此问题?”
你的循环没问题,所以这不是你必须解决的问题。 您必须 debug 才能找到循环原因而不执行您的操作。
我在修改后的代码下面发布了两个功能:1)它完全符合Range
的条件,因此您可以避免意外错误,您可能需要检查this; 2)它使用MsgBox
es,一种调试方式。
这可能会查明“错误”。
Private Sub CommandButton1_Click()
Dim pelanggan As Range, alamat As Range, diskon As Range, jdiskon As Range, tanggal As Range, jtempo As Range
Dim rout(1 To 10) As Variant, i As Long
Dim path As String
path = "\\Faizal\Data D Faizal\Daftar Harga\Price List"
Filename = Dir(path & "database.xlsx")
Dim wb as Workbook, ws1 as Worksheet, ws2 as Worksheet, rng1 as Range
Set wb = Workbooks("database.xlsx")
Set ws1 = wb.Worksheets("DB")
Set ws2 = wb.Worksheets("Gold")
Set rng1 = ws.Range("A6:N1350")
Set pelanggan = ws1.Range("E7")
Set alamat = ws1.Range("E8")
Set diskon = ws1.Range("L25")
Set tanggal = ws1.Range("L7")
Set jdiskon = ws1.Range("P13")
Set jtempo = ws1.Range("K30")
Dim rng2 as Range
Set rng2 = ws1.Range(pelanggan.Value & ws1.Range("J7").Value)
getalamat = Application.WorksheetFunction.VLookup(rng2, rng1, 14, False)
getdiskon = Application.WorksheetFunction.VLookup(rng2, rng1, 6, False)
getjdiskon = Application.WorksheetFunction.VLookup(rng2, rng1, 11, False)
getjtempo = Application.WorksheetFunction.VLookup(rng2, rng1, 13, False)
alamat.Value = getalamat
diskon.Value = getdiskon / 100
jdiskon.Value = getjdiskon
tanggal.Value = DateValue(Now)
jtempo.Value = getjtempo
'here is the calculation that won't go loop
For i = 13 To 22
Dim rng3 as Range
Set rng3 = ws1.Range(ws1.Range("D" & i).Value & ws1.Range("E" & i).Value)
getharga = Application.WorksheetFunction.VLookup(rng3, ws2.Range("E4:H80"), 4, False)
MsgBox "getharga = " & getharga & " for i = " & i
If jdiskon = "Nett" Then
ws1.Range("M" & i).Value = getharga - (getharga * diskon)
ws1.Range("L25").ClearContents
ElseIf jdiskon = "Pot" Then
ws1.Range("M" & i).Value = getharga
ws1.Range("L25").Value = diskon
ElseIf jdiskon = "Diskon Kitir" Then
ws1.Range("M" & i).Value = getharga
ws1.Range("L25").ClearContents
Else
MsgBox "jdiskon = " & jdiskon & " for i = " & i
End If
Next
End Sub
(PS:我目前没有Excel系统,因此这段代码可能需要很少的调整)。
答案 1 :(得分:0)
对于迟到的评论感到抱歉,我将“昏暗的磁盘作为范围”更改为“昏暗的磁盘作为变体”,然后我的代码完美运行。 感谢您为我提供的帮助。