通过VLookup提高macor效率?

我一直在试图拼凑一个function的工作簿,使用VBA代码,我走了。

我目前有一个macros创build,查看列中的dynamic范围的值,并将其转换为新的值。

Sub ConvertWireSize() Dim i As Long Sheets("Circuits").Select Range("H1").Select For i = 1 To Rows.Count If Cells(i, 8).Value = 0.5 Then Cells(i, 8).Value = 20 ElseIf Cells(i, 8).Value = 0.8 Then Cells(i, 8).Value = 18 ElseIf Cells(i, 8).Value = 1 Then Cells(i, 8).Value = 16 ElseIf Cells(i, 8).Value = 2 Then Cells(i, 8).Value = 14 ElseIf Cells(i, 8).Value = 3 Then Cells(i, 8).Value = 12 ElseIf Cells(i, 8).Value = 5 Then Cells(i, 8).Value = 10 ElseIf Cells(i, 8).Value = 8 Then Cells(i, 8).Value = 8 ElseIf Cells(i, 8).Value = 13 Then Cells(i, 8).Value = 6 ElseIf Cells(i, 8).Value = 19 Then Cells(i, 8).Value = 4 End If Next i MsgBox "Wire Size Has Been Converted From CSA to AWG." Sheets("Main").Select End Sub 

这似乎是一种非常低效和缓慢的做事方式。 我一直在拼凑一个使用VLookup的新macros,但是我做的研究越多,我就越困惑。

有人能帮我指出正确的方向吗?

不要去行1048576,只要列H的值延伸。 使用Select Case清理您的IF。 不要使用select 。

 Sub ConvertWireSize() Dim i As Long with workSheets("Circuits") For i = 1 To .cells(.Rows.Count, "H").end(xlup).row select case .Cells(i, 8).Value2 case 0.5 .Cells(i, 8).Value = 20 case 0.8 .Cells(i, 8).Value = 18 case 1 .Cells(i, 8).Value = 16 ... end select Next i end with MsgBox "Wire Size Has Been Converted From CSA to AWG." workSheets("Main").Select End Sub 

我不能轻易确定转换的线性模式,但是您可以调整一些math来进行每次转换。

如果你有超过几千个这样的转换,那么一个内存数组将显示效率的显着提高。