简体   繁体   中英

Adding to a Range.Value method

Currently I have a spreadsheet that automates some concenate functions for me, but its restricted to an array of 5 columns...

ws.Range(Cells(x, 10), Cells(x, 10)).Value = "" & Source(1) & " " & Source(2) & " " & Source(3) & " " & Source(4) & " " & Source(5) & ""

I've rewrote the code to better and more convieniently automate this task, which includes a userform that populates lists with the Columns.UsedRange function. I want to take the results of the selection of the listbox, and add to the above code. In otherwords if I select B through D my code becomes...

ws.Range(Cells(x, v), Cells(x, v)).Value = "" & Source(1) & " " & Source(2) & " " & Source(3) & ""

v is lastcolumn + 1, x is a for loop starting from the first column selected in the userform.

So, what would be a good way to add, and take away from the one function?

Thanks in advance,

Adam

Cells is a range object. Range(Cell1, Cell2) returns a range that extends from Cell1 to Cell2. For example: Range("A1","A10") is the same as Range("$A$1:$A$10"). If the ws is the ActiveSheet then ws.Range(Cells(x, 10), Cells(x, 10)).Value is equivalent to Cells(x, 10).Value. If ws is not the ActiveSheet, you'll receive an Method 'Range' of object '_Worksheet' failed error, because Cells are assumed to be the ActiveSheet.Cells unless otherwise qualified.

You don't need to wrap a string in empty strings. "" & Source(1) & " " & Source(2) & " " & Source(3) & "" is the same as Source(1) & " " & Source(2) & " " & Source(3)

ws.Range(Cells(x, 10), Cells(x, 10)).Value = "" & Source(1) & " " & Source(2) & " " & Source(3) & " " & Source(4) & " " & Source(5) & ""

A better way to write the code above is:

ws.Cells(x, 10).Value =Source(1) & " " & Source(2) & " " & Source(3) & " " & Source(4) & " " & Source(5)

Use Join(SourceArray, Delemeter) to concatenate all the elements of a one dimensional array.

ws.Cells(x, 10).Value = Join(Source, " ")

Clarify you question and I'll provide a more specific answer.

I got it with Join. Thanks for your time. Here's my code if anyone wants to do something like it...

Module:

Public wb As Workbook
Public ws As Worksheet
Public LastRow As Integer
Public LastColumn As Integer


Sub Button1_Click()
Set wb = ActiveWorkbook
Set ws = wb.Sheets("Line 6 Winder")
LastRow = ws.UsedRange.Rows.Count
LastColumn = ws.UsedRange.Columns.Count
UserForm1.ListBox_Populate LastRow, LastColumn
UserForm1.Show
End Sub

Sub Conc_Data(StartColStr As String, StartIndex As Integer, EndColStr As String, EndIndex As Integer)
Dim source(1 To 26) As String

ColCount = EndIndex - StartIndex
DataColumn = LastColumn + 2
For x = 1 To LastRow
    Z = 0
    For y = StartIndex To EndIndex
        Z = Z + 1
        source(Z) = ws.Range(Cells(x, y), Cells(x, y)).Value
    Next
    ws.Range(Cells(x, DataColumn), Cells(x, DataColumn)).Value = Join(source, "")
Next
End Sub

Form:

Public Sub ListBox_Populate(s As Integer, e As Integer)
Dim Col_Letter As String
Dim vArr As Variant
    For x = 1 To e
        vArr = Split(Cells(1, x).Address(True, False), "$")
        Col_Letter = vArr(0)
        Me.StartList.AddItem Col_Letter
        Me.EndList.AddItem Col_Letter
    Next
End Sub

Private Sub OkayButton_Click()
Dim StartColStr As String
Dim StartIndex As Integer
Dim EndColStr As String
Dim EndIndex As Integer

StartColStr = Me.StartList.Value
StartIndex = Range(Me.StartList.Value & 1).Column
EndColStr = Me.EndList.Value
EndIndex = Range(Me.EndList.Value & 1).Column
Module1.Conc_Data StartColStr, StartIndex, EndColStr, EndIndex
Unload Me

End Sub

Private Sub UserForm_Click()

End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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