简体   繁体   English

循环直到一行中的最后一列并填充组合框

[英]Loop until the last column in a row and fill comboBox

How to fill my ComboBox1 with all the values of a row without blanks from B2 until the End of the row?如何使用从 B2 到行尾没有空格的行的所有值填充我的 ComboBox1?

I know :我知道 :

Sheets("Sheetname").Range("A2:A" & .Range("A65536").End(xlUp).Row).Value

but this is the code to loop until the last row in a column, I want to do the opposite.但这是循环到列中最后一行的代码,我想做相反的事情。

I would use a variable ( LC ) to store the column index of your last used column for readability.我会使用一个变量 ( LC ) 来存储上次使用的列的列索引以提高可读性。 You should also declare a Worksheet variable, or use a With Block to ensure all of your objects are properly qualified.您还应该声明一个Worksheet变量,或使用With Block来确保您的所有对象都被正确限定。

Dim ws As Worksheet: Set ws = ThisWorkbook.Sheets("Sheet1")
Dim LC As Long

LC = ws.Cells(2, ws.Columns.Count).End(xlToLeft).Column

ws.Range(ws.Cells(2, 2), ws.Cells(2, LC)).[what?]

Here is how you could implement a Column Loop这是实现Column Loop

Option Explicit

Dim ws As Worksheet: Set ws = ThisWorkbook.Sheets("Sheet1")
Dim LC As Long

LC = ws.Cells(2, ws.Columns.Count).End(xlToLeft).Column

For i = 2 To LC
    If ws.Cells(2, i) = "" Then
        MsgBox "Blank Cell: " & ws.Cells(2, i).Address(False, False)
    Else
        MsgBox "Non-Blank Cell: " & ws.Cells(2, i).Address(False, False)
    End If
Next i

I think what you're looking for is:我想你要找的是:

Sheets("Sheetname").Range(Cells(2, 2), Cells(2, Cells(2, Columns.Count).End(xlToLeft).Column))

Note - it looks like in your example you were using a With block - so add periods in front of all those cells to make sure your ranges are fully qualified.注意 - 在您的示例中,您似乎使用了With块 - 因此在所有这些单元格前添加句点以确保您的范围是完全合格的。

Fill Combo With Row (Resize Version)用行填充组合(调整大小版本)

ActiveSheet活动表

Private Sub UserForm_Activate()

   Const cStrFirst As String = "A2"           ' First Cell Range

   Dim vntRange As Variant                    ' Range Array
   Dim i As Integer                           ' Columns Counter

   vntRange = Range(cStrFirst).Resize(, Cells(Range(cStrFirst).Row, _
       Columns.Count).End(xlToLeft).Column)

   For i = 1 To UBound(vntRange, 2)
     If vntRange(1, i) <> "" Then
       ComboBox1.AddItem vntRange(1, i)
     End If
   Next

End Sub

Any Sheet任何工作表

Private Sub UserForm_Activate()

   Const cStrSheet As Variant = "Sheetname"   ' Sheet Name/Index
   Const cStrFirst As String = "A2"           ' First Cell Range

   Dim vntRange As Variant                    ' Range Array
   Dim i As Integer                           ' Columns Counter

   With Worksheets(cStrSheet)
     vntRange = .Range(cStrFirst).Resize(, .Cells(.Range(cStrFirst).Row, _
       .Columns.Count).End(xlToLeft).Column)
   End With

   For i = 1 To UBound(vntRange, 2)
     If vntRange(1, i) <> "" Then
       ComboBox1.AddItem vntRange(1, i)
     End If
   Next

End Sub
dim xrow1 as long, xrow2 as long, ws1 as Worksheet, ws2 as Worksheet

set ws1 = ThisWorkbook.Worksheets("worksheet1")
set ws2 = ThisWorkbook.Worksheets("worksheet2")
xrow2 = 2

for xrow1 = 2 to 65536
    if ws1.cells(xrow1, 1) = "" Then
    else
        ws1.cells(xrow1, 1).value = ws2.cells(xrow2, 1).value     
    end if
    xrow2 = xrow2 + 1
next xrow1

This would copy all the values in the first column of ws1 to ws2 and skip over all of the blank rows.这会将 ws1 第一列中的所有值复制到 ws2 并跳过所有空白行。 I'm not familiar with the specific syntax for a combo box, but I assume if you altered my code slightly, you could make it work.我不熟悉组合框的特定语法,但我假设如果您稍微更改我的代码,您可以使其工作。

If you want me to code something specific to your project, let me see what you have written so far and/or some of the data you want transferred.如果你想让我为你的项目编写一些特定的代码,让我看看你到目前为止写了什么和/或你想要传输的一些数据。

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

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