简体   繁体   English

错误为“下标超出范围”

[英]Error as “Subscript Out of range”

Am new to VBA Programming Kindly help me in getting the solution. VBA编程的新手,请帮助我获得解决方案。

My code has to accept user defined excel files and take those values of the cells as log which are colored.I am getting error as "Subscript Out of range" 我的代码必须接受用户定义的excel文件,并将这些单元格的值作为彩色的日志记录。我收到“下标超出范围”错误

Public color_Change, color_Recall
Private Sub CommandButton1_Click()
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False

    Dim rcell As Range
    Dim CellData As String
    Dim fso As FileSystemObject

    Set fso = New FileSystemObject

    Dim stream As TextStream
    Set stream = fso.OpenTextFile("D:\Support.log", ForWriting, True)
    CellData = ""

    Dim vaFiles As Variant
    vaFiles = Application.GetOpenFilename()
    ActiveSheet.Range("B10") = vaFiles

    Set wb = Workbooks.Open(vaFiles)

    For Each vaFiles In ActiveWorkbook.Worksheets
        Worksheets(vaFiles.Name).Activate
        stream.WriteLine "The name of the Tab Sheet is :" & vaFiles.Name
        color_Change = getRGB2("A1")
        'color_Recall = getRGB2("A2")
        For Each rcell In vaFiles.UsedRange.Cells
            arrcolor = color_Change
            rcell.Interior.Color = getRGB1("A3")
            For Each color_Recall In ActiveSheet.UsedRange
                If rcell.Interior.Color = arrcolor Then
                CellData = Trim(rcell.Value)
                stream.WriteLine "The Value at location (" & rcell.Row & "," & rcell.Column & ") " & CellData & " " & rcell.Address
            End If
        'End If
        Next
    Next
    stream.WriteLine vbCrLf
    'Next
    'Next
    stream.Close
    MsgBox ("Job Done")
End Sub

Function getRGB2(ccell) As String
    Dim wkb As Workbook

    ThisWorkbook.Sheets(Sheet).Activate
    'wkb.Activate
    Dim i As Integer, rng As Range
    Dim r As Byte, g As Byte, B As Byte

    Set rng = Range(ccell)
    With rng.Interior
        r = .Color Mod 256
        g = .Color \ 256 Mod 256
        B = .Color \ (CLng(256) * 256)
    End With
    getRGB2 = r & "," & g & "," & B
End Function

Function getRGB1(ccell) As String
    Dim wkb As Workbook

    ThisWorkbook.Sheets(Sheet).Activate
    'wkb.Activate
    Dim i As Integer, rng As Range
    Dim r As Byte, g As Byte, B As Byte

    Set rng = Range(ccell)
    With rng.Interior
        r = .Color Mod 256
        g = .Color \ 256 Mod 256
        B = .Color \ (CLng(256) * 256)
    End With
    getRGB1 = r & "," & g & "," & B
End Function

I can't replicate your error but: 我无法复制您的错误,但:

  1. You don't need to Activate the sheets, you cant loop through the sheets if you qualify the getRGB1 and getRGB2 functions 您不需要Activate表,如果您符合getRGB1getRGB2函数的资格,则getRGB1 getRGB2工作表
  2. You have a second loop looking at all cells ( color_Recall ) that doesn't seem to serve any purpose 您有第二个循环查看似乎没有任何作用的所有单元格( color_Recall

suggest 建议

For Each vafiles In ActiveWorkbook.Worksheets
    stream.WriteLine "The name of the Tab Sheet is :" & vafiles.Name
    color_Change = getRGB2(vafiles.Range("A1"))
        For Each rcell In vafiles.UsedRange.Cells
            arrcolor = color_Change
            rcell.Interior.Color = getRGB1(vafiles.Range("A3"))
            If rcell.Interior.Color = arrcolor Then
                 CellData = Trim(rcell.Value)
                  stream.WriteLine "The Value at location (" & rcell.Row & "," & rcell.Column & ") " & CellData & " " & rcell.Address
            End If
        Next
Next

The fundamental differences between Subs and Functions are Subs和Function之间的根本区别是

  • A Sub can work on objects 潜艇可以在物体上工作
  • A Sub doesn't have a return value Sub没有返回值
  • A Function can not change an object 函数不能更改对象
  • A Function will usually return something When you call 函数通常会在您调用时返回某些内容

     ThisWorkbook.Sheets(Sheet).Activate 

    You are trying to change the Workbook object which isn't allowed. 您正在尝试更改不允许的工作簿对象。

I'm also not sure that ThisWorkbook.Sheets(Sheet) is a valid object unless you've defined Sheet as a global variable. 我也不确定ThisWorkbook.Sheets(Sheet)是有效的对象,除非您已将Sheet定义为全局变量。

A Google search for Google搜索

get rgb color excel 获得RGB颜色Excel

turned up this as the top result 将此作为最高结果

Function getRGB2(rcell) As String
Dim C As Long
Dim R As Long
Dim G As Long
Dim B As Long

    C = rcell.Interior.Color
    R = C Mod 256
    G = C \ 256 Mod 256
    B = C \ 65536 Mod 256
    getRGB2 = R & "," & G & "," & B
End Function

From http://excel.tips.net/T010179_Determining_the_RGB_Value_of_a_Color.html 来自http://excel.tips.net/T010179_Determining_the_RGB_Value_of_a_Color.html

Function getRGB2(ccell) As String
Dim wkb As Workbook

ThisWorkbook.Sheets(Sheet).Activate

Instead try this: 而是尝试这样:

Function getRGB2(ccell) As String
Dim wkb As Workbook ' or rename this to Dim ThisWorkbook As Workbook
Set wkb = ActiveWorkbook ' or rename this to Set ThisWorkbook = ActiveWorkbook
wkb.Sheets("Name of the sheet you want").Activate ' or rename this to ThisWorkbook.Sheets("Name of the sheet you want").Activate

I think what your problem is, is that you haven't decleard what wkb/ThisWorkbook will be, you have told it the varible it will be in the Dim but you have then done nothing with it, you need to tell the code which work book you want it to use, after that you can use it in your code. 我认为您的问题是,您还没有清除wkb / ThisWorkbook的名称,您已经告诉它变暗的变量,但是您什么都没做,您需要告诉代码哪个可以工作预定要使用的书,之后可以在代码中使用它。

Hope this helps 希望这可以帮助

If you don't understand what I mean comment and i'll explain it in greater detail if I can. 如果您不明白我的意思,我会详细解释。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM