脱离上下文并在列中选择填充的值

时间:2019-02-05 22:04:09

标签: excel vba

我整天都在尝试解决2个问题。每当我尝试观看任何变量时,无论手表栏中的变量是什么,都首先要开始。我什至尝试只设置一个变量等于一个数字并观看它,但它仍然给了我。

第二,我试图将B列中所有具有值的值放入数组(TagName)中,这使我无法自拔。这是for循环的重点。脱离上下文的事情对案件没有帮助。

仅供参考,“ ist”是i作为字符串,但随后我添加了B只是为了缩短代码。

不用担心那些已经可以工作的代码带来的额外暗淡

谢谢您的帮助!

0

3 个答案:

答案 0 :(得分:0)

如果您只想要带有值的单元格,则应将其作为循环的一部分。我认为这应该有效。如果您混合使用字符串和数字,我也将数组更改为变体。

Sub GenTags()

Dim FolderPath As String
Dim OutputFileNum As Integer
Dim TagName(100) As Variant
Dim i As Long, c As Long
Dim ist As String

Sheets("Parameters").Activate


For i = 1 To ActiveWorkbook.ActiveSheet.Columns("B").End(xlDown).Row
    If Not IsEmpty(Range("B" & i)) Then
        TagName(c) = Range("B" & i).Value
        c = c + 1
    End If


Next


End Sub

答案 1 :(得分:0)

这种方法更加精细,可以处理列中的空白单元格。

轻松自定义“ >>>>”部分

Sub GenTags()

Dim FolderPath As String
Dim OutputFileNum As Integer


Dim ist As String

' Define object variables
Dim sourceSheet As Worksheet
Dim paramSheet As Worksheet
Dim sourceRange As Range
Dim cellEval As Range

' Define other variables
Dim sourceSheetName As String
Dim paramSheetName As String
Dim sourceColumn As String


Dim tagName() As Variant
Dim counter As Long ' before i
Dim nonBlankCounter As Long
Dim totalCells As Long


' >>> Customize to fit your needs
sourceSheetName = "Sheet1"
paramSheetName = "Parameters"
sourceColumn = "B"

' Initialize objects - Change sheets names
Set sourceSheet = ThisWorkbook.Worksheets(sourceSheetName)
Set paramSheet = ThisWorkbook.Worksheets(paramSheetName)
Set sourceRange = Application.Union(sourceSheet.Columns(sourceColumn).SpecialCells(xlCellTypeConstants), sourceSheet.Columns(sourceColumn).SpecialCells(xlCellTypeFormulas))

' Get how many items in column b are
totalCells = sourceRange.Cells.Count

' Redimension the array to include all the items
ReDim tagName(totalCells)

' Initilize the counter (for documentation sake)
counter = 0

For Each cellEval In sourceRange

    ' Add non empty values
    If Trim(cellEval.Value) <> vbNullString Then

        ' Store it in the array
        tagName(counter) = cellEval.Value

        counter = counter + 1

    End If

Next cellEval

' Redim to leave only used items
ReDim Preserve tagName(counter - 1)

End Sub

让我知道是否有帮助!

答案 2 :(得分:0)

感谢您的回复。不幸的是,我不得不把这个项目推迟到昨天,但是我确实尝试了两个答案,但都没有用。我决定对整个代码采取不同的方向,并使其正常工作。谢谢您的帮助,也很抱歉收到您的回复。