简体   繁体   English

定义所有可见的单元格在一个范围内的行中

[英]Define all visible cells are in a row of a range to a in sequence

I need defines each Cells are in a range row in sequence If confront a Hidden column. 我需要定义每个Cells按顺序位于范围行中(如果面对“隐藏”列)。 In pointed area of code below, when reached a hidden column in the specific row, jump to next row and at last refer to continue of this row and assign to RngCell variable. 在下面的代码指向区域中,当到达特定行中的隐藏列时,跳至下一行,最后引用该行的继续并分配给RngCell变量。

I want when For Each loop reached a hided column, continue the cells assigning to next visible cell of appropriated row. 我想要当For Each循环到达隐藏的列时,继续将单元格分配给相应行的下一个可见单元格。

Sub CsvExportRange(rngRange As Range, strFileName As Variant, strCharset As String)

    Dim rngRow As Range
    Dim objStream As Object

    Set objStream = CreateObject("ADODB.Stream")
    objStream.Type = 2
    objStream.Charset = strCharset
    objStream.Open

    OffColumns (True)
    Call Tax_WP

    For Each rngRow In rngRange.Rows 'The problem becuase Here
        objStream.WriteText CsvFormatRow(rngRow) 
    Next rngRow

    objStream.SaveToFile strFileName, 2
    objStream.Close
End Sub
Function CsvFormatRow(rngRow As Range) As String

    Dim arrCsvRow() As String
    Dim strRowEnd As String

    strRowEnd = vbCrLf

    ReDim arrCsvRow(rngRow.Cells.Count - 1)
    Dim rngCell As Range
    Dim lngIndex As Long

    lngIndex = 0

    For Each rngCell In rngRow.Cells '***Problem is Here***
        arrCsvRow(lngIndex) = CsvFormatString(rngCell.Value)
        lngIndex = lngIndex + 1
    Next rngCell


    CsvFormatRow = Join(arrCsvRow, ",") & vbCrLf

