返回列表 上一主題 發帖

[發問] 寫判斷式如果跳脫用的數值要從原始資料中抓取該如何是好

[發問] 寫判斷式如果跳脫用的數值要從原始資料中抓取該如何是好

本帖最後由 GBKEE 於 2015-6-20 14:16 編輯

小妹不才,寫了一段判斷原始EXCEL工作表中的表格車輛數
原始資料表格如下

可是跑程式時會出現資料型態不正確的錯誤訊息
程式如下

Sub 車輛多筆拆成一筆()
Dim car_num, last_row As Integer
Dim mycell As Range


ActiveSheet.Cells.SpecialCells(xlLastCell).Select
last_row = Selection.Row


For i = 1 To last_row
Set mycell = ActiveSheet.Cells(i, 2)
car_num = mycell.Value

    If car_num > 1 Then
    mycell.Interior.ColorIndex = 2
        For j = 1 To car_num  <-----錯誤出現行,所以是不能將資料表格中的值當作真正的數字去跑回圈嗎??可是這是唯一判斷的數值啊..求解  TT
           mycell.Rows(i).Insert
           mycell.Rows(i).Copy Destination:=mycell.Rows(i + j)
           a = ActiveSheet.Cells(i, car_num).Value
           ActiveSheet.Cells(i, car_num).Value = a / car_num
           b = ActiveSheet.Cells(i, 8).Value
           ActiveSheet.Cells(i, 8).Value = b / car_num
           c = ActiveSheet.Cells(i, 9).Value
           ActiveSheet.Cells(i, 9).Value = c / car_num
           ActiveSheet.Cells(i, 11).Value = ActiveSheet.Cells(i, 9).Value * ActiveSheet.Cells(i, 10).Value
           d = ActiveSheet.Cells(i, 12).Value
           ActiveSheet.Cells(i, 12).Value = d / car_num
           e = ActiveSheet.Cells(i, 13).Value
           ActiveSheet.Cells(i, 13).Value = e / car_num
           f = ActiveSheet.Cells(i, 14).Value
           ActiveSheet.Cells(i, 14).Value = f / car_num
        Next j
    End If
Next i

End Sub

回復 1# Ackroyd


1.ActiveSheet不需要在每個地方都使用
2.您的程式碼可以再簡短許多,未來比較容易維護

您附檔案
幫您看比較快
PKKO

TOP

'不太會看程序請參考一下
Sub xx()
lastrow = [A65536].End(xlUp).Row  ''判斷最後一列
For j = 1 To lastrow
xr = Cells(j, 1)
    For i = 1 To xr
        Cells(i, 4) = Cells(i, 4) / xr ''用自己除
    Next
Next
End Sub

TOP

回復 1# Ackroyd
  1. If car_num > 1 Then
  2.         MsgBox car_num  '測試看看是數值嗎?
  3.         mycell.Interior.ColorIndex = 2
複製代碼
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

GB大所言甚是

原因就是那一欄有很多空白值不被當作數值

感謝你!!!!

TOP

        靜思自在 : 小事不做、大事難成。
返回列表 上一主題