Excel VBA - 数据编号超过4位数会导致脚本错误

时间:2017-03-22 19:16:19

标签: excel excel-vba vba

我有一个脚本循环遍历文件夹中的所有CSV文件,将数据导入主工作簿,然后关闭并删除CSV文件,然后再转到下一个文件。数据导入的一部分将某些单元格设置为变量,然后执行计算以获取新数字,然后将其发布到工作簿中。我有一个问题,我的一个CSV给了我一个错误,我已经缩小到与数据是否长度超过4位有关。 This screenshot of the CSV shows what data is causing an error.如果我要通过从每个突出显示的单元格中删除一位数来更改值,则CSV将正确导入。我不知道造成这种情况的原因或解决方法。将单元格格式更改为数字/文本/一般/等等并没有什么不同。单元格的值本身也不包含逗号(值实际为9513,而不是9,513)

以下是处理导入CSV数据的脚本的摘录:

'LOOP THROUGH FOLLOWING CODE IF A MATCH IS FOUND
    If Not rng1 Is Nothing Then
        Dim table_list_object As ListObject
        Dim table_object_row As ListRow
        Set table_list_object = Workbooks("Spokes- Google Analytics trends.xlsm").Worksheets(sheet_name_result).ListObjects(1)
        Set table_object_row = table_list_object.ListRows.Add

    'calculate pageviews
        Dim organic_pageviews As Integer
        Dim paid_pageviews As Integer
        Dim direct_pageviews As Integer
        Dim referral_pageviews As Integer
        Dim display_pageviews As Integer
        Dim sum_pageviews As Integer
        organic_pageviews = Application.SumIf(Range("A:A"), "organic", Range("C:C"))
        paid_pageviews = Application.SumIf(Range("A:A"), "paid", Range("C:C"))
        direct_pageviews = Application.SumIf(Range("A:A"), "direct", Range("C:C"))
        referral_pageviews = Application.SumIf(Range("A:A"), "referral", Range("C:C"))
        display_pageviews = Application.SumIf(Range("A:A"), "display", Range("C:C"))
        sum_pageviews = organic_pageviews + paid_pageviews + direct_pageviews + referral_pageviews + display_pageviews

    'calculate visitors aka sessions
        Dim organic_visitors As Integer
        Dim paid_visitors As Integer
        Dim direct_visitors As Integer
        Dim referral_visitors As Integer
        Dim display_visitors As Integer
        Dim sum_visitors As Integer
        organic_visitors = Application.SumIf(Range("A:A"), "organic", Range("F:F"))
        paid_visitors = Application.SumIf(Range("A:A"), "paid", Range("F:F"))
        direct_visitors = Application.SumIf(Range("A:A"), "direct", Range("F:F"))
        referral_visitors = Application.SumIf(Range("A:A"), "referral", Range("F:F"))
        display_visitors = Application.SumIf(Range("A:A"), "display", Range("F:F"))
        sum_visitors = organic_visitors + paid_visitors + direct_visitors + referral_visitors + display_visitors

    'calculate unique visitors aka new users
        Dim organic_new As Integer
        Dim paid_new As Integer
        Dim direct_new As Integer
        Dim referral_new As Integer
        Dim display_new As Integer
        Dim sum_new As Integer
        organic_new = Application.SumIf(Range("A:A"), "organic", Range("E:E"))
        paid_new = Application.SumIf(Range("A:A"), "paid", Range("E:E"))
        direct_new = Application.SumIf(Range("A:A"), "direct", Range("E:E"))
        referral_new = Application.SumIf(Range("A:A"), "referral", Range("E:E"))
        display_new = Application.SumIf(Range("A:A"), "display", Range("E:E"))
        sum_new = organic_new + paid_new + direct_new + referral_new + display_new

    'calculate pages/visit
        Dim pages_per_visit As String
        pages_per_visit = (sum_pageviews / sum_visitors) / 100
    'calculate organic traffic
        Dim organic_percent As String
        organic_percent = (organic_visitors / sum_visitors)

    'calculate referral traffic
        Dim referral_percent As String
        referral_percent = (referral_visitors / sum_visitors)

    'isolate start date
        Dim date_location As String
        Dim start_date_ugly As String
        Dim start_date_string As String

        date_location = ActiveWorkbook.Sheets(1).Range("A4")
        start_date_ugly = Left(date_location, 10)
        start_date_string = Right(start_date_ugly, 8)

    'isolate end date
        Dim end_date_string As String

        end_date_string = Right(date_location, 8)

    'posts data to columns
        table_object_row.Range(1, 1).Value = DateSerial(Left(start_date_string, 4), Mid(start_date_string, 5, 2), Right(start_date_string, 2))
            table_object_row.Range(1, 1).NumberFormat = "mm/dd/yyyy"
        table_object_row.Range(1, 2).Value = DateSerial(Left(end_date_string, 4), Mid(end_date_string, 5, 2), Right(end_date_string, 2))
            table_object_row.Range(1, 2).NumberFormat = "mm/dd/yyyy"
        table_object_row.Range(1, 3).Value = sum_pageviews
        table_object_row.Range(1, 4).Value = sum_visitors
        table_object_row.Range(1, 5).Value = sum_new
        table_object_row.Range(1, 6).Value = Format((pages_per_visit), Percent)
        table_object_row.Range(1, 7).Value = organic_percent
        table_object_row.Range(1, 8).Value = referral_percent

1 个答案:

答案 0 :(得分:1)

屏幕截图数据显示溢出错误。

Read up on the limitations of Integer data type:

  

整数变量可以保持介于-32,768和32,767之间的值,而长变量的范围可以是-2,147,483,648到2,147,483,647

在您的情况下,几个整数的总和可能超过Integer变量的允许值。将您的所有As Integer声明更改为As Long

试试这个来说明它

Sub f()
    Dim x As Integer, y As Integer, z As Integer
    x = 9000
    y = 23767
    z = x + y
    MsgBox z

    MsgBox z + 1  '## Now we're over the limit of Integer type, this line will error

End Sub

使用Integer类型的算术似乎也有些棘手 - 即:如果你将几个值加在一起并且其中任何一个都是Integer类型,VBA预计返回值也是一个整数。有时候这不是问题,但是当处理更大的数字时,通常是这样,所以如果你这样做:

val = 6 + 32767 'This will also raise an error!
val = 32767 + 64738 'This will NOT error!

您需要将整数强制转换为Long,您可以使用CLng函数执行此操作:

val = CLng(6) + 32767