End Function
Function CsvFormatString(strRaw As String) As String

    Dim boolNeedsDelimiting As Boolean

    Dim strDelimiter, strSeparator, strDelimiterEscaped As String

    strDelimiter = """"
    strSeparator = strSeparator = ","
    strDelimiterEscaped = strDelimiter & strDelimiter

    boolNeedsDelimiting = InStr(1, strRaw, strDelimiter) > 0 _
        Or InStr(1, strRaw, chr(10)) > 0 _
        Or InStr(1, strRaw, strSeparator) > 0

    CsvFormatString = strRaw

    If boolNeedsDelimiting Then
        CsvFormatString = strDelimiter & _
            Replace(strRaw, strDelimiter, strDelimiterEscaped) & _
            strDelimiter
    End If

End Function

I'm having a hard time understanding what you want to happen when you encounter a hidden column. 当遇到隐藏的列时,我很难理解要发生什么。 In your post you say "jump to next row", but then in bold, you say, "continue the cells assigning to next visible cell of appropriated row" which is unclear but might mean you wish to continue on the row, but skip the hidden column? 在您的帖子中,您说“跳至下一行”,然后用黑体表示“继续分配给相应行的下一个可见单元格的单元格”,虽然尚不清楚,但可能意味着您希望继续在该行上,但跳过该行隐藏的列?

You also use an array combined with rngRow.Cells.Count - 1 which is always going to get you 16,383 to define your array. 您还可以将数组与rngRow.Cells.Count - 1结合使用,这总是会让您16,383来定义您的数组。 It might be easier if you dynamically updated your array while you looped through your rows, or simply string together your text to a single variable? 如果在循环遍历行时动态更新数组,或者只是将文本串联到单个变量中,可能会更容易?

Are you ultimately trying to avoid a bunch of ",,"? 您最终是否要避免使用一堆“,”? If so, this method may work better. 如果是这样,此方法可能会更好。

In summary, the below code will: 总之,以下代码将:

  • End your CSVFormatRow function when rngCell is in a hidden column. rngCell在隐藏列中时,结束CSVFormatRow函数。
  • End your CSVFormatRow function when rngCell is in a column that is outside CSV file's used range of ANY row (not just the row it's looping through). rngCell位于CSV文件的ANY行(不仅仅是其循环经过的行)的使用范围之外的列中时,请结束CSVFormatRow函数。
  • Instead of using an array, the code just string together members to variable StrinCVSRow 该代码仅使用成员将变量StrinCVSRow串在一起,而不是使用数组
  • Exclude the final , in StrinCVSRow 排除最终,StrinCVSRow

If that doesn't address your question, please clarify the requirements. 如果那不能解决您的问题,请阐明要求。 Hope this helps. 希望这可以帮助。

Function CsvFormatRow(rngRow As Range) As String

    'Dim arrCsvRow() As String

    Dim StrinCVSRow As String 'new variable to string row text together.

    Dim strRowEnd As String 'these two lines aren't doing anything
    strRowEnd = vbCrLf      'these two lines aren't doing anything

    'ReDim arrCsvRow(rngRow.Cells.Count - 1) ' always equals 16383
    Dim rngCell As Range

    'Dim lngIndex As Long ' (not needed if Array omitted)

    'lngIndex = 0

    For Each rngCell In rngRow.Cells '***Problem is Here***
        If rngCell.EntireColumn.Hidden = True Then
            'The "Exit For" will end the code here and jump to next row.
            'If you wanted to continue through the row you can leave
            'this condition in, but delete the "Exit For"
            'and this will simply "skip" this particular column.
            Exit For
        ElseIf Intersect(rngCell, Sheets(rngCell.Sheet.Name).UsedRange) Is Nothing Then
            'ends loop if last column with any data in entire CSV file is reached.
            Exit For

        Else
            StrinCVSRow = CsvFormatString(rngCell.Value) & ","
     '       lngIndex = lngIndex + 1
        End If

    Next rngCell

    'CsvFormatRow = Join(arrCsvRow, ",") & vbCrLf

    'left function trims off final ",".
    CsvFormatRow = Left(StrinCVSRow, Len(StrinCVSRow) - 1) & vbCrLf

End Function

In code bellow I reached a way that truly catch all visible cells are in a table row with integrating SpecialCells(12) , Fore each and For i : 在下面的代码中,我通过集成SpecialCells(12)Fore eachFor i来真正捕获表格行中的所有可见单元格:

(with writing inserting comments in above positions) (以书面形式在上述位置插入评论)

Sub CsvExportRange(rngRange As Object, strFileName As String, strCharset As String)

    Dim rngRow As Range
    Dim objStream As Object
    Dim i, lngFR As Long 'First Row

    lngFR = rngRange.SpecialCells(xlCellTypeVisible).Rows(1).Row - rngRange.Rows(1).Row + 1 'giving absolute Row number of first Table's row.

    Set objStream = CreateObject("ADODB.Stream")
    objStream.Type = 2
    objStream.Charset = strCharset
    objStream.Open
    For i = lngFR To lngFR + rngRange.SpecialCells(xlCellTypeVisible).Rows.Count - 1
        objStream.WriteText CsvFormatRow(rngRange.Rows(i)) 'Gives all visible lines are in table entirely of sheet. (Here using Fore i... is suitable)
    Next i

    objStream.SaveToFile strFileName, 2
    objStream.Close
End Sub
Function CsvFormatRow(rngRow As Variant) As String

    Dim arrCsvRow() As String
    Dim strRowEnd As String

    strRowEnd = vbCrLf

    ReDim arrCsvRow(rngRow.SpecialCells(xlCellTypeVisible).Cells.Count - 1) 'Defining array dimension for saving each cell in a array room and at last using Join() command
    Dim rngCell As Range
    Dim lngIndex As Long

    lngIndex = 0

    For Each rngCell In rngRow.SpecialCells(xlCellTypeVisible).Cells 'Here we used For Each to give only visible cells of above entire line so thats line is there in Table range.
        arrCsvRow(lngIndex) = CsvFormatString(rngCell.Value)
        lngIndex = lngIndex + 1
    Next rngCell

    CsvFormatRow = Join(arrCsvRow, ",") & vbCrLf 'At last, here generating destination CSV file data file.

End Function
Function CsvFormatString(strRaw As String) As String

    Dim boolNeedsDelimiting As Boolean

    Dim strDelimiter, strSeparator, strDelimiterEscaped As String

    strDelimiter = """"
    strSeparator = strSeparator = ","
    strDelimiterEscaped = strDelimiter & strDelimiter

    boolNeedsDelimiting = InStr(1, strRaw, strDelimiter) > 0 _
        Or InStr(1, strRaw, chr(10)) > 0 _
        Or InStr(1, strRaw, strSeparator) > 0

    CsvFormatString = strRaw

    If boolNeedsDelimiting Then
        CsvFormatString = strDelimiter & _
            Replace(strRaw, strDelimiter, strDelimiterEscaped) & _
            strDelimiter
    End If

End Function

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

